1/********************************************************************* 2* #### Twitter Post Fetcher v18.0.3 #### 3* Coded by Jason Mayes 2015. A present to all the developers out there. 4* www.jasonmayes.com 5* Please keep this disclaimer with my code if you use it. Thanks. :-) 6* Got feedback or questions, ask here: 7* http://www.jasonmayes.com/projects/twitterApi/ 8* Github: https://github.com/jasonmayes/Twitter-Post-Fetcher 9* Updates will be posted to this site. 10*********************************************************************/ 11(function (root, factory) { if (typeof define === 'function' && define.amd) { define([], factory); } else if (typeof exports === 'object') { module.exports = factory(); } else { factory(); } }(this, function () { 12 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) { 13 if (customCallbackFunction === null) { 14 var x = tweets.length; var n = 0; var element = document.getElementById(domNode); var html = '<ul>'; while (n < x) { html += '<li>' + tweets[n] + '</li>'; n++; } 15 html += '</ul>'; element.innerHTML = html; 16 } else { customCallbackFunction(tweets); } 17 } 18 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, ''); } 19 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'); } } 20 function getElementsByClassName(node, classname) { 21 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]); } } 22 return a; 23 } 24 function extractImagesUrl(image_data) { 25 if (image_data !== undefined && image_data.innerHTML.indexOf('data-image') >= 0) { 26 var data_src = image_data.innerHTML.match(/data-image=\"([^"]+)\"/ig); for (var i = 0; i < data_src.length; i++) { data_src[i] = data_src[i].match(/data-image=\"([^"]+)\"/i)[1]; data_src[i] = decodeURIComponent(data_src[i]) + '.jpg'; } 27 return data_src; 28 } 29 } 30 var twitterFetcher = { 31 fetch: function (config) { 32 if (config.maxTweets === undefined) { config.maxTweets = 20; } 33 if (config.enableLinks === undefined) { config.enableLinks = true; } 34 if (config.showUser === undefined) { config.showUser = true; } 35 if (config.showTime === undefined) { config.showTime = true; } 36 if (config.dateFunction === undefined) { config.dateFunction = 'default'; } 37 if (config.showRetweet === undefined) { config.showRetweet = true; } 38 if (config.customCallback === undefined) { config.customCallback = null; } 39 if (config.showInteraction === undefined) { config.showInteraction = true; } 40 if (config.showImages === undefined) { config.showImages = false; } 41 if (config.useEmoji === undefined) { config.useEmoji = false; } 42 if (config.linksInNewWindow === undefined) { config.linksInNewWindow = true; } 43 if (config.showPermalinks === undefined) { config.showPermalinks = true; } 44 if (config.dataOnly === undefined) { config.dataOnly = false; } 45 if (inProgress) { queue.push(config); } else { 46 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); } 47 script = document.createElement('script'); script.type = 'text/javascript'; if (config.list !== undefined) { 48 script.src = 'https://syndication.twitter.com/timeline/list?' + 'callback=__twttrf.callback&dnt=false&list_slug=' + 49 config.list.listSlug + '&screen_name=' + config.list.screenName + '&suppress_response_codes=true&lang=' + (config.lang || lang) + '&rnd=' + Math.random(); 50 } 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 { 51 script.src = 'https://cdn.syndication.twimg.com/widgets/timelines/' + 52 config.id + '?&lang=' + (config.lang || lang) + '&callback=__twttrf.callback&' + 'suppress_response_codes=true&rnd=' + Math.random(); 53 } 54 head.appendChild(script); 55 } 56 }, callback: function (data) { 57 if (data === undefined || data.body === undefined) { 58 inProgress = false; if (queue.length > 0) { twitterFetcher.fetch(queue[0]); queue.splice(0, 1); } 59 return; 60 } 61 if (!useEmoji) { data.body = data.body.replace(/(<img[^c]*class="Emoji[^>]*>)|(<img[^c]*class="u-block[^>
61]*>)/g, ''); } 62 if (!showImages) { data.body = data.body.replace(/(<img[^c]*class="NaturalImage-image[^>]*>|(<img[^c]*class="CroppedImage-image[^>]*>))/g, ''); } 63 if (!printUser) { data.body = data.body.replace(/(<img[^c]*class="Avatar"[^>]*>)/g, ''); } 64 var div = document.createElement('div'); div.innerHTML = data.body; if (typeof (div.getElementsByClassName) === 'undefined') { supportsClassName = false; } 65 function swapDataSrc(element) { 66 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); } 67 return element; 68 } 69 var tweets = []; var authors = []; var times = []; var images = []; var rts = []; var tids = []; var permalinksURL = []; var x = 0; if (supportsClassName) { 70 var tmp = div.getElementsByClassName('timeline-Tweet'); while (x < tmp.length) { 71 if (tmp[x].getElementsByClassName('timeline-Tweet-retweetCredit').length > 0) { rts.push(true); } else { rts.push(false); } 72 if (!rts[x] || rts[x] && showRts) { 73 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])); } 74 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); } 75 } 76 x++; 77 } 78 } else { 79 var tmp = getElementsByClassName(div, 'timeline-Tweet'); while (x < tmp.length) { 80 if (getElementsByClassName(tmp[x], 'timeline-Tweet-retweetCredit').length > 0) { rts.push(true); } else { rts.push(false); } 81 if (!rts[x] || rts[x] && showRts) { 82 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])); } 83 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); } 84 } 85 x++; 86 } 87 } 88 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)); } 89 var arrayTweets = []; var x = tweets.length; var n = 0; if (dataOnly) { while (n < x) { arrayTweets.push({ tweet: tweets[n].innerHTML, 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 { 90 while (n < x) { 91 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) { 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; } } 92 var op = ''; if (parseLinks) { 93 if (targetBlank) { targetLinksToNewWindow(tweets[n]); if (printUser) { targetLinksToNewWindow(authors[n]); } } 94 if (printUser) { op += '<div class="user">' + strip(authors[n].innerHTML) + '</div>'; } 95 op += '<p class="tweet">' + strip(tweets[n].innerHTML) + '</p>'; if (printTime) { 96 if (permalinks) { op += '<p class="timePosted"><a href="' + permalinksURL[n] + '">' + times[n].getAttribute('aria-label') + '</a></p>'; } else { 97 op += '<p class="timePosted">' + 98 times[n].getAttribute('aria-label') + '</p>'; 99 } 100 } 101 } else { 102 if (tweets[n].textContent) { 103 if (printUser) { op += '<p class="user">' + authors[n].textContent + '</p>'; } 104 op += '<p class="tweet">' + tweets[n].textContent + '</p>'; if (printTime) { op += '<p class="timePosted">' + times[n].textContent + '</p>'; } 105 } else { 106 if (printUser) { op += '<p class="user">' + authors[n].textContent + '</p>'; } 107 op += '<p class="tweet">' + tweets[n].textContent + '</p>'; if (printTime) { op += '<p class="timePosted">' + times[n].textContent + '</p>'; } 108 } 109 } 110 if (showInteractionLinks) { 111 op += '<p class="interact"><a href="https://twitter.com/intent/' + 'tweet?in_reply_to=' + tids[n] + '" class="twitter_reply_icon"' + 112 (targetBlank ? ' target="_blank" rel="noopener">' : '>') + 'Reply</a><a href="https://twitter.com/intent/retweet?' + 'tweet_id=' + tids[n] + '" class="twitter_retweet_icon"' + 113 (targetBlank ? ' target="_blank" rel="noopener">' : '>') + 'Retweet</a>' + '<a href="https://twitter.com/intent/favorite?tweet_id=' + 114 tids[n] + '" class="twitter_fav_icon"' + 115 (targetBlank ? ' target="_blank" rel="noopener">' : '>') + 'Favorite</a></p>'; 116 } 117 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>'; } } 118 if (showImages) { arrayTweets.push(op); } else if (!showImages && tweets[n].textContent.length) { arrayTweets.push(op); } 119 n++; 120 } 121 } 122 handleTweets(arrayTweets); inProgress = false; if (queue.length > 0) { twitterFetcher.fetch(queue[0]); queue.splice(0, 1); } 123 } 124 }; window.__twttrf = twitterFetcher; window.twitterFetcher = twitterFetcher; return twitterFetcher; 125})); (function (arr) { 126 arr.forEach(function (item) { 127 if (item.hasOwnProperty('prepend')) { return; } 128 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); } }); 129 });
130})([Element.prototype, Document.prototype, DocumentFragment.prototype]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.