Tuesday, 29 April 2014

Fetching Tweets Feed using javascript


Fetching Twetter's Tweets using javascript:


Script Part:

<script>
            /*********************************************************************
            #### Twitter Post Fetcher! ####            ********************************************************************/
var twitterFetcher = function()
{
var d = null; return { fetch: function(a, b) { d = b; var c = document.createElement("script"); c.type = "text/javascript"; c.src = "http://cdn.syndication.twimg.com/widgets/timelines/" + a + "?&lang=en&callback=twitterFetcher.callback&suppress_response_codes=true&rnd=" + Math.random(); document.getElementsByTagName("head")[0].appendChild(c) }, callback: function(a) { var b = document.createElement("div"); b.innerHTML = a.body; a = b.getElementsByClassName("e-entry-title"); d(a) } } } ();

/*
* ### HOW TO USE: ###
* Create an ID:
* Go to www.twitter.com and sign in as normal, go to your settings page.
* Go to "Widgets" on the left hand side.
* Create a new widget for "user timeline". Feel free to check "exclude replies"
* if you dont want replies in results.
* Now go back to settings page, and then go back to widgets page, you should
* see the widget you just created. Click edit.
* Now look at the URL in your web browser, you will see a long number like this:
* 123456789012345678
* Use this as your ID below instead!
*/

twitterFetcher.fetch('123456789012345678', function(tweets) {
// Do what you want with your tweets here! For example:
var x = tweets.length;
var n = 0;
var element = document.getElementById('tweets');

if (x > 0) {
var html = '<ul class="twitter-widget">';
    var tweetscount = 2;
while (n < tweetscount)
    {
if (tweets[n].innerHTML) {
html += '<li><a href="#">@DiveRAID</a> ' + tweets[n].innerHTML + '</li>';
         }
else {
html += '<li><a href="#">@DiveRAID</a> ' + tweets[n].textContent + '</li>';
         }
         n++;
}                   
    html += '</ul>';
}
element.innerHTML = html;
});       
</script>
Body Part:

<div id="tweets"></div>



2 comments: