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