-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher.js
482 lines (453 loc) · 18.8 KB
/
fetcher.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
/*********************************************************************
* #### Twitter Post Fetcher v18.0.3 ####
* Coded by Jason Mayes 2015. A present to all the developers out there.
* www.jasonmayes.com
* Please keep this disclaimer with my code if you use it. Thanks. :-)
* Got feedback or questions, ask here:
* http://www.jasonmayes.com/projects/twitterApi/
* Github: https://github.com/jasonmayes/Twitter-Post-Fetcher
* Updates will be posted to this site.
*********************************************************************/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals.
factory();
}
}(this, function() {
var domNode = '';
var maxTweets = 20;
var parseLinks = true;
var queue = [];
var inProgress = false;
var printTime = true;
var printUser = true;
var formatterFunction = null;
var supportsClassName = true;
var showRts = true;
var customCallbackFunction = null;
var showInteractionLinks = true;
var showImages = false;
var useEmoji = false;
var targetBlank = true;
var lang = 'en';
var permalinks = true;
var dataOnly = false;
var script = null;
var scriptAdded = false;
function handleTweets(tweets){
if (customCallbackFunction === null) {
var x = tweets.length;
var n = 0;
var element = document.getElementById(domNode);
var html = '<ul>';
while(n < x) {
html += '<li>' + tweets[n] + '</li>';
n++;
}
html += '</ul>';
console.log(domNode)
element.innerHTML = html;
} else {
customCallbackFunction(tweets);
}
}
function strip(data) {
return data.replace(/<b[^>]*>(.*?)<\/b>/gi, function(a,s){return s;})
.replace(/class="(?!(tco-hidden|tco-display|tco-ellipsis))+.*?"|data-query-source=".*?"|dir=".*?"|rel=".*?"/gi,
'');
}
function targetLinksToNewWindow(el) {
var links = el.getElementsByTagName('a');
for (var i = links.length - 1; i >= 0; i--) {
links[i].setAttribute('target', '_blank');
links[i].setAttribute('rel', 'noopener');
}
}
function getElementsByClassName (node, classname) {
var a = [];
var regex = new RegExp('(^| )' + classname + '( |$)');
var elems = node.getElementsByTagName('*');
for (var i = 0, j = elems.length; i < j; i++) {
if(regex.test(elems[i].className)){
a.push(elems[i]);
}
}
return a;
}
function extractImagesUrl(image_data) {
if (image_data !== undefined && image_data.innerHTML.indexOf('data-image') >= 0) {
var data_src = image_data.innerHTML.match(/data-image=\"([A-z0-9]+:\/\/[A-z0-9]+\.[A-z0-9]+\.[A-z0-9]+\/[A-z0-9]+\/[A-z0-9\-]+)\"/ig);
for (var i = 0; i < data_src.length; i++) {
data_src[i] = data_src[i].match(/data-image=\"([A-z0-9]+:\/\/[A-z0-9]+\.[A-z0-9]+\.[A-z0-9]+\/[A-z0-9]+\/[A-z0-9\-]+)\"/i)[1];
data_src[i] = decodeURIComponent(data_src[i]) + '.jpg';
}
return data_src;
}
}
var twitterFetcher = {
fetch: function(config) {
if (config.maxTweets === undefined) {
config.maxTweets = 20;
}
if (config.enableLinks === undefined) {
config.enableLinks = true;
}
if (config.showUser === undefined) {
config.showUser = true;
}
if (config.showTime === undefined) {
config.showTime = true;
}
if (config.dateFunction === undefined) {
config.dateFunction = 'default';
}
if (config.showRetweet === undefined) {
config.showRetweet = true;
}
if (config.customCallback === undefined) {
config.customCallback = null;
}
if (config.showInteraction === undefined) {
config.showInteraction = true;
}
if (config.showImages === undefined) {
config.showImages = false;
}
if (config.useEmoji === undefined) {
config.useEmoji = false;
}
if (config.linksInNewWindow === undefined) {
config.linksInNewWindow = true;
}
if (config.showPermalinks === undefined) {
config.showPermalinks = true;
}
if (config.dataOnly === undefined) {
config.dataOnly = false;
}
if (inProgress) {
queue.push(config);
} else {
inProgress = true;
domNode = config.domId;
maxTweets = config.maxTweets;
parseLinks = config.enableLinks;
printUser = config.showUser;
printTime = config.showTime;
showRts = config.showRetweet;
formatterFunction = config.dateFunction;
customCallbackFunction = config.customCallback;
showInteractionLinks = config.showInteraction;
showImages = config.showImages;
useEmoji = config.useEmoji;
targetBlank = config.linksInNewWindow;
permalinks = config.showPermalinks;
dataOnly = config.dataOnly;
var head = document.getElementsByTagName('head')[0];
if (script !== null) {
head.removeChild(script);
}
script = document.createElement('script');
script.type = 'text/javascript';
if (config.list !== undefined) {
script.src = 'https://syndication.twitter.com/timeline/list?' +
'callback=__twttrf.callback&dnt=false&list_slug=' +
config.list.listSlug + '&screen_name=' + config.list.screenName +
'&suppress_response_codes=true&lang=' + (config.lang || lang) +
'&rnd=' + Math.random();
} else if (config.profile !== undefined) {
script.src = 'https://syndication.twitter.com/timeline/profile?' +
'callback=__twttrf.callback&dnt=false' +
'&screen_name=' + config.profile.screenName +
'&suppress_response_codes=true&lang=' + (config.lang || lang) +
'&rnd=' + Math.random();
} else if (config.likes !== undefined) {
script.src = 'https://syndication.twitter.com/timeline/likes?' +
'callback=__twttrf.callback&dnt=false' +
'&screen_name=' + config.likes.screenName +
'&suppress_response_codes=true&lang=' + (config.lang || lang) +
'&rnd=' + Math.random();
} else if (config.collection !== undefined) {
script.src = 'https://syndication.twitter.com/timeline/collection?' +
'callback=__twttrf.callback&dnt=false' +
'&collection_id=' + config.collection.collectionId +
'&suppress_response_codes=true&lang=' + (config.lang || lang) +
'&rnd=' + Math.random();
} else {
script.src = 'https://cdn.syndication.twimg.com/widgets/timelines/' +
config.id + '?&lang=' + (config.lang || lang) +
'&callback=__twttrf.callback&' +
'suppress_response_codes=true&rnd=' + Math.random();
}
head.appendChild(script);
}
},
callback: function(data) {
if (data === undefined || data.body === undefined) {
inProgress = false;
if (queue.length > 0) {
twitterFetcher.fetch(queue[0]);
queue.splice(0,1);
}
return;
}
// Remove emoji and summary card images.
if(!useEmoji){
data.body = data.body.replace(/(<img[^c]*class="Emoji[^>]*>)|(<img[^c]*class="u-block[^>]*>)/g, '');
}
// Remove display images.
if (!showImages) {
data.body = data.body.replace(/(<img[^c]*class="NaturalImage-image[^>]*>|(<img[^c]*class="CroppedImage-image[^>]*>))/g, '');
}
// Remove avatar images.
if (!printUser) {
data.body = data.body.replace(/(<img[^c]*class="Avatar"[^>]*>)/g, '');
}
var div = document.createElement('div');
div.innerHTML = data.body;
if (typeof(div.getElementsByClassName) === 'undefined') {
supportsClassName = false;
}
function swapDataSrc(element) {
var avatarImg = element.getElementsByTagName('img')[0];
if (avatarImg) {
avatarImg.src = avatarImg.getAttribute('data-src-2x');
} else {
var screenName = element.getElementsByTagName('a')[0]
.getAttribute('href').split('twitter.com/')[1];
var img = document.createElement('img');
img.setAttribute('src', 'https://twitter.com/' + screenName +
'/profile_image?size=bigger');
element.prepend(img);
}
return element;
}
var tweets = [];
var authors = [];
var times = [];
var images = [];
var rts = [];
var tids = [];
var permalinksURL = [];
var x = 0;
if (supportsClassName) {
var tmp = div.getElementsByClassName('timeline-Tweet');
while (x < tmp.length) {
if (tmp[x].getElementsByClassName('timeline-Tweet-retweetCredit').length > 0) {
rts.push(true);
} else {
rts.push(false);
}
if (!rts[x] || rts[x] && showRts) {
tweets.push(tmp[x].getElementsByClassName('timeline-Tweet-text')[0]);
tids.push(tmp[x].getAttribute('data-tweet-id'));
if (printUser) {
authors.push(swapDataSrc(tmp[x].getElementsByClassName('timeline-Tweet-author')[0]));
}
times.push(tmp[x].getElementsByClassName('dt-updated')[0]);
permalinksURL.push(tmp[x].getElementsByClassName('timeline-Tweet-timestamp')[0]);
if (tmp[x].getElementsByClassName('timeline-Tweet-media')[0] !==
undefined) {
images.push(tmp[x].getElementsByClassName('timeline-Tweet-media')[0]);
} else {
images.push(undefined);
}
}
x++;
}
} else {
var tmp = getElementsByClassName(div, 'timeline-Tweet');
while (x < tmp.length) {
if (getElementsByClassName(tmp[x], 'timeline-Tweet-retweetCredit').length > 0) {
rts.push(true);
} else {
rts.push(false);
}
if (!rts[x] || rts[x] && showRts) {
tweets.push(getElementsByClassName(tmp[x], 'timeline-Tweet-text')[0]);
tids.push(tmp[x].getAttribute('data-tweet-id'));
if (printUser) {
authors.push(swapDataSrc(getElementsByClassName(tmp[x],'timeline-Tweet-author')[0]));
}
times.push(getElementsByClassName(tmp[x], 'dt-updated')[0]);
permalinksURL.push(getElementsByClassName(tmp[x], 'timeline-Tweet-timestamp')[0]);
if (getElementsByClassName(tmp[x], 'timeline-Tweet-media')[0] !== undefined) {
images.push(getElementsByClassName(tmp[x], 'timeline-Tweet-media')[0]);
} else {
images.push(undefined);
}
}
x++;
}
}
if (tweets.length > maxTweets) {
tweets.splice(maxTweets, (tweets.length - maxTweets));
authors.splice(maxTweets, (authors.length - maxTweets));
times.splice(maxTweets, (times.length - maxTweets));
rts.splice(maxTweets, (rts.length - maxTweets));
images.splice(maxTweets, (images.length - maxTweets));
permalinksURL.splice(maxTweets, (permalinksURL.length - maxTweets));
}
var arrayTweets = [];
var x = tweets.length;
var n = 0;
if (dataOnly) {
while (n < x) {
arrayTweets.push({
tweet: tweets[n].innerHTML,
tweetText: tweets[n].textContent,
author: authors[n] ? authors[n].innerHTML : 'Unknown Author',
author_data: {
profile_url: authors[n] ? authors[n].querySelector('[data-scribe="element:user_link"]').href : null,
profile_image: authors[n] ?
'https://twitter.com/' + authors[n].querySelector('[data-scribe="element:screen_name"]').title.split('@')[1] + '/profile_image?size=bigger' : null,
profile_image_2x: authors[n] ? 'https://twitter.com/' + authors[n].querySelector('[data-scribe="element:screen_name"]').title.split('@')[1] + '/profile_image?size=original' : null,
screen_name: authors[n] ? authors[n].querySelector('[data-scribe="element:screen_name"]').title : null,
name: authors[n] ? authors[n].querySelector('[data-scribe="element:name"]').title : null
},
time: times[n].textContent,
timestamp: times[n].getAttribute('datetime').replace('+0000', 'Z').replace(/([\+\-])(\d\d)(\d\d)/, '$1$2:$3'),
image: (extractImagesUrl(images[n]) ? extractImagesUrl(images[n])[0] : undefined),
images: extractImagesUrl(images[n]),
rt: rts[n],
tid: tids[n],
permalinkURL: (permalinksURL[n] === undefined) ?
'' : permalinksURL[n].href
});
n++;
}
} else {
while (n < x) {
if (typeof(formatterFunction) !== 'string') {
var datetimeText = times[n].getAttribute('datetime');
var newDate = new Date(times[n].getAttribute('datetime')
.replace(/-/g,'/').replace('T', ' ').split('+')[0]);
var dateString = formatterFunction(newDate, datetimeText);
times[n].setAttribute('aria-label', dateString);
if (tweets[n].textContent) {
// IE hack.
if (supportsClassName) {
times[n].textContent = dateString;
} else {
var h = document.createElement('p');
var t = document.createTextNode(dateString);
h.appendChild(t);
h.setAttribute('aria-label', dateString);
times[n] = h;
}
} else {
times[n].textContent = dateString;
}
}
var op = '';
if (parseLinks) {
if (targetBlank) {
targetLinksToNewWindow(tweets[n]);
if (printUser) {
targetLinksToNewWindow(authors[n]);
}
}
if (printUser) {
op += '<div class="user">' + strip(authors[n].innerHTML) +
'</div>';
}
op += '<p class="tweet">' + strip(tweets[n].innerHTML) + '</p>';
if (printTime) {
if (permalinks) {
op += '<p class="timePosted"><a href="' + permalinksURL[n] +
'">' + times[n].getAttribute('aria-label') + '</a></p>';
} else {
op += '<p class="timePosted">' +
times[n].getAttribute('aria-label') + '</p>';
}
}
} else {
if (tweets[n].textContent) {
if (printUser) {
op += '<p class="user">' + authors[n].textContent + '</p>';
}
op += '<p class="tweet">' + tweets[n].textContent + '</p>';
if (printTime) {
op += '<p class="timePosted">' + times[n].textContent + '</p>';
}
} else {
if (printUser) {
op += '<p class="user">' + authors[n].textContent + '</p>';
}
op += '<p class="tweet">' + tweets[n].textContent + '</p>';
if (printTime) {
op += '<p class="timePosted">' + times[n].textContent + '</p>';
}
}
}
if (showInteractionLinks) {
op += '<p class="interact"><a href="https://twitter.com/intent/' +
'tweet?in_reply_to=' + tids[n] +
'" class="twitter_reply_icon"' +
(targetBlank ? ' target="_blank" rel="noopener">' : '>') +
'Reply</a><a href="https://twitter.com/intent/retweet?' +
'tweet_id=' + tids[n] + '" class="twitter_retweet_icon"' +
(targetBlank ? ' target="_blank" rel="noopener">' : '>') + 'Retweet</a>' +
'<a href="https://twitter.com/intent/favorite?tweet_id=' +
tids[n] + '" class="twitter_fav_icon"' +
(targetBlank ? ' target="_blank" rel="noopener">' : '>') + 'Favorite</a></p>';
}
if (showImages && images[n] !== undefined && extractImagesUrl(images[n]) !== undefined) {
var extractedImages = extractImagesUrl(images[n]);
for (var i = 0; i < extractedImages.length; i++) {
op += '<div class="media">' +
'<img src="' + extractedImages[i] +
'" alt="Image from tweet" />' + '</div>';
}
}
if (showImages) {
arrayTweets.push(op);
} else if (!showImages && tweets[n].textContent.length) {
arrayTweets.push(op);
}
n++;
}
}
handleTweets(arrayTweets);
inProgress = false;
if (queue.length > 0) {
twitterFetcher.fetch(queue[0]);
queue.splice(0,1);
}
}
};
// It must be a global variable because it will be called by JSONP.
window.__twttrf = twitterFetcher;
window.twitterFetcher = twitterFetcher;
return twitterFetcher;
}));
// Prepend polyfill for IE/Edge.
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('prepend')) {
return;
}
Object.defineProperty(item, 'prepend', {
configurable: true,
enumerable: true,
writable: true,
value: function prepend() {
var argArr = Array.prototype.slice.call(arguments),
docFrag = document.createDocumentFragment();
argArr.forEach(function (argItem) {
var isNode = argItem instanceof Node;
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
});
this.insertBefore(docFrag, this.firstChild);
}
});
});
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);