comparison tweetdeck-large-image.user.js @ 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 2e1cae72a63e
children 09f735548b2c
comparison
equal deleted inserted replaced
54:e76b7766d5bf 55:19c391840d4a
1 // ==UserScript== 1 // ==UserScript==
2 // @name Tweetdeck large image 2 // @name Tweetdeck large image
3 // @namespace https://myconan.net 3 // @namespace https://myconan.net
4 // @version 1.0.2 4 // @version 2.0.0
5 // @description No more stupid link for images in tweetdeck 5 // @description No more stupid link for images in tweetdeck
6 // @author nanaya 6 // @author nanaya
7 // @match https://tweetdeck.twitter.com/* 7 // @match https://tweetdeck.twitter.com/*
8 // @grant none 8 // @grant none
9 // @downloadURL https://bitbucket.org/!api/2.0/snippets/nanayapro/TK64/tip/files/tweetdeck-large-image.user.js 9 // @downloadURL https://bitbucket.org/!api/2.0/snippets/nanayapro/TK64/tip/files/tweetdeck-large-image.user.js
10 // ==/UserScript== 10 // ==/UserScript==
11 11
12 ;(function() { 12 ;(function() {
13 "use strict"; 13 "use strict";
14 14
15 var $ = jQuery; 15 // loop through passed nodes (or body if called without arguments)
16 var replaceLink = function(e) { 16 var run = function(nodes) {
17 var link = e.currentTarget; 17 if (nodes === undefined) {
18 if (link._ecUserscript === true) { return; } 18 nodes = [document.body];
19 19 }
20 var images = link.getElementsByClassName("media-img"); 20
21 21 for (var i = 0; i < nodes.length; i++) {
22 // first try fixing itself
23 fix(nodes[i]);
24
25 // and then find all the links inside
26 var links = nodes[i].querySelectorAll(".js-media-image-link");
27
28 for (var j = 0; j < links.length; j++) {
29 fix(links[j]);
30 }
31 }
32 };
33
34 var fix = function(link) {
35 // basic sanity check
36 if (!link.classList.contains("js-media-image-link")) {
37 return;
38 }
39
40 // don't run again if already run on passed link
41 if (link._ecUserscript) {
42 return;
43 }
44 link._ecUserscript = true;
45
46 var image = link.querySelector(".media-img");
22 var url; 47 var url;
23 if (images.length) { 48
24 url = images[0].src.replace(/:[a-z0-9]+$/, ""); 49 if (image === null) {
50 // sometimes the image is just background image of the link.
51 // strip all query strings and original :size suffix
52 url = getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)(?::small)?(?:\?.*)?\1\)$/, "$2");
25 } else { 53 } else {
26 url = getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)(:small)?\1\)$/, "$2"); 54 url = image.src.replace(/:[a-z0-9]+(?:\?.*)?$/, "");
27 } 55 }
56
28 link.setAttribute("href", url + ":orig"); 57 link.setAttribute("href", url + ":orig");
29 link._ecUserscript = true; 58
30 }; 59 };
31 60
32 $(document).off(".ec-userscript"); 61 var onMutate = function(mutations) {
33 $(document).on("mouseenter.ec-userscript", ".js-media-image-link", replaceLink); 62 for (var mutation in mutations) {
63 run(mutation.addedNodes);
64 }
65 };
66
67 // the observer
68 var observer = new MutationObserver(onMutate);
69
70 // start the observer
71 observer.observe(document, { childList: true, subtree: true});
72 // initial run on existing document
73 run();
34 }).call(); 74 }).call();