Mercurial > ec-userscripts
annotate 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 | 
| 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 | 
| 109 | 14 function origLink () { | 
| 85 | 15 const fix = function (link) { | 
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 16 // basic sanity check | 
| 66 | 17 if (!link.classList.contains('js-media-image-link')) { | 
| 108 | 18 return; | 
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 19 } | 
| 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 20 | 
| 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 21 // don't run again if already run on passed link | 
| 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 22 if (link._ecUserscript) { | 
| 108 | 23 return; | 
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 24 } | 
| 108 | 25 link._ecUserscript = true; | 
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 26 | 
| 108 | 27 const image = link.querySelector('.media-img'); | 
| 28 let url; | |
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 29 | 
| 61 
753765f3814e
Not sure why but sometimes link style is null?
 nanaya <me@nanaya.pro> parents: 
60diff
changeset | 30 // sometimes the image is just background image of the link. | 
| 
753765f3814e
Not sure why but sometimes link style is null?
 nanaya <me@nanaya.pro> parents: 
60diff
changeset | 31 // strip all query strings and original :size suffix | 
| 60 | 32 if (image == null) { | 
| 108 | 33 url = window.getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, '$2'); | 
| 28 | 34 } else { | 
| 108 | 35 url = image.src; | 
| 28 | 36 } | 
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 37 | 
| 108 | 38 const parsedUrl = new URL(url); | 
| 63 
53d0f935ecb8
Fix orig might be in different format
 nanaya <me@nanaya.pro> parents: 
62diff
changeset | 39 | 
| 
53d0f935ecb8
Fix orig might be in different format
 nanaya <me@nanaya.pro> parents: 
62diff
changeset | 40 if (parsedUrl.searchParams.get('name') == null) { | 
| 108 | 41 url = url.replace(/(\..+:).+/, '$1orig'); | 
| 63 
53d0f935ecb8
Fix orig might be in different format
 nanaya <me@nanaya.pro> parents: 
62diff
changeset | 42 } else { | 
| 67 
f03e2d169a8a
Only delete format if there's extension in filename
 nanaya <me@nanaya.pro> parents: 
66diff
changeset | 43 if (parsedUrl.pathname.match(/\.[^.]+$/) !== null) { | 
| 108 | 44 parsedUrl.searchParams.delete('format'); | 
| 67 
f03e2d169a8a
Only delete format if there's extension in filename
 nanaya <me@nanaya.pro> parents: 
66diff
changeset | 45 } | 
| 108 | 46 parsedUrl.searchParams.set('name', 'orig'); | 
| 47 url = parsedUrl.href; | |
| 62 
a065dafbe010
Update to handle new tweetdeck?/twitter? url
 nanaya <me@nanaya.pro> parents: 
61diff
changeset | 48 } | 
| 
a065dafbe010
Update to handle new tweetdeck?/twitter? url
 nanaya <me@nanaya.pro> parents: 
61diff
changeset | 49 | 
| 108 | 50 link.setAttribute('href', url); | 
| 51 }; | |
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 52 | 
| 85 | 53 // loop through passed nodes (or body if called without arguments) | 
| 54 const run = function (nodes) { | |
| 55 if (nodes == null) { | |
| 108 | 56 nodes = [document.body]; | 
| 85 | 57 } | 
| 58 | |
| 59 for (let i = 0; i < nodes.length; i++) { | |
| 60 // first try fixing itself | |
| 108 | 61 fix(nodes[i]); | 
| 85 | 62 | 
| 63 // and then find all the links inside | |
| 108 | 64 const links = nodes[i].querySelectorAll('.js-media-image-link'); | 
| 85 | 65 | 
| 66 for (let j = 0; j < links.length; j++) { | |
| 108 | 67 fix(links[j]); | 
| 85 | 68 } | 
| 69 } | |
| 108 | 70 }; | 
| 85 | 71 | 
| 72 const onMutate = function (mutations) { | |
| 73 for (const mutation in mutations) { | |
| 108 | 74 run(mutation.addedNodes); | 
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 75 } | 
| 108 | 76 }; | 
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 77 | 
| 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 78 // the observer | 
| 108 | 79 const observer = new window.MutationObserver(onMutate); | 
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 80 | 
| 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 81 // start the observer | 
| 108 | 82 observer.observe(document, { childList: true, subtree: true }); | 
| 55 
19c391840d4a
Rewrite the whole thing to use observer instead
 nanaya <me@nanaya.pro> parents: 
49diff
changeset | 83 // initial run on existing document | 
| 108 | 84 run(); | 
| 109 | 85 } | 
| 86 | |
| 87 origLink(); | 
