twitter-unliker

Delete your Twitter likes older than 90 days
git clone https://src.jayvii.de/pub/twitter-unliker.git
Log | Files | Refs

twitter_unliker.js (2750B)


      1 function isinvp (el) {
      2     var rect = el.getBoundingClientRect();
      3     return (
      4         rect.top >= 0 &&
      5         rect.left >= 0 &&
      6         rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
      7         rect.right <= (window.innerWidth || document.documentElement.clientWidth)
      8     );
      9 }
     10 
     11 function check_and_unlike(tweet) {
     12     
     13     //tweet_in_vp = isinvp(tweet);
     14     
     15     //if (tweet_in_vp) {
     16         
     17         // Get time of tweet
     18         try{ twtime = tweet.querySelector("time").dateTime; }
     19         catch { twtime = new Date(); twtime = twtime.getDate(); }
     20         // find a-parent which has tweet url
     21 
     22         // Get timestamp 90 days ago
     23         t90d = new Date();
     24         t90d.setDate(t90d.getDate() - 90);
     25         
     26         // unlike only if tweet is older than 90 days
     27         if ((Date.parse(t90d) - Date.parse(twtime)) > 0) {
     28             
     29             // get ID of Tweet (href used as ID)
     30             tw_id = tweet.querySelector("time").parentNode.getAttribute("href");
     31             
     32             if (global_tweet_register.indexOf(tw_id) < 0) {
     33             
     34                 // add Tweet ID to register
     35                 global_tweet_register.push(tw_id);
     36             
     37                 // trigger like button
     38                 // (older liked tweets are not marked as "liked" anymore)
     39                 try {tw_lkb = tweet.querySelector("div[data-testid=like]"); }
     40                 finally { if (tw_lkb != null) tw_lkb.click(); }
     41 
     42                 // trigger unlike button after short wait
     43                 setTimeout(function() {
     44                     try { tw_ukb = tweet.querySelector("div[data-testid=unlike]"); }
     45                     finally { if (tw_ukb != null) tw_ukb.click(); }
     46                 }, 1500);
     47                 
     48                 // print output
     49                 console.log("unliked: " + tw_id + " @ " + twtime);
     50                 global_like_deleted = true;
     51             }
     52         }
     53     //}
     54 }
     55 
     56 function get_tweets() {
     57     // find all tweets in the current view / scroll section
     58     tweets = document.querySelectorAll("article");
     59     // run the check-and-unlike procedure on all of them individually
     60     for (let i = 0; i < tweets.length; i++) {
     61         setTimeout(function() { check_and_unlike(tweets[i]); }, 250);
     62     }
     63 }
     64 
     65 function daemon() {
     66     // run procedure
     67     get_tweets();
     68     if (global_like_deleted) {
     69         timeout = (1000 * 60 * 5); // 5 minutes
     70         global_like_deleted = false;
     71     } else {
     72         timeout = 500;
     73     }
     74     // scroll page to the very bottom (load new tweets)
     75     //window.scrollByLines(15);
     76     window.scrollByPages(1);
     77     // after wait, recall yourself!
     78     setTimeout(function() { daemon(); }, timeout);
     79 }
     80 
     81 let global_tweet_register = [];
     82 var global_like_deleted = false;
     83 daemon();