New twitter improvements

Improves new version of twitter a little bit

Versione datata 19/07/2019. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name New twitter improvements
// @description Improves new version of twitter a little bit
// @namespace Violentmonkey Scripts
// @match https://mobile.twitter.com/*
// @grant none
// @inject-into content
// @license WTFPL
// @version 0.3
// ==/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[alt="Image"]');
        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('[data-testid^="tweet"]');
    tweets.forEach(function(tw){
      let d = tw.querySelector('div[data-openimg="1"]');
      if (!d) {
        let gr = tw.querySelector('div[role="group"]');
        let buttons = tw.querySelectorAll('div[role="button"]')
        let r = buttons[buttons.length-1];
        
        let odiv = r.parentNode.cloneNode(true);
        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,
  });
})();