Menü schliessen
Created: May 28th 2015
Last updated: May 1st 2020
Categories: IT Development
Author: Marcus Fleuti

Twitter Post Fetcher!

Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Someone wrote a "clever" snippet in vanilla javascript that fetches twitter widgets. Not the cleanest way and maybe not future-proof, but for now the only solution to access twitter data without OAuth and server-side support.

#### 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:

You gonna need ID for this, follow next steps to get you 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". Check "exclude replies" if you dont want replies in results.
- Go back to settings page, and then 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: 345735908357048478
- Use this as your ID below instead!

twitterFetcher.fetch('YOUR ID', function(tweets){
      // Do what you want with your tweets here! For example:
      var x = tweets.length;
      var n = 0;
      var element = document.getElementById('tweets');
      var html = '<ul>';
      while(n < x) {
        if (tweets[n].innerText) {
          html += '<li>' + tweets[n].innerText + '</li>';
        } else {
          html += '<li>' + tweets[n].textContent + '</li>';
        }
        n++;
      }
      html += '</ul>';
      element.innerHTML = html;
});