-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
301 lines (272 loc) · 11 KB
/
index.html
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<!DOCTYPE html>
<html>
<head>
<title>CRIMSON FB PROF PIC GENERATOR</title>
</head>
<body>
<script>
/*
* JavaScript Canvas to Blob 2.0.5
* https://github.com/blueimp/JavaScript-Canvas-to-Blob
*
* Copyright 2012, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*
* Based on stackoverflow user Stoive's code snippet:
* http://stackoverflow.com/q/4998908
*/
/*jslint nomen: true, regexp: true */
/*global window, atob, Blob, ArrayBuffer, Uint8Array, define */
(function (window) {
'use strict';
var CanvasPrototype = window.HTMLCanvasElement &&
window.HTMLCanvasElement.prototype,
hasBlobConstructor = window.Blob && (function () {
try {
return Boolean(new Blob());
} catch (e) {
return false;
}
}()),
hasArrayBufferViewSupport = hasBlobConstructor && window.Uint8Array &&
(function () {
try {
return new Blob([new Uint8Array(100)]).size === 100;
} catch (e) {
return false;
}
}()),
BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder ||
window.MozBlobBuilder || window.MSBlobBuilder,
dataURLtoBlob = (hasBlobConstructor || BlobBuilder) && window.atob &&
window.ArrayBuffer && window.Uint8Array && function (dataURI) {
var byteString,
arrayBuffer,
intArray,
i,
mimeString,
bb;
if (dataURI.split(',')[0].indexOf('base64') >= 0) {
// Convert base64 to raw binary data held in a string:
byteString = atob(dataURI.split(',')[1]);
} else {
// Convert base64/URLEncoded data component to raw binary data:
byteString = decodeURIComponent(dataURI.split(',')[1]);
}
// Write the bytes of the string to an ArrayBuffer:
arrayBuffer = new ArrayBuffer(byteString.length);
intArray = new Uint8Array(arrayBuffer);
for (i = 0; i < byteString.length; i += 1) {
intArray[i] = byteString.charCodeAt(i);
}
// Separate out the mime component:
mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// Write the ArrayBuffer (or ArrayBufferView) to a blob:
if (hasBlobConstructor) {
return new Blob(
[hasArrayBufferViewSupport ? intArray : arrayBuffer],
{type: mimeString}
);
}
bb = new BlobBuilder();
bb.append(arrayBuffer);
return bb.getBlob(mimeString);
};
if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) {
if (CanvasPrototype.mozGetAsFile) {
CanvasPrototype.toBlob = function (callback, type, quality) {
if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) {
callback(dataURLtoBlob(this.toDataURL(type, quality)));
} else {
callback(this.mozGetAsFile('blob', type));
}
};
} else if (CanvasPrototype.toDataURL && dataURLtoBlob) {
CanvasPrototype.toBlob = function (callback, type, quality) {
callback(dataURLtoBlob(this.toDataURL(type, quality)));
};
}
}
if (typeof define === 'function' && define.amd) {
define(function () {
return dataURLtoBlob;
});
} else {
window.dataURLtoBlob = dataURLtoBlob;
}
}(window));
</script>
<script>
var canOverlay = false;
var profpic;
var authResponse;
function $() {
return document.querySelector.apply(document, arguments);
}
function log(txt) {
$("#console").innerText = txt;
}
function login() {
log("Logging in...");
FB.login(function (response) {
if (response.authResponse) {
log("Logged in.");
authResponse = response.authResponse;
go();
} else {
log("Login failed. Try again.");
}
}, {
scope: 'publish_actions'
});
}
function logout() {
log("Logging out...");
FB.api(
"/me/permissions",
"delete",
function (response) {
if (response && !response.error) {
log("Logged out.");
window.location.reload();
} else {
console.log(response);
log("Couldn't log you out. Try again?");
}
}
);
}
function overlay() {
if (!canOverlay) return;
var canvas = $("#canvas");
canvas.width = profpic.naturalWidth;
canvas.height = profpic.naturalHeight;
var context = canvas.getContext('2d');
context.drawImage(profpic, 0, 0);
context.drawImage($("#overlay-" + $("#sel").value), 0, 0, profpic.naturalWidth, profpic.naturalHeight);
}
function upload() {
log("Exporting image...");
$("#canvas").toBlob(function (blob) {
var fd = new FormData();
fd.append("access_token", authResponse.accessToken);
fd.append("source", blob, {type: 'text/jpg'});
fd.append("no_story", true);
var req = new XMLHttpRequest();
req.addEventListener("progress", function (e) {
if (e.lengthComputable) {
var percentComplete = e.loaded / e.total;
log("Uploading... " + percentComplete + "%");
} else {
log("Uploading...");
}
}, false);
req.addEventListener("load", function () {
log("Upload complete! Generating link...");
var resp = JSON.parse(req.responseText);
FB.api(
"/" + resp.id,
{fields: 'link'},
function (response) {
if (response && !response.error) {
window.open(response.link + "&makeprofile=1");
$("#uploadlink").style.display = "none";
$("#setlink").style.display = "block";
$("#setlink").href = response.link + "&makeprofile=1";
log("Set that as your profile picture. Come again soon!");
} else {
log("Unable to get link to photo.")
}
}
)
}, false);
req.addEventListener("error", function () {
log("Something went wrong. Try again?");
}, false);
req.open("POST", "https://graph.facebook.com/me/photos");
req.send(fd);
}, 'image/jpeg');
}
function go() {
FB.api('/me/permissions', function (response) {
var permissions = response.data.filter(function (o) {
return o.permission == "publish_actions" && o.status == "granted";
});
if (permissions.length !== 1) {
$("#moreperms").style.display = "block";
$("#loginbutton").style.display = "block";
} else {
$("#moreperms").style.display = "none";
$("#loginbutton").style.display = "none";
$("#logoutbutton").style.display = "inline";
FB.api(
"/me/picture?width=8000&height=8000",
function (response) {
if (response && !response.error) {
$("#sel").style.display = "inline";
$("#uploadlink").style.display = "block";
profpic = new Image();
profpic.crossOrigin = "Anonymous";
profpic.src = response.data.url;
profpic.onload = function () {
canOverlay = true;
overlay();
}
} else {
log("Could not load profile picture. Try again? :S");
}
}
);
}
});
}
window.fbAsyncInit = function() {
FB.init({
appId: window.location.hostname == 'localhost' ? '736869426439800' : '736552736471469',
xfbml: true,
version: 'v2.4',
status: true,
});
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
log("Logged in.");
authResponse = response.authResponse;
go();
}
else {
log("Please login.");
$("#loginbutton").style.display = "block";
}
});
};
(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/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<h1>The Harvard Crimson</h1>
<h2>Facebook profile picture generator</h2>
<p>Spring 2017</p>
<span id="moreperms" style="color: red; display: none;">
Need permissions to publish. We don't post, except to upload this photo. Please login again.
</span>
<button id="loginbutton" onclick="login()" style="display: none; background-color: #1e5d96; color: #fff; border: 0; cursor: pointer; font-size: 14px; font-family: helvetica, arial, sans-serif; font-weight: 500;">Login with Facebook</button>
<select id="sel" style="display: none" onchange="overlay()">
<option>right</option>
</select><br>
<canvas id="canvas" width="0" height="0"></canvas>
<br>
<a id="uploadlink" href="javascript: upload()" style="display: none">Upload to Facebook!</a>
<a id="setlink" href="" style="display: none; font-size: 48px; position: absolute; height: 100px; width: 500px; background: cyan; border:1px solid black; padding: 10px; top: 50%; left: 50%; margin: -50px 0 0 -150px; text-align: center;">Set as profile picture!</a>
<br>
<strong>Status: </strong><span id="console">Loading...</span>
<button id="logoutbutton" onclick="logout()" style="display: none;">Logout</button>
<img id="overlay-right" src="right.png" style="display: none">
</body>
</html>