This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
buffer-overlay.js
620 lines (513 loc) · 18.2 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/* globals bufferpm, chrome, safari */
// Put together a query string for the iframe
var buildSrc = function(data, config) {
var src = data.local ?
config.overlay.localendpoint :
config.overlay.endpoint;
var qs = '';
config.attributes.forEach(function(attr, i){
if ( !data[ attr.name ] ) return;
if ( qs.length ) qs += '&';
qs += attr.name + '=' + attr.encode( data[attr.name] );
});
if (qs.length) src += '?' + qs;
return src;
};
var openPopUp = function(src, port, doneCallback, isSmallPopup) {
// Open popups from privileged code
port.emit('buffer_open_popup', { src: src, isSmallPopup: isSmallPopup });
// 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();
});
};
var shouldOpenSmallPopupForUrl = function(url) {
var hostname = window.location.hostname;
var pathname = window.location.pathname;
var isTwitterIntent = /twitter.com$/.test(hostname) && /^\/intent/.test(pathname);
if (isTwitterIntent) return true;
else return false;
}
// Build that overlay!
// Triggered by code working from the button up
var bufferOverlay = function(data, config, port, doneCallback) {
if( ! doneCallback ) doneCallback = function () {};
if( ! config ) return;
portCache.grabPort(port);
var src = buildSrc(data, config);
var domain = window.location.hostname;
if (shouldOpenSmallPopupForUrl(window.location.href)) {
openPopUp(src, port, doneCallback, true);
return;
}
if (xt.options['buffer.op.tpc-disabled'] || xt.options['buffer.op.open-as'] === 'new-tab') {
openPopUp(src, port, doneCallback);
return;
}
var shouldContinue = ensureOnlyOneOverlayOpen(data, closePopup.bind(window, document, doneCallback));
if (!shouldContinue) return;
port.emit('buffer_overlay_open');
// Create the iframe and add the footer:
var iframe = document.createElement('iframe');
iframe.allowtransparency = 'true';
iframe.scrolling = 'no';
iframe.id = 'buffer_overlay';
iframe.name = 'buffer_overlay';
iframe.style.cssText = config.overlay.getCSS();
iframe.src = xt.data.get('data/shared/buffer-frame-container.html');
iframe.addEventListener('load', function() {
iframe.contentWindow.postMessage({
src: src,
css: config.overlay.getCSS(true),
}, '*');
});
var rightCnt = createBtnContainer('right');
var upgradeButton = createUpgradeButton();
var helpButton = createHelpButton();
var dashboardButton = createDashboardButton();
rightCnt.appendChild(upgradeButton);
rightCnt.appendChild(helpButton);
rightCnt.appendChild(dashboardButton);
getExtensionUserData(function(userData) {
upgradeButton.classList.toggle('hidden', !userData.shouldDisplayAwesomeCTA);
// That feature has evolved a bit since it was first named: it's not about
// hiding the button anymore, but redirecting to the FAQ instead of opening
// up a contact window.
if (!userData.shouldDisplayHelpButton) {
helpButton.setAttribute('href', helpButton.getAttribute('data-faq-href'));
}
});
var leftCnt = createBtnContainer('left');
var cancelButton = createCancelButton();
leftCnt.appendChild(cancelButton);
document.body.appendChild(iframe);
document.body.appendChild(rightCnt);
document.body.appendChild(leftCnt);
$(document).on('click', '.buffer-btn-cancel', function() {
closePopup(document, doneCallback);
});
// Bind close listener
// Listen for when the overlay has closed itself
bufferpm.bind('buffermessage', function(overlaydata) {
closePopup(document, doneCallback, overlaydata);
});
/**
* Listen to ESC key and close the popup when hit.
*/
$(document).on('keyup.bufferOverlay', function(e) {
if (e.keyCode == 27) {
// When an overlay instance is hidden (but still open), don't let shortcuts close it
if (!ensureOnlyOneOverlayOpen.isOverlayVisible()) return;
closePopup(document, doneCallback);
}
});
// Remove the loading image when we hear from the other side
bufferpm.bind('buffer_loaded', function(data) {
// Send user data to background script for caching
if (data && data.userData) port.emit('buffer_user_data', data.userData);
bufferpm.unbind('buffer_loaded');
iframe.style.cssText += 'background-image: none !important';
$(".buffer-btn-cancel").remove();
});
};
// Returns true if a new overlay should be open, false if we've toggled the visibility of
// and existing overlay instead.
function ensureOnlyOneOverlayOpen(data, closePopup) {
// State can't be saved in this script, since it gets re-executed multiple times by some browsers
// (e.g. Firefox), so we rely on the DOM instead.
var isOverlayOpen = function() { return !!$('#buffer_overlay').length };
// If the open intent comes from the Buffer toolbar button or a keyboard shortcut, toggle the
// visibility of the overlay if it's already open, otherwise allow a new one to be open.
if (data.placement == 'toolbar' || data.placement == 'hotkey') {
if (!isOverlayOpen()) return true;
$('#buffer_overlay, .buffer-btn-container').toggle();
return false;
}
// If the open intent comes from somewhere else, discard any hidden overlay and open a new one
closePopup();
return true;
};
ensureOnlyOneOverlayOpen.isOverlayVisible = function() { return $('#buffer_overlay').is(':visible') };
function closePopup(document, doneCallback, overlayData) {
$('#buffer_overlay, .buffer-btn-container').remove();
bufferpm.unbind('buffermessage');
bufferpm.unbind('buffer_addbutton');
$(document).off('keyup.bufferOverlay');
setTimeout(function () {
doneCallback(overlayData);
}, 0);
window.focus();
}
// position = 'left' || 'right'
var createBtnContainer = function(position) {
var container = document.createElement('div');
container.setAttribute('class', 'buffer-btn-container buffer-btn-container-' + position);
return container;
};
var createHelpButton = function() {
var contactHref = 'https://buffer.com/app#contact-from-extension';
var faqHref = 'https://support.buffer.com/hc/en-us/articles/360039028273-Installing-and-using-the-Buffer-Browser-Extension';
var button = document.createElement('a');
button.href = contactHref;
button.target = '_blank';
button.setAttribute('data-faq-href', faqHref);
button.setAttribute('class', 'buffer-btn-help');
var icon = document.createElement('span');
icon.classList.add('icon');
var iconUrl = xt.data.get('data/shared/img/[email protected]');
icon.style.cssText = 'background-image: url(' + iconUrl + ') !important;';
button.appendChild(icon);
var text = document.createTextNode('Help');
button.appendChild(text);
button.addEventListener('click', function() {
_bmq.trackAction(['overlay', 'help_button'], {
button_action: button.href === contactHref ? 'contact' : 'faq',
});
}, false);
return button;
};
var createDashboardButton = function() {
var button = document.createElement('a');
button.href = 'https://buffer.com/app';
button.target = '_blank';
button.setAttribute('class', 'buffer-btn-dashboard');
var text = document.createTextNode('Go to Buffer');
button.appendChild(text);
button.addEventListener('click', function() {
_bmq.trackAction(['overlay', 'go_to_buffer_button']);
}, false);
return button;
};
var createCancelButton = function() {
var button = document.createElement('button');
button.setAttribute('class', 'buffer-btn-cancel');
var text = document.createTextNode('Cancel');
button.appendChild(text);
return button;
};
var createUpgradeButton = function() {
var button = document.createElement('a');
button.href = 'https://buffer.com/awesome?utm_campaign=extensions_header&utm_medium=web';
button.target = '_blank';
button.setAttribute('class', 'buffer-btn-upgrade hidden');
var icon = document.createElement('span');
icon.classList.add('icon');
var iconUrl = xt.data.get('data/shared/img/[email protected]');
icon.style.cssText = 'background-image: url(' + iconUrl + ') !important;';
button.appendChild(icon);
var text = document.createTextNode('Upgrade to Awesome');
button.appendChild(text);
button.addEventListener('click', function() {
_bmq.trackAction(['overlay', 'upgrade_to_awesome_button']);
}, false);
return button;
};
// getOverlayConfig returns the configuration object for use by bufferOverlay
var getOverlayConfig = function(postData){
var config = {};
// Set this to true for using a local server while testing
config.local = false;
var segments = window.location.pathname.split('/');
config.pocketWeb = ( window.location.host.indexOf('getpocket') !== -1 && segments[2] === 'read' );
config.isYoutubeVideo = ( window.location.host.indexOf('.youtube.com') !== -1 && segments[1] === 'watch' );
config.isImgur = ( window.location.host.indexOf('imgur.com') !== -1 && segments[1] === 'gallery' );
// 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) {
var selectedText = document.getSelection().toString();
var quoteChars;
if (selectedText) {
// If quotes surround the selected text, strip them away
quoteChars = ['"', '“', '”', '\'', '‘', '’', '«', '»'];
if (quoteChars.indexOf(selectedText[0]) != -1 && quoteChars.indexOf(selectedText[selectedText.length - 1]) != -1) {
selectedText = selectedText.slice(1, selectedText.length - 1);
}
return cb('“' + selectedText + '”');
}
if (config.pocketWeb){
var headline = document.querySelectorAll('.reader_head h1')[0];
var title = headline && headline.textContent;
return cb(title);
}
// In some situations (e.g. YT's autoplay feature), the page is updated via xhr, the
// title attribute too, but not the og:title meta tag: default to retrieving the title
// of the page from the title attr for YouTube videos, and format it a bit
if (config.isYoutubeVideo) {
var title = document.title.replace('- YouTube', '');
return cb(title);
}
// Similarly to YouTube above, in some situations (e.g. navigating from one picture to
// another), the page is updated via xhr, the title attribute too, but not the
// og:title meta tag: let's default to retrieving the title of the page from the title attr
// for YouTube videos, and format it a bit
if (config.isImgur) {
var title = document.title.replace('- Imgur', '');
return cb(title);
}
var ogTitle = document.head && document.head.querySelector('meta[property="og:title"]');
if (ogTitle && ogTitle.content && ogTitle.content.length) {
return cb(ogTitle.content);
}
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: "retweet_comment",
get: function (cb) {
cb(postData.retweet_comment);
},
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: "disable-telemetry",
get: function (cb) {
cb(postData['disable-telemetry']);
},
encode: function (val) {
return encodeURIComponent(val);
}
}
];
var loadingImg = xt.data.get('data/shared/img/black-loading-gif-small.gif');
config.overlay = {
endpoint: "https://buffer.com/add/",
localendpoint: "https://local.buffer.com/add/",
getCSS: function (forDeepestFrame) {
return [
'border: none !important;',
'height: 100% !important;',
'width: 100% !important;',
forDeepestFrame ? '' : 'position: fixed !important;',
'z-index: 999999999999 !important;',
'top:0 !important;',
'left:0 !important;',
'display: block !important;',
'max-width: 100% !important;',
'max-height: 100% !important;',
'padding: 0 !important;',
forDeepestFrame ? '' : 'background: rgba(245, 245, 245, 0.74) url(' + loadingImg +') no-repeat center center !important;',
forDeepestFrame ? '' : 'background-size: 40px !important;'
].join('');
}
};
return config;
};
// Method for handling the async firing of the cb
var executeAfter = function(done, count, args, cb) {
if(done === count) {
setTimeout(function(){
cb.apply( null, args );
}, 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 async down the line, it's there.
var getData = function (postData, cb) {
var config = getOverlayConfig( postData );
var count = config.attributes.length;
var done = 0;
var data = {};
config.attributes.forEach(function(attr, i){
attr.get(function(d){
done += 1;
data[ attr.name ] = d;
executeAfter(done, count, [ data, config ], cb);
});
});
};
// 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;
// Transform the data somewhat, and then create an overlay.
// When it's done, fire buffer_done back to the extension
var createOverlay = function (data, config) {
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, port, 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(postData, createOverlay);
};
// Cache for the port to avoid passing it around in function calls
var portCache = (function() {
var _port;
var exposed = {
// Executed at the very beginning of bufferOverlay()'s execution to
// update the cached port
grabPort: function(port) {
_port = port;
},
getPort: function() { return _port }
};
return exposed;
})();
// _bmq exposes the same API here as it does in background scripts, but
// here it only takes care of passing this data to the background script's
// _bmq where it will be effectively taken care of.
var _bmq = (function() {
var _availableMethods = ['push', 'trackAction'];
var _passForward = function(methodName) {
var payload = {
methodName: methodName,
args: Array.prototype.slice.call(arguments, 1)
};
portCache.getPort().emit('buffer_tracking', payload);
};
var exposed = {};
// Expose all _availableMethods
_availableMethods.forEach(function(methodName) {
exposed[methodName] = _passForward.bind(null, methodName);
});
return exposed;
}());
// Get some user data asynchronously; the callback will be run once immediately if such data has already
// been cached by the background script, and once shortly after displaying the overlay when the cache
// has been refreshed from the user data in the iframe
getExtensionUserData = function(cb) {
portCache.getPort().on('buffer_user_data', cb);
};
// On buffer.com/add, listen to messages coming from the page straight away: don't
// wait for the overlay being open, since we're already on the share dialog's new tab.
var isBufferShareTab = (
document.location.href.indexOf('https://buffer.com/add') === 0 ||
document.location.href.indexOf('https://local.buffer.com/add') === 0
);
if (isBufferShareTab) {
bufferpm.bind('buffermessage', function() {
// Close popup from privileged code
if (window.chrome) chrome.runtime.sendMessage({ type: 'buffer_close_popup' });
});
}