comparison tweetdeck-large-image.user.js @ 85:9c8cde985caf

Update standard and fix
author nanaya <me@nanaya.pro>
date Thu, 15 Apr 2021 15:01:22 +0900
parents 86da34e62d29
children 2c4470b73ad9
comparison
equal deleted inserted replaced
84:af4f8b25495e 85:9c8cde985caf
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 const fix = function (link) {
16 var run = function (nodes) {
17 if (nodes == null) {
18 nodes = [document.body]
19 }
20
21 for (var i = 0; i < nodes.length; i++) {
22 // first try fixing itself
23 fix(nodes[i])
24
25 // and then find all the links inside
26 var links = nodes[i].querySelectorAll('.js-media-image-link')
27
28 for (var j = 0; j < links.length; j++) {
29 fix(links[j])
30 }
31 }
32 }
33
34 var fix = function (link) {
35 // basic sanity check 16 // basic sanity check
36 if (!link.classList.contains('js-media-image-link')) { 17 if (!link.classList.contains('js-media-image-link')) {
37 return 18 return
38 } 19 }
39 20
41 if (link._ecUserscript) { 22 if (link._ecUserscript) {
42 return 23 return
43 } 24 }
44 link._ecUserscript = true 25 link._ecUserscript = true
45 26
46 var image = link.querySelector('.media-img') 27 const image = link.querySelector('.media-img')
47 var url 28 let url
48 29
49 // sometimes the image is just background image of the link. 30 // sometimes the image is just background image of the link.
50 // strip all query strings and original :size suffix 31 // strip all query strings and original :size suffix
51 if (image == null) { 32 if (image == null) {
52 url = window.getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, '$2') 33 url = window.getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, '$2')
53 } else { 34 } else {
54 url = image.src 35 url = image.src
55 } 36 }
56 37
57 var parsedUrl = new URL(url) 38 const parsedUrl = new URL(url)
58 39
59 if (parsedUrl.searchParams.get('name') == null) { 40 if (parsedUrl.searchParams.get('name') == null) {
60 url = url.replace(/(\..+:).+/, '$1orig') 41 url = url.replace(/(\..+:).+/, '$1orig')
61 } else { 42 } else {
62 if (parsedUrl.pathname.match(/\.[^.]+$/) !== null) { 43 if (parsedUrl.pathname.match(/\.[^.]+$/) !== null) {
67 } 48 }
68 49
69 link.setAttribute('href', url) 50 link.setAttribute('href', url)
70 } 51 }
71 52
72 var onMutate = function (mutations) { 53 // loop through passed nodes (or body if called without arguments)
73 for (var mutation in mutations) { 54 const run = function (nodes) {
55 if (nodes == null) {
56 nodes = [document.body]
57 }
58
59 for (let i = 0; i < nodes.length; i++) {
60 // first try fixing itself
61 fix(nodes[i])
62
63 // and then find all the links inside
64 const links = nodes[i].querySelectorAll('.js-media-image-link')
65
66 for (let j = 0; j < links.length; j++) {
67 fix(links[j])
68 }
69 }
70 }
71
72 const onMutate = function (mutations) {
73 for (const mutation in mutations) {
74 run(mutation.addedNodes) 74 run(mutation.addedNodes)
75 } 75 }
76 } 76 }
77 77
78 // the observer 78 // the observer
79 var observer = new window.MutationObserver(onMutate) 79 const observer = new window.MutationObserver(onMutate)
80 80
81 // start the observer 81 // start the observer
82 observer.observe(document, { childList: true, subtree: true }) 82 observer.observe(document, { childList: true, subtree: true })
83 // initial run on existing document 83 // initial run on existing document
84 run() 84 run()