This repository was archived by the owner on Apr 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwitterfetch.js
More file actions
44 lines (40 loc) · 1.67 KB
/
twitterfetch.js
File metadata and controls
44 lines (40 loc) · 1.67 KB
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
/**!
@preserve twitterFetch 1.0.1
@copyright 2016 Edwin Martin
@see {@link http://www.bitstorm.org/javascript/}
@license MIT
*/
var twitterFetch = function () {
var _callback, _options;
return {
fetch: function (widgetId, options, callback) {
_options = options;
_callback = callback;
var script = document.createElement("script");
script.src = "https://cdn.syndication.twimg.com/widgets/timelines/" + widgetId
+ "?&lang=en&callback=twitterFetch.callback&suppress_response_codes=true&rnd=" + Math.random();
document.head.appendChild(script);
},
callback: function (feed) {
var div = document.createElement("div");
var html = feed.body;
if (_options.images !== true) {
html = html.replace(/<img/g, '<x-img');
}
div.innerHTML = html;
var tweets = Array.prototype.map.call(div.querySelectorAll(".timeline-Tweet-text"), function(el) {
var txt = el.textContent;
if (_options.plaintext !== true) {
txt = txt.replace('&', '&').replace('<', '<').replace('>', '>');
}
if (_options.hyperlinks !== false) {
txt = txt.replace(/(https?:\/\/[a-zA-Z0-9\/\&\?=\[\]%~#;\$\-_\+!\*',":\.\(\)]+[a-zA-Z0-9\/\&\?=\[\]%~#;\$\-_\+!\*',":]{2})/g,
'<a href=\'$1\'>$1</a>')
.replace(/@([0-9a-zA-Z_-]+)/g, '<a href=\'http://twitter.com/$1\'>@$1</a>');
}
return txt;
});
_callback(tweets)
}
}
}();