comparison tweetdeck-large-image.user.js @ 117:d9dc190bccaf

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