-
Notifications
You must be signed in to change notification settings - Fork 28
/
script.js
101 lines (94 loc) · 3.58 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// from: http://stackoverflow.com/a/5303242/945521
if ( XMLHttpRequest.prototype.sendAsBinary === undefined ) {
XMLHttpRequest.prototype.sendAsBinary = function(string) {
var bytes = Array.prototype.map.call(string, function(c) {
return c.charCodeAt(0) & 0xff;
});
this.send(new Uint8Array(bytes).buffer);
};
};
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
window.fbAsyncInit = function() {
FB.init({
appId : "404477149649756",
status : true,
cookie : true,
xfbml : true // parse XFBML
});
};
function postImageToFacebook( authToken, filename, mimeType, imageData, message )
{
// this is the multipart/form-data boundary we'll use
var boundary = '----ThisIsTheBoundary1234567890';
// let's encode our image file, which is contained in the var
var formData = '--' + boundary + '\r\n'
formData += 'Content-Disposition: form-data; name="source"; filename="' + filename + '"\r\n';
formData += 'Content-Type: ' + mimeType + '\r\n\r\n';
for ( var i = 0; i < imageData.length; ++i )
{
formData += String.fromCharCode( imageData[ i ] & 0xff );
}
formData += '\r\n';
formData += '--' + boundary + '\r\n';
formData += 'Content-Disposition: form-data; name="message"\r\n\r\n';
formData += message + '\r\n'
formData += '--' + boundary + '--\r\n';
var xhr = new XMLHttpRequest();
xhr.open( 'POST', 'https://graph.facebook.com/me/photos?access_token=' + authToken, true );
xhr.onload = xhr.onerror = function() {
console.log( xhr.responseText );
};
xhr.setRequestHeader( "Content-Type", "multipart/form-data; boundary=" + boundary );
xhr.sendAsBinary( formData );
};
var canvas;
var context;
var centerX;
var img;
var authToken;
function drawCanvas() {
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
img = new Image();
img.src = "heroes.jpg";
img.onload = function() {
context.drawImage(img, 0, 0);
context.fillStyle = "rgb(231,222,201)";
centerX = canvas.width / 2;
context.textAlign = 'center';
context.font = 'italic bold 16px sans-serif';
context.fillText("The month of the exams!", centerX, 70);
context.fillText("Student population decreases.", centerX, 130);
};
};
function onKeyUp(e){
context.drawImage(img, 0, 0);
var topText = document.getElementById("toptext").value;
context.fillText(topText, centerX, 70);
var bottomText = document.getElementById("bottomtext").value;
context.fillText(bottomText, centerX, 130);
};
function postCanvasToFacebook() {
var data = canvas.toDataURL("image/png");
var encodedPng = data.substring(data.indexOf(',') + 1, data.length);
var decodedPng = Base64Binary.decode(encodedPng);
FB.getLoginStatus(function(response) {
if (response.status === "connected") {
postImageToFacebook(response.authResponse.accessToken, "heroesgenerator", "image/png", decodedPng, "www.heroesgenerator.com");
} else if (response.status === "not_authorized") {
FB.login(function(response) {
postImageToFacebook(response.authResponse.accessToken, "heroesgenerator", "image/png", decodedPng, "www.heroesgenerator.com");
}, {scope: "publish_actions"});
} else {
FB.login(function(response) {
postImageToFacebook(response.authResponse.accessToken, "heroesgenerator", "image/png", decodedPng, "www.heroesgenerator.com");
}, {scope: "publish_actions"});
}
});
};