forked from bufferapp/buffer-extension-shared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer-overlay.js
416 lines (375 loc) · 17 KB
/
buffer-overlay.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Build that overlay!
// Triggered by code working from the button up
var bufferOverlay = function(data, config, doneCallback) {
if( ! doneCallback ) doneCallback = function () {};
if( ! config ) return;
// Put together a query string for the iframe
var buildSrc = function() {
var src = config.overlay.endpoint;
if( data.local ) src = config.overlay.localendpoint;
// Add button attributes
var first = true, count = 0;
for(var i=0, l=config.attributes.length; i < l; i++) {
var a = config.attributes[i];
if( ! data[a.name] ) continue;
if( first ) { src += '?'; first = false; }
count += 1;
if( count > 1 ) src += '&';
src += a.name + '=' + a.encode(data[a.name]);
}
return src;
};
var src = buildSrc();
if (xt.options['buffer.op.tpc-disabled']) {
window.open(src, null, 'height=600,width=850');
// Bind close listener
// Listen for when the overlay has closed itself
bufferpm.bind("buffermessage", function(overlaydata) {
bufferpm.unbind("buffermessage");
setTimeout(function () {
doneCallback(overlaydata);
}, 0);
window.focus();
});
return;
}
// Create the iframe and add the footer:
var temp = document.createElement('iframe');
temp.allowtransparency = 'true';
temp.scrolling = 'no';
temp.id = 'buffer_overlay';
temp.name = 'buffer_overlay';
temp.style.cssText = config.overlay.getCSS();
temp.src = buildSrc();
var footerButtonBaseCSS = [
'display: inline-block;',
'border: 1px solid #aaa;',
'border-top: 1px solid #ccc;',
'border-left: 1px solid #ccc;',
'padding: 6px 20px;',
'font-size: 13px;',
'font-weight: bold;',
'line-height: 22px;',
'text-decoration: none;',
'text-shadow: 0 1px #fff;',
'cursor: pointer;',
'font-family: \'HelveticaNeue\',\'Helvetica Neue\',Helvetica,Arial,sans-serif !important;',
'-moz-border-radius: 3px;',
'border-radius: 3px;',
'-moz-background-clip: padding;',
'-webkit-background-clip: padding-box;',
'background-clip: padding-box;',
'box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2),inset 0 1px 0 rgba(255, 255, 255, 0.2);'
].join('');
var footerButtonWhiteCSS = [
footerButtonBaseCSS,
'background: #f8f8f8;',
'background: -moz-linear-gradient(top, #ffffff 0%, #f8f8f8 100%);',
'background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ffffff), color-stop(100%, #f8f8f8));',
'background: -webkit-linear-gradient(top, #ffffff 0%, #f8f8f8 100%);',
'background: -o-linear-gradient(top, #ffffff 0%, #f8f8f8 100%);',
'background: -ms-linear-gradient(top, #ffffff 0%, #f8f8f8 100%);',
'background: linear-gradient(top, #ffffff 0%, #f8f8f8 100%);'
].join('');
var footerButtonBlueCSS = [
footerButtonBaseCSS,
'background: #168EEA;',
'background: -moz-linear-gradient(top, #1F93EB 0%, #168EEA 100%);',
'background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #1F93EB), color-stop(100%, #168EEA));',
'background: -webkit-linear-gradient(top, #1F93EB 0%, #168EEA 100%);',
'background: -o-linear-gradient(top, #1F93EB 0%, #168EEA 100%);',
'background: -ms-linear-gradient(top, #1F93EB 0%, #168EEA 100%);',
'background: linear-gradient(top, #1F93EB 0%, #168EEA 100%);',
'box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2),inset 0 1px 0 rgba(255, 255, 255, 0.2);',
'text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);',
'border: 1px solid #1277C5;'
].join('');
var footerButtonIconCSS = [
'display: inline-block;',
'width: 10px;',
'height: 11px;',
'margin-right: 4px;',
'background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+Cjxzdmcgd2lkdGg9IjEwcHgiIGhlaWdodD0iMTRweCIgdmlld0JveD0iMCAwIDEwIDE0IiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHhtbG5zOnNrZXRjaD0iaHR0cDovL3d3dy5ib2hlbWlhbmNvZGluZy5jb20vc2tldGNoL25zIj4KICAgIDx0aXRsZT5TbGljZSAxPC90aXRsZT4KICAgIDxkZXNjcmlwdGlvbj5DcmVhdGVkIHdpdGggU2tldGNoIChodHRwOi8vd3d3LmJvaGVtaWFuY29kaW5nLmNvbS9za2V0Y2gpPC9kZXNjcmlwdGlvbj4KICAgIDxkZWZzPjwvZGV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jI2MzM3NDQ5IDguNjU3NDA3NDEsMS42MjU1MTQ0IDcuODQ3MjIyMjIsMS44NjQxOTc1MyBDNy4wMzcwMzcwNCwyLjEwMjg4MDY2IDYuMDg3OTYyOTYsMi4yMjIyMjIyMiA1LDIuMjIyMjIyMjIgTDUsMC4yMjIyMjIyMjIgQzUsMC4xNjQ2MDkwNTMgNC45ODA3MDk4OCwwLjExMzE2ODcyNCA0Ljk0MjEyOTYzLDAuMDY3OTAxMjM0NiBDNC45MDM1NDkzOCwwLjAyMjYzMzc0NDkgNC44NTMzOTUwNiwwIDQuNzkxNjY2NjcsMCBDNC43Mzc2NTQzMiwwIDQuNjk1MjE2MDUsMC4wMTY0NjA5MDUzIDQuNjY0MzUxODUsMC4wNDkzODI3MTYgTDAuMDgxMDE4NTE4NSwzLjgyNzE2MDQ5IEMwLjAyNzAwNjE3MjgsMy44NjgzMTI3NiAwLDMuOTI1OTI1OTMgMCw0IEwwLDQgWiBNMCw0IiBpZD0iU2hhcGUiPjwvcGF0aD4KICAgICAgICA8L2c+CiAgICA8L2c+Cjwvc3ZnPg==);',
'opacity: 0.8;'
].join('');
var footer = document.createElement('div');
footer.id = 'buffer_widget_footer';
footer.style.cssText = "z-index:999999999;background: #ffffff url(https://d389zggrogs7qo.cloudfront.net/images/bookmarklet_icon.png) 35px 16px no-repeat; background-size: 30px; box-shadow: 0 -1px 8px rgba(0, 0, 0, 0.1); border-top: 1px solid #ccc; border-bottom-left-radius: 4px; height: 60px; width: 100%; position: fixed; bottom: 0; right: 0;";
footer.innerHTML = [
'<ul id="buffer_widget_footer_list" style="float: right; margin-top: 13px; margin-right: 20px; min-width: 170px;">',
'<li style="list-style-type: none; background:none; width: auto; display: inline-block;">',
'<a href="https://bufferapp.com/app" target="_blank" class="buffer_widget_button_white" style="' + footerButtonWhiteCSS + '">',
'<i style="' + footerButtonIconCSS + '"></i> Visit Buffer Dashboard',
'</a>',
'</li>',
'</ul>'
].join('');
document.body.appendChild(footer);
var footerHoverCss = document.createElement('style');
footerHoverCss.type="text/css";
footerHoverCss.innerHTML = [
'#buffer_widget_footer a{color: #777!important;opacity:1.0!important;}',
'#buffer_widget_footer a:hover{color: #444!important;}',
'#buffer_widget_footer a.buffer_widget_button_white:hover{background-image:none !important;background-color:#fff !important;}',
'#buffer_widget_footer a.buffer_widget_button_blue{color:#fff!important;}',
'#buffer_widget_footer a.buffer_widget_button_blue:hover{background-image:none !important;background-color:#2D99EC !important;}',
'#buffer_widget_footer a:hover i{opacity:1.0;}'
].join('');
document.body.appendChild(footerHoverCss);
document.body.appendChild(temp);
// Returns a button and it's surrounding list element to insertion into
// the footer
function createButtonListElement(id, url, text, color){
// Color options: 'blue' or 'white'
color = color || 'blue';
var style = color === 'blue' ? footerButtonBlueCSS : footerButtonWhiteCSS;
var li = document.createElement('li');
li.style.cssText = 'list-style-type: none; background:none; width: auto; display: inline-block; margin-right: 8px;';
li.innerHTML = [
'<a id="' + id + '" href="' + url + '" target="_blank" class="buffer_widget_button_blue" style="' + style + '">',
text,
'</a>'
].join('');
return li;
}
// Given data sent via postmessage, this prepends a new button in the footer
function prependButton(buttonData){
var ul = footer.children[0];
var firstLi = ul.children[0];
var id = 'buffer_footer_button_insert';
var li = createButtonListElement(id, buttonData.url, buttonData.text, buttonData.color);
ul.insertBefore(li, firstLi);
// Send the iframe click info via postmessage for any new buttons.
// Original button data is passed back to the listening iframe
document.getElementById( id ).addEventListener('click', function(){
bufferpm({
target: temp.contentWindow,
type: 'buffer_buttonclick',
data: buttonData
});
}, false);
}
// Bind the event so the extension can add a call-to-action button in
// the footer which is in the page's DOM, not the iframe itself
bufferpm.bind('buffer_addbutton', prependButton);
// Bind close listener
// Listen for when the overlay has closed itself
bufferpm.bind("buffermessage", function(overlaydata) {
document.body.removeChild(temp);
document.body.removeChild(footer);
bufferpm.unbind("buffermessage");
bufferpm.unbind('buffer_addbutton');
setTimeout(function () {
doneCallback(overlaydata);
}, 0);
window.focus();
});
};
// bufferData is triggered by the buffer_click listener in
// the buffer-browser-embed file, where it's passed a port
// to communicate with the extension and data sent from the
// background page.
var bufferData = function (port, postData) {
if (window.top !== window) {
return;
}
var config = {};
config.local = false;
config.pocketWeb = false;
var segments = window.location.pathname.split('/');
if( window.location.host.indexOf("getpocket") != -1 && segments[2] == "read" ) config.pocketWeb = true;
// Specification for gathering data for the overlay
config.attributes = [
{
name: "url",
get: function (cb) {
if(config.pocketWeb){
var li = document.getElementsByClassName('original')[0];
var link = li.getElementsByTagName('a')[0].href;
cb(link);
}
else{
cb(window.location.href);
}
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "text",
get: function (cb) {
if(document.getSelection().toString().length) {
cb('"' + document.getSelection().toString() + '"');
}
else{
if(config.pocketWeb){
var header = document.getElementsByClassName('reader_head')[0];
var title = header.getElementsByTagName('h1')[0].innerHTML;
cb(title);
}
else{
cb(document.title);
}
}
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "retweeted_tweet_id",
get: function (cb) {
cb(postData.retweeted_tweet_id);
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "retweeted_user_id",
get: function (cb) {
cb(postData.retweeted_user_id);
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "retweeted_user_name",
get: function (cb) {
cb(postData.retweeted_user_name);
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "retweeted_user_display_name",
get: function (cb) {
cb(postData.retweeted_user_display_name);
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "picture",
get: function (cb) {
cb(postData.image);
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "embed",
get: function (cb) {
cb(postData.embed);
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "local",
get: function (cb) {
cb(config.local);
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "version",
get: function (cb) {
cb(postData.version);
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "placement",
get: function (cb) {
if( postData.placement ) cb(postData.placement);
else cb('general');
},
encode: function (val) {
return encodeURIComponent(val);
}
},
{
name: "client_assistance",
get: function (cb) {
cb('1');
},
encode: function (val) {
return encodeURIComponent(val);
}
}
];
config.overlay = {
endpoint: "https://bufferapp.com/add/",
localendpoint: "https://local.bufferapp.com/add/",
//endpoint: (config.local ? 'https:' : document.location.protocol) + '//bufferapp.com/add/',
//localendpoint: (config.local ? 'https:' : document.location.protocol) + '//local.bufferapp.com/add/',
getCSS: function () { return "border:none;height:100%;width:100%;position:fixed!important;z-index:99999999;top:0;left:0;display:block!important;max-width:100%!important;max-height:100%!important;padding:0!important;background: none; background-color: transparent; background-color: rgba(0, 0, 0, 0.1);"; }
};
// Method for handling the async firing of the cb
var executeAfter = function(done, count, data, cb) {
if(done === count) {
setTimeout(function(){
cb(data)
}, 0);
}
};
// Asynchronously gather data about the page and from embedded sources,
// like Twitter or Facebook. Currently the async is a bit over the top,
// and not used, but if we need aysnc down the line, it's there.
var getData = function (cb) {
var count = config.attributes.length;
var done = 0;
var data = {};
for(var i=0; i < count; i++) {
// Wrapped in a self-executing function to ensure we don't overwrite ‘a’
// and that the correct ‘i’ is used
(function (i) {
var a = config.attributes[i];
a.get(function(d) {
done += 1;
data[a.name] = d;
executeAfter(done, count, data, cb);
});
}(i));
}
};
// Transform the data somewhat, and then create an overlay.
// When it's done, fire buffer_done back to the extension
var createOverlay = function (data) {
if( data.embed ) {
if( typeof data.embed === "object" ) {
for( var i in data.embed ) {
if( data.embed.hasOwnProperty(i) ) {
data[i] = data.embed[i];
}
}
if( data.embed.text && !data.embed.url ) {
data.url = null;
}
data.embed = null;
} else {
data.text = data.embed;
data.url = null;
data.embed = null;
}
}
bufferOverlay(data, config, function (overlaydata) {
port.emit("buffer_done", overlaydata);
});
};
// It all starts here.
// createOverlay is the callback that should fire after getData has
// gathered all the necessaries
getData(createOverlay);
};