comparison tweetdeck-large-image.user.js @ 108:2c4470b73ad9

Switch to semistandard No more `;` at the start of line.
author nanaya <me@nanaya.net>
date Sun, 15 Jan 2023 23:24:03 +0900
parents 9c8cde985caf
children ef21ef445fc6
comparison
equal deleted inserted replaced
107:4bc5a633437c 108:2c4470b73ad9
7 // @match https://tweetdeck.twitter.com/* 7 // @match https://tweetdeck.twitter.com/*
8 // @grant none 8 // @grant none
9 // @downloadURL https://hg.myconan.net/ec-userscripts/raw-file/tip/tweetdeck-large-image.user.js 9 // @downloadURL https://hg.myconan.net/ec-userscripts/raw-file/tip/tweetdeck-large-image.user.js
10 // ==/UserScript== 10 // ==/UserScript==
11 11
12 ;(function () { 12 (function () {
13 'use strict' 13 'use strict';
14 14
15 const fix = function (link) { 15 const fix = function (link) {
16 // basic sanity check 16 // basic sanity check
17 if (!link.classList.contains('js-media-image-link')) { 17 if (!link.classList.contains('js-media-image-link')) {
18 return 18 return;
19 } 19 }
20 20
21 // don't run again if already run on passed link 21 // don't run again if already run on passed link
22 if (link._ecUserscript) { 22 if (link._ecUserscript) {
23 return 23 return;
24 } 24 }
25 link._ecUserscript = true 25 link._ecUserscript = true;
26 26
27 const image = link.querySelector('.media-img') 27 const image = link.querySelector('.media-img');
28 let url 28 let url;
29 29
30 // sometimes the image is just background image of the link. 30 // sometimes the image is just background image of the link.
31 // strip all query strings and original :size suffix 31 // strip all query strings and original :size suffix
32 if (image == null) { 32 if (image == null) {
33 url = window.getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, '$2') 33 url = window.getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, '$2');
34 } else { 34 } else {
35 url = image.src 35 url = image.src;
36 } 36 }
37 37
38 const parsedUrl = new URL(url) 38 const parsedUrl = new URL(url);
39 39
40 if (parsedUrl.searchParams.get('name') == null) { 40 if (parsedUrl.searchParams.get('name') == null) {
41 url = url.replace(/(\..+:).+/, '$1orig') 41 url = url.replace(/(\..+:).+/, '$1orig');
42 } else { 42 } else {
43 if (parsedUrl.pathname.match(/\.[^.]+$/) !== null) { 43 if (parsedUrl.pathname.match(/\.[^.]+$/) !== null) {
44 parsedUrl.searchParams.delete('format') 44 parsedUrl.searchParams.delete('format');
45 } 45 }
46 parsedUrl.searchParams.set('name', 'orig') 46 parsedUrl.searchParams.set('name', 'orig');
47 url = parsedUrl.href 47 url = parsedUrl.href;
48 } 48 }
49 49
50 link.setAttribute('href', url) 50 link.setAttribute('href', url);
51 } 51 };
52 52
53 // loop through passed nodes (or body if called without arguments) 53 // loop through passed nodes (or body if called without arguments)
54 const run = function (nodes) { 54 const run = function (nodes) {
55 if (nodes == null) { 55 if (nodes == null) {
56 nodes = [document.body] 56 nodes = [document.body];
57 } 57 }
58 58
59 for (let i = 0; i < nodes.length; i++) { 59 for (let i = 0; i < nodes.length; i++) {
60 // first try fixing itself 60 // first try fixing itself
61 fix(nodes[i]) 61 fix(nodes[i]);
62 62
63 // and then find all the links inside 63 // and then find all the links inside
64 const links = nodes[i].querySelectorAll('.js-media-image-link') 64 const links = nodes[i].querySelectorAll('.js-media-image-link');
65 65
66 for (let j = 0; j < links.length; j++) { 66 for (let j = 0; j < links.length; j++) {
67 fix(links[j]) 67 fix(links[j]);
68 } 68 }
69 } 69 }
70 } 70 };
71 71
72 const onMutate = function (mutations) { 72 const onMutate = function (mutations) {
73 for (const mutation in mutations) { 73 for (const mutation in mutations) {
74 run(mutation.addedNodes) 74 run(mutation.addedNodes);
75 } 75 }
76 } 76 };
77 77
78 // the observer 78 // the observer
79 const observer = new window.MutationObserver(onMutate) 79 const observer = new window.MutationObserver(onMutate);
80 80
81 // start the observer 81 // start the observer
82 observer.observe(document, { childList: true, subtree: true }) 82 observer.observe(document, { childList: true, subtree: true });
83 // initial run on existing document 83 // initial run on existing document
84 run() 84 run();
85 }).call() 85 }).call();