New twitter improvements

Improves new version of twitter a little bit

Ekde 2019/10/11. Vidu La ĝisdata versio.

// ==UserScript==
// @name New twitter improvements
// @description Improves new version of twitter a little bit
// @namespace Violentmonkey Scripts
// @match https://mobile.twitter.com/*
// @match https://twitter.com/*
// @grant none
// @inject-into content
// @license WTFPL
// @version 0.6
// ==/UserScript==
// 
//
// *********************************************************************************
// * TODO: stop reply, retweet and like count from updating, it's very annoying    *
// *                                                                               *
// *********************************************************************************
// 

function insertAfter(newNode, referenceNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

(function() {
  
  var re = /^\/.*\/status\/\d*\/photo\/\d$/;
  var img_re = /\?.*/;
  var img_repl = '.jpg:orig';
  
  //useless shit to remove
  var remove_elements = [
    'a[role="button"][href="/compose/tweet"]',
    'a[role="link"][href="/explore"]',
    //profile will only work on english version
    'a[role="link"][aria-label="Profile"]',
    //ads and analytics are blocked anyway
    'a[role="link"][href*="ads.twitter.com"]',
    'a[role="link"][href*="analytics.twitter.com"]'
  ]
  
  let ob = new window.MutationObserver(function() {
    
    //remove useless elements
    remove_elements.forEach(function(sel) {
      let el = document.querySelector(sel);
      if (el) {
        el.remove();
      }
    })
    
    //trends are more complicated
    if (!window.location.pathname.startsWith("/settings")) {
      let sections = document.querySelectorAll('section[role="region"]');
      sections.forEach(function(s) {
        if (s.querySelector('a[href="/settings/trends"]')) {
          s.parentElement.parentElement.parentElement.remove();
        }
      });
    }
    
    //put direct links to images
    let links = document.querySelectorAll('a');
    links.forEach(function(a) {
      if (re.test(a.getAttribute('href'))) {
        let img = a.querySelector('img');
        if (img) {
          let src = img.getAttribute('src').replace(img_re, img_repl);
          a.setAttribute('href', src);
          a.setAttribute('x-image', 'true');
        }
      }
    });
    
    //add button to open all images
    let tweets = document.querySelectorAll('article');
    tweets.forEach(function(tw){
      if (!tw.querySelector('div[data-testid="tweet"]')) {
        return;
      }
      let d = tw.querySelector('div[data-openimg="1"]');
      if (!d) {
        let gr = tw.querySelector('div[role="group"]');
        
        var is_single_tweet = false;
        if (getComputedStyle(gr).maxWidth == "none") {
          is_single_tweet = true;
        }
        
        let buttons = gr.querySelectorAll('div[role="button"]')
        let r = buttons[buttons.length-1].parentNode;
        
        let odiv = r.cloneNode(true);
        
        let analyticslink = gr.querySelector('a[role="link"]');
        if (analyticslink) {
          analyticslink.parentNode.style.flexBasis = "0px";
          analyticslink.parentNode.style.flexGrow = "1";
          odiv.style.flexBasis = "auto";
          odiv.style.flexGrow = "0";
        } else if (!is_single_tweet) {
          r.style.flexBasis = "0px";
          r.style.flexGrow = "1";
        }
        
        odiv.setAttribute('data-openimg', '1');
        odiv.querySelector('g').innerHTML = '<path d="M19.75 2H4.25C3.01 2 2 3.01 2 4.25v15.5C2 20.99 3.01 22 4.25 22h15.5c1.24 0 2.25-1.01 2.25-2.25V4.25C22 3.01 20.99 2 19.75 2zM4.25 3.5h15.5c.413 0 .75.337.75.75v9.676l-3.858-3.858c-.14-.14-.33-.22-.53-.22h-.003c-.2 0-.393.08-.532.224l-4.317 4.384-1.813-1.806c-.14-.14-.33-.22-.53-.22-.193-.03-.395.08-.535.227L3.5 17.642V4.25c0-.413.337-.75.75-.75zm-.744 16.28l5.418-5.534 6.282 6.254H4.25c-.402 0-.727-.322-.744-.72zm16.244.72h-2.42l-5.007-4.987 3.792-3.85 4.385 4.384v3.703c0 .413-.337.75-.75.75z"></path><circle cx="8.868" cy="8.309" r="1.542"></circle>';
        odiv.querySelectorAll('div')[0].addEventListener('click', function() {
          let images = tw.querySelectorAll('a[x-image="true"]');
          images.forEach(function(im) {
            window.open(im.getAttribute('href'));
          });
        });
       
        gr.appendChild(odiv);
       
      }
    });
    
  });
  
  ob.observe(document, {
    childList: true,
    subtree: true,
  });
})();