Download the latest version now:
Version 1.0.1 - MIT licensed
A nice way to let visitors of your website know about your twitter feed, is to show your latest tweet.
Twitter already offers two options to show your tweets on your website:
They are fully supported by Twitter and might be the best solution for you.
Disadvantages are:
TwitterFetch is a JavaScript that can be included on your webpage. It will fetch the HTML that’s meant for the widget, extracts all tweets and makes them available in JavaScript, ready to do what you want.
The script provides some options: you can prevent loading images that you won’t show, prevent making the text HTML-save and making links and usernames into hyperlinks, so you can click them.
Since this script is using the Twitter Widget in a way that’s not supported by Twitter, there’s no guarantee that the script will still work tomorrow.
Here’s some example code:
twitterFetch.fetch('345615146724495360', {}, function (tweets) { if (!tweets || tweets.length == 0) { return; } document.getElementById('tweets').innerHTML = tweets[0]; });
But don’t use the widget id 345615146724495360, that belongs to my own @edwinmdev Twitter account.
To get your own widget id, create a Twitter widget and look at the generated code. You’ll find data-widget-id=”…” with the number you can use.
The twitterFetch fetch function takes three parameters:
twitterFetch.fetch(widgetId, options, callbackFunction);
The widget id you get when creating a widget, see above.
An object literal with several options:
images (boolean): when false, prevents loading images that are not needed (default: false).
plaintext (boolean): when false, encodes some entities to make the text safe for HTML (default: false).
hyperlinks (boolean): when true, turns urls and Twitter @usernames into hyperlinks (default: true).
Example:
{ images: false, plaintext: true }
The function that is called back with one argument containing an array with tweets.
You can fetch twitterFetch from GitHub, or you can install twitterfetch from npm:
npm install twitterfetch
twitterFetch is copyright 2016 Edwin Martin and released under the MIT license.