Mercurial > ec-userscripts
comparison tweetdeck-large-image.user.js @ 66:444b17e657be
Standardize code style
author | nanaya <me@nanaya.pro> |
---|---|
date | Wed, 10 Jul 2019 22:36:31 +0900 |
parents | 6715e53ad0bf |
children | f03e2d169a8a |
comparison
equal
deleted
inserted
replaced
65:72ef6fcdff44 | 66:444b17e657be |
---|---|
7 // @match https://tweetdeck.twitter.com/* | 7 // @match https://tweetdeck.twitter.com/* |
8 // @grant none | 8 // @grant none |
9 // @downloadURL https://bitbucket.org/nanayapro/ec-userscripts/raw/tip/tweetdeck-large-image.user.js | 9 // @downloadURL https://bitbucket.org/nanayapro/ec-userscripts/raw/tip/tweetdeck-large-image.user.js |
10 // ==/UserScript== | 10 // ==/UserScript== |
11 | 11 |
12 ;(function() { | 12 ;(function () { |
13 "use strict"; | 13 'use strict' |
14 | 14 |
15 // loop through passed nodes (or body if called without arguments) | 15 // loop through passed nodes (or body if called without arguments) |
16 var run = function(nodes) { | 16 var run = function (nodes) { |
17 if (nodes == null) { | 17 if (nodes == null) { |
18 nodes = [document.body]; | 18 nodes = [document.body] |
19 } | 19 } |
20 | 20 |
21 for (var i = 0; i < nodes.length; i++) { | 21 for (var i = 0; i < nodes.length; i++) { |
22 // first try fixing itself | 22 // first try fixing itself |
23 fix(nodes[i]); | 23 fix(nodes[i]) |
24 | 24 |
25 // and then find all the links inside | 25 // and then find all the links inside |
26 var links = nodes[i].querySelectorAll(".js-media-image-link"); | 26 var links = nodes[i].querySelectorAll('.js-media-image-link') |
27 | 27 |
28 for (var j = 0; j < links.length; j++) { | 28 for (var j = 0; j < links.length; j++) { |
29 fix(links[j]); | 29 fix(links[j]) |
30 } | 30 } |
31 } | 31 } |
32 }; | 32 } |
33 | 33 |
34 var fix = function(link) { | 34 var fix = function (link) { |
35 // basic sanity check | 35 // basic sanity check |
36 if (!link.classList.contains("js-media-image-link")) { | 36 if (!link.classList.contains('js-media-image-link')) { |
37 return; | 37 return |
38 } | 38 } |
39 | 39 |
40 // don't run again if already run on passed link | 40 // don't run again if already run on passed link |
41 if (link._ecUserscript) { | 41 if (link._ecUserscript) { |
42 return; | 42 return |
43 } | 43 } |
44 link._ecUserscript = true; | 44 link._ecUserscript = true |
45 | 45 |
46 var image = link.querySelector(".media-img"); | 46 var image = link.querySelector('.media-img') |
47 var url; | 47 var url |
48 | 48 |
49 // sometimes the image is just background image of the link. | 49 // sometimes the image is just background image of the link. |
50 // strip all query strings and original :size suffix | 50 // strip all query strings and original :size suffix |
51 if (image == null) { | 51 if (image == null) { |
52 url = getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, "$2"); | 52 url = window.getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, '$2') |
53 } else { | 53 } else { |
54 url = image.src; | 54 url = image.src |
55 } | 55 } |
56 | 56 |
57 var parsedUrl = new URL(url); | 57 var parsedUrl = new URL(url) |
58 | 58 |
59 if (parsedUrl.searchParams.get('name') == null) { | 59 if (parsedUrl.searchParams.get('name') == null) { |
60 url = url.replace(/(\..+:).+/, "$1orig"); | 60 url = url.replace(/(\..+:).+/, '$1orig') |
61 } else { | 61 } else { |
62 parsedUrl.searchParams.delete('format'); | 62 parsedUrl.searchParams.delete('format') |
63 parsedUrl.searchParams.set('name', 'orig'); | 63 parsedUrl.searchParams.set('name', 'orig') |
64 url = parsedUrl.href; | 64 url = parsedUrl.href |
65 } | 65 } |
66 | 66 |
67 link.setAttribute("href", url); | 67 link.setAttribute('href', url) |
68 } | |
68 | 69 |
69 }; | 70 var onMutate = function (mutations) { |
70 | |
71 var onMutate = function(mutations) { | |
72 for (var mutation in mutations) { | 71 for (var mutation in mutations) { |
73 run(mutation.addedNodes); | 72 run(mutation.addedNodes) |
74 } | 73 } |
75 }; | 74 } |
76 | 75 |
77 // the observer | 76 // the observer |
78 var observer = new MutationObserver(onMutate); | 77 var observer = new window.MutationObserver(onMutate) |
79 | 78 |
80 // start the observer | 79 // start the observer |
81 observer.observe(document, { childList: true, subtree: true}); | 80 observer.observe(document, { childList: true, subtree: true }) |
82 // initial run on existing document | 81 // initial run on existing document |
83 run(); | 82 run() |
84 }).call(); | 83 }).call() |