comparison tweetdeck-large-image.user.js @ 115:7774174022af

Backed out changeset b9b84779a672
author nanaya <me@nanaya.net>
date Sun, 15 Jan 2023 23:43:44 +0900
parents tweetdeck-fixes.user.js@817b62848b27
children d9dc190bccaf
comparison
equal deleted inserted replaced
114:817b62848b27 115:7774174022af
1 // ==UserScript==
2 // @name Tweetdeck large image
3 // @namespace https://nanaya.net
4 // @version 2.0.9
5 // @description No more stupid link for images in tweetdeck
6 // @author nanaya
7 // @match https://tweetdeck.twitter.com/*
8 // @grant none
9 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/tweetdeck-large-image.user.js
10 // ==/UserScript==
11
12 'use strict';
13
14 function origLink () {
15 const fix = function (link) {
16 // basic sanity check
17 if (!link.classList.contains('js-media-image-link')) {
18 return;
19 }
20
21 // don't run again if already run on passed link
22 if (link._ecUserscript) {
23 return;
24 }
25 link._ecUserscript = true;
26
27 const image = link.querySelector('.media-img');
28 let url;
29
30 // sometimes the image is just background image of the link.
31 // strip all query strings and original :size suffix
32 if (image == null) {
33 url = window.getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, '$2');
34 } else {
35 url = image.src;
36 }
37
38 const parsedUrl = new URL(url);
39
40 if (parsedUrl.searchParams.get('name') == null) {
41 url = url.replace(/(\..+:).+/, '$1orig');
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();