Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed a twitter timeline without retweets?

Tags:

I'm embedding a twitter timeline into my website. However my retweets are being displayed there and I don't want them to.

Is there any easy way to turn them off? I can't seem to find any

My search query is from:myaccount. I also tried from:myaccount -RT, with no luck. I can hide retweets with a search widget but tweets older than a week are not shown which is unacceptable in my case.

Hiding the tweets via some CSS magic would also be OK. I managed to find out that manually inserting

div.timeline-Tweet--isRetweet {   display: none !important; } 

in the CSS for the embedded HTML does the trick, but I can't manage to do it programatically.

like image 690
Guido Avatar asked Feb 22 '17 15:02

Guido


People also ask

How do you embed a tweet without original tweet?

Click the icon located within the Tweet. From the menu, select Embed Tweet. This will open publish.twitter.com where you can customize the look of the embedded Tweet by clicking set customization options. If the Tweet is a reply to another Tweet, you can check Hide Conversation to hide the original Tweet.

Can you hide retweets from your timeline?

To do it, go to Twitter.com and click on your profile. Click "Following" to see the complete list of users you're following. Find the user for whom you want to hide retweets and click on the silhouette button located just to the left of the Following button. A drop-down menu will open up; select "Turn off Retweets."


2 Answers

It's quite easy. We will use for example caitp88 account for demo you can follow her afterwards if you want tho)

As you can see in her profile, the latest tweet is a Retweet enter image description here

But we don't want retweets, right?

So we go to search form and type from:caitp88 -RT enter image description here

Then we create an embed from this search enter image description here Widget configuration window opens, you just click on create widget

enter image description here

The widget will not contain any retweets
In action: enter image description here

Don't forget to change caitp88 to your own twitter handle

AND NOTE, THIS IS OFFICIAL, LEGAL SOLUTION

like image 89
Medet Tleukabiluly Avatar answered Sep 19 '22 14:09

Medet Tleukabiluly


Adaptation from my comments

To go with hacky style injection, simply put following code in script tag. This puts https://platform.twitter.com/widgets.js script tag to your page so you no longer need it as separate script tag.

// widgets script loading taken from Twitter window.twttr = (function (d, s, id) {     var js, fjs = d.getElementsByTagName(s)[0],         t = window.twttr || {};     if (d.getElementById(id)) return;     js = d.createElement(s);     js.id = id;     js.src = "https://platform.twitter.com/widgets.js";     fjs.parentNode.insertBefore(js, fjs);      t._e = [];     t.ready = function (f) {         t._e.push(f);     };      return t; }(document, "script", "twitter-wjs"));  twttr.ready(function (twttr) {     twttr.events.bind("rendered", function (event) {         var widgetFrame = event.target;         var retweets = widgetFrame.contentDocument.querySelectorAll('div.timeline-Tweet--isRetweet');         retweets.forEach(function (node) {             if (node.parentNode) { // (in)sanity check                 node.parentNode.style = 'display: none;' // hide entire parent li tag             }         })     }); }); 

Remark about it

We take parent node as every div.timeline-Tweet--isRetweet sits in li tag which gives you border, so you end up with some strange double border left after hidden retweets. I think that there is no way to reach parent through CSS selectors so we have to do this with a code.

Another remark

Following hack strongly relies on way how contents of the timelines are structured and those may change without the notice from Twitter so you risk here that one day you will wake up with you pants down and retweets back in your timeline ;)


Update

ok here is v2 which listen for updates on ol... but after writing it I start to understand dr Frankenstein feelings ;)

window.twttr = (function (d, s, id) {     var js, fjs = d.getElementsByTagName(s)[0],         t = window.twttr || {};     if (d.getElementById(id)) return;     js = d.createElement(s);     js.id = id;     js.src = "https://platform.twitter.com/widgets.js";     fjs.parentNode.insertBefore(js, fjs);      t._e = [];     t.ready = function (f) {         t._e.push(f);     };      return t; }(document, "script", "twitter-wjs"));  twttr.ready(function (twttr) {     twttr.events.bind("rendered", function (event) {         var frameDoc = event.target.contentDocument;          // initially hide retweets         var hideRetweets = function () {             var retweets = frameDoc.querySelectorAll('div.timeline-Tweet--isRetweet');             retweets.forEach(function (node) {                 if (node.parentNode && node.parentNode.style !== 'display: none;') { // (in)sanity check                     node.parentNode.style = 'display: none;' // hide entire parent li tag                 }             });         };          hideRetweets();          // Twitter widget emitts no events on updates so we hook up ourselves to ol element to listen on it for additions of children         var watcher = new MutationObserver(function (mutations) {             // check whether new tweets arrived             var hasNewTweets = mutations.some(function (mutation) {                 return mutation.addedNodes.length > 0;             });             if (hasNewTweets) {                 hideRetweets(); // rescan all tweets as it is easier             }         });         watcher.observe(frameDoc.querySelector('ol.timeline-TweetList'), { childList: true });     }); }); 
like image 33
gaa Avatar answered Sep 18 '22 14:09

gaa