view tweetdeck-large-image.user.js @ 62:a065dafbe010

Update to handle new tweetdeck?/twitter? url
author nanaya <me@nanaya.pro>
date Wed, 10 Jul 2019 11:29:08 +0900
parents 753765f3814e
children 53d0f935ecb8
line wrap: on
line source

// ==UserScript==
// @name         Tweetdeck large image
// @namespace    https://myconan.net
// @version      2.0.4
// @description  No more stupid link for images in tweetdeck
// @author       nanaya
// @match        https://tweetdeck.twitter.com/*
// @grant        none
// @downloadURL  https://bitbucket.org/nanayapro/ec-userscripts/raw/tip/tweetdeck-large-image.user.js
// ==/UserScript==

;(function() {
  "use strict";

  // loop through passed nodes (or body if called without arguments)
  var run = function(nodes) {
    if (nodes == null) {
      nodes = [document.body];
    }

    for (var i = 0; i < nodes.length; i++) {
      // first try fixing itself
      fix(nodes[i]);

      // and then find all the links inside
      var links = nodes[i].querySelectorAll(".js-media-image-link");

      for (var j = 0; j < links.length; j++) {
        fix(links[j]);
      }
    }
  };

  var fix = function(link) {
    // basic sanity check
    if (!link.classList.contains("js-media-image-link")) {
      return;
    }

    // don't run again if already run on passed link
    if (link._ecUserscript) {
      return;
    }
    link._ecUserscript = true;

    var image = link.querySelector(".media-img");
    var url;

    // sometimes the image is just background image of the link.
    // strip all query strings and original :size suffix
    if (image == null) {
      url = getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, "$2");
    } else {
      url = image.src;
    }

    if (url.match(/\..+:.+/) != null) {
      url = url.replace(/(\..+:).+/, "$1orig");
    } else if (url.match(/name=/) != null) {
      url = url.replace(/(\?|&)name=.+($|&)/, "$1name=orig$2");
    }

    link.setAttribute("href", url);

  };

  var onMutate = function(mutations) {
    for (var mutation in mutations) {
      run(mutation.addedNodes);
    }
  };

  // the observer
  var observer = new MutationObserver(onMutate);

  // start the observer
  observer.observe(document, { childList: true, subtree: true});
  // initial run on existing document
  run();
}).call();