changeset 55:19c391840d4a

Rewrite the whole thing to use observer instead And holy crap plain javascript is stupid. Doesn't help I'm not using smarter loop method.
author nanaya <me@nanaya.pro>
date Sat, 07 Jul 2018 01:29:36 +0900
parents e76b7766d5bf
children e5377751525b
files tweetdeck-large-image.user.js
diffstat 1 files changed, 56 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/tweetdeck-large-image.user.js	Sat Jun 30 21:56:55 2018 +0900
+++ b/tweetdeck-large-image.user.js	Sat Jul 07 01:29:36 2018 +0900
@@ -1,7 +1,7 @@
 // ==UserScript==
 // @name         Tweetdeck large image
 // @namespace    https://myconan.net
-// @version      1.0.2
+// @version      2.0.0
 // @description  No more stupid link for images in tweetdeck
 // @author       nanaya
 // @match        https://tweetdeck.twitter.com/*
@@ -12,23 +12,63 @@
 ;(function() {
   "use strict";
 
-  var $ = jQuery;
-  var replaceLink = function(e) {
-    var link = e.currentTarget;
-    if (link._ecUserscript === true) { return; }
-    
-    var images = link.getElementsByClassName("media-img");
-    
+  // loop through passed nodes (or body if called without arguments)
+  var run = function(nodes) {
+    if (nodes === undefined) {
+      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;
-    if (images.length) {
-      url = images[0].src.replace(/:[a-z0-9]+$/, "");
+
+    if (image === null) {
+      // sometimes the image is just background image of the link.
+      // strip all query strings and original :size suffix
+      url = getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)(?::small)?(?:\?.*)?\1\)$/, "$2");
     } else {
-      url = getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)(:small)?\1\)$/, "$2");
+      url = image.src.replace(/:[a-z0-9]+(?:\?.*)?$/, "");
     }
+
     link.setAttribute("href", url + ":orig");
-    link._ecUserscript = true;
+
+  };
+
+  var onMutate = function(mutations) {
+    for (var mutation in mutations) {
+      run(mutation.addedNodes);
+    }
   };
-  
-  $(document).off(".ec-userscript");
-  $(document).on("mouseenter.ec-userscript", ".js-media-image-link", replaceLink);
-}).call();
\ No newline at end of file
+
+  // the observer
+  var observer = new MutationObserver(onMutate);
+
+  // start the observer
+  observer.observe(document, { childList: true, subtree: true});
+  // initial run on existing document
+  run();
+}).call();