Mercurial > ec-userscripts
annotate 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 |
rev | line source |
---|---|
24 | 1 // ==UserScript== |
114 | 2 // @name Tweetdeck large image |
109 | 3 // @namespace https://nanaya.net |
114 | 4 // @version 2.0.9 |
115 | 5 // @description No more stupid link for images in tweetdeck |
24 | 6 // @author nanaya |
7 // @match https://tweetdeck.twitter.com/* | |
113 | 8 // @grant none |
115 | 9 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/tweetdeck-large-image.user.js |
24 | 10 // ==/UserScript== |
11 | |
109 | 12 'use strict'; |
33 | 13 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
14 const fix = function (link) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
15 // basic sanity check |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
16 if (!link.classList.contains('js-media-image-link')) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
17 return; |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
18 } |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
19 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
20 // don't run again if already run on passed link |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
21 if (link._ecUserscript) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
22 return; |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
23 } |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
24 link._ecUserscript = true; |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
25 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
26 const image = link.querySelector('.media-img'); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
27 let url; |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
28 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
29 // sometimes the image is just background image of the link. |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
30 // strip all query strings and original :size suffix |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
31 if (image == null) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
32 url = window.getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, '$2'); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
33 } else { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
34 url = image.src; |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
35 } |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
36 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
37 const parsedUrl = new URL(url); |
63
53d0f935ecb8
Fix orig might be in different format
nanaya <me@nanaya.pro>
parents:
62
diff
changeset
|
38 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
39 if (parsedUrl.searchParams.get('name') == null) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
40 url = url.replace(/(\..+:).+/, '$1orig'); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
41 } else { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
42 if (parsedUrl.pathname.match(/\.[^.]+$/) !== null) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
43 parsedUrl.searchParams.delete('format'); |
62
a065dafbe010
Update to handle new tweetdeck?/twitter? url
nanaya <me@nanaya.pro>
parents:
61
diff
changeset
|
44 } |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
45 parsedUrl.searchParams.set('name', 'orig'); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
46 url = parsedUrl.href; |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
47 } |
62
a065dafbe010
Update to handle new tweetdeck?/twitter? url
nanaya <me@nanaya.pro>
parents:
61
diff
changeset
|
48 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
49 link.setAttribute('href', url); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
50 }; |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
51 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
52 // loop through passed nodes (or body if called without arguments) |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
53 const run = function (nodes) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
54 if (nodes == null) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
55 nodes = [document.body]; |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
56 } |
85 | 57 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
58 for (let i = 0; i < nodes.length; i++) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
59 // first try fixing itself |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
60 fix(nodes[i]); |
85 | 61 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
62 // and then find all the links inside |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
63 const links = nodes[i].querySelectorAll('.js-media-image-link'); |
85 | 64 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
65 for (let j = 0; j < links.length; j++) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
66 fix(links[j]); |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
67 } |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
68 } |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
69 }; |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
70 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
71 const onMutate = function (mutations) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
72 for (const mutation in mutations) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
73 run(mutation.addedNodes); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
74 } |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
75 }; |
109 | 76 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
77 // the observer |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
78 const observer = new window.MutationObserver(onMutate); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
79 |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
80 // start the observer |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
81 observer.observe(document, { childList: true, subtree: true }); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
82 // initial run on existing document |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
115
diff
changeset
|
83 run(); |