Mercurial > ec-userscripts
annotate tweetdeck-large-image.user.js @ 61:753765f3814e
Not sure why but sometimes link style is null?
author | nanaya <me@nanaya.pro> |
---|---|
date | Sun, 12 May 2019 01:30:44 +0900 |
parents | cd3ffdc31613 |
children | a065dafbe010 |
rev | line source |
---|---|
24 | 1 // ==UserScript== |
2 // @name Tweetdeck large image | |
3 // @namespace https://myconan.net | |
61
753765f3814e
Not sure why but sometimes link style is null?
nanaya <me@nanaya.pro>
parents:
60
diff
changeset
|
4 // @version 2.0.3 |
24 | 5 // @description No more stupid link for images in tweetdeck |
6 // @author nanaya | |
7 // @match https://tweetdeck.twitter.com/* | |
8 // @grant none | |
57 | 9 // @downloadURL https://bitbucket.org/nanayapro/ec-userscripts/raw/tip/tweetdeck-large-image.user.js |
24 | 10 // ==/UserScript== |
11 | |
12 ;(function() { | |
33 | 13 "use strict"; |
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) |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
16 var run = function(nodes) { |
60 | 17 if (nodes == null) { |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
18 nodes = [document.body]; |
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 |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
23 fix(nodes[i]); |
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 |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
26 var links = nodes[i].querySelectorAll(".js-media-image-link"); |
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++) { |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
29 fix(links[j]); |
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 } |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
32 }; |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
33 |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
34 var fix = function(link) { |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
35 // basic sanity check |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
36 if (!link.classList.contains("js-media-image-link")) { |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
37 return; |
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) { |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
42 return; |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
43 } |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
44 link._ecUserscript = true; |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
45 |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
46 var image = link.querySelector(".media-img"); |
49 | 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) { |
61
753765f3814e
Not sure why but sometimes link style is null?
nanaya <me@nanaya.pro>
parents:
60
diff
changeset
|
52 |
753765f3814e
Not sure why but sometimes link style is null?
nanaya <me@nanaya.pro>
parents:
60
diff
changeset
|
53 var linkStyle = getComputedStyle(link); |
753765f3814e
Not sure why but sometimes link style is null?
nanaya <me@nanaya.pro>
parents:
60
diff
changeset
|
54 |
753765f3814e
Not sure why but sometimes link style is null?
nanaya <me@nanaya.pro>
parents:
60
diff
changeset
|
55 if (linkStyle == null) { |
753765f3814e
Not sure why but sometimes link style is null?
nanaya <me@nanaya.pro>
parents:
60
diff
changeset
|
56 return; |
753765f3814e
Not sure why but sometimes link style is null?
nanaya <me@nanaya.pro>
parents:
60
diff
changeset
|
57 } |
753765f3814e
Not sure why but sometimes link style is null?
nanaya <me@nanaya.pro>
parents:
60
diff
changeset
|
58 |
753765f3814e
Not sure why but sometimes link style is null?
nanaya <me@nanaya.pro>
parents:
60
diff
changeset
|
59 url = linkStyle.backgroundImage.replace(/^url\(('|")?(.+?)(?::small)?(?:\?.*)?\1\)$/, "$2"); |
28 | 60 } else { |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
61 url = image.src.replace(/:[a-z0-9]+(?:\?.*)?$/, ""); |
28 | 62 } |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
63 |
49 | 64 link.setAttribute("href", url + ":orig"); |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
65 |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
66 }; |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
67 |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
68 var onMutate = function(mutations) { |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
69 for (var mutation in mutations) { |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
70 run(mutation.addedNodes); |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
71 } |
49 | 72 }; |
55
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
73 |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
74 // the observer |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
75 var observer = new MutationObserver(onMutate); |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
76 |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
77 // start the observer |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
78 observer.observe(document, { childList: true, subtree: true}); |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
79 // initial run on existing document |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
80 run(); |
19c391840d4a
Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents:
49
diff
changeset
|
81 }).call(); |