annotate tweetdeck-large-image.user.js @ 102:3fded109e23a

Also run on own node
author nanaya <me@nanaya.net>
date Sat, 24 Dec 2022 21:33:30 +0900
parents 9c8cde985caf
children 2c4470b73ad9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
Edho Arief <me@myconan.net>
parents:
diff changeset
1 // ==UserScript==
Edho Arief <me@myconan.net>
parents:
diff changeset
2 // @name Tweetdeck large image
Edho Arief <me@myconan.net>
parents:
diff changeset
3 // @namespace https://myconan.net
82
86da34e62d29 Update url
nanaya <me@nanaya.pro>
parents: 77
diff changeset
4 // @version 2.0.9
24
Edho Arief <me@myconan.net>
parents:
diff changeset
5 // @description No more stupid link for images in tweetdeck
Edho Arief <me@myconan.net>
parents:
diff changeset
6 // @author nanaya
Edho Arief <me@myconan.net>
parents:
diff changeset
7 // @match https://tweetdeck.twitter.com/*
Edho Arief <me@myconan.net>
parents:
diff changeset
8 // @grant none
82
86da34e62d29 Update url
nanaya <me@nanaya.pro>
parents: 77
diff changeset
9 // @downloadURL https://hg.myconan.net/ec-userscripts/raw-file/tip/tweetdeck-large-image.user.js
24
Edho Arief <me@myconan.net>
parents:
diff changeset
10 // ==/UserScript==
Edho Arief <me@myconan.net>
parents:
diff changeset
11
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
12 ;(function () {
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
13 'use strict'
33
Edho Arief <me@myconan.net>
parents: 28
diff changeset
14
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
15 const fix = function (link) {
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
16 // basic sanity check
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
17 if (!link.classList.contains('js-media-image-link')) {
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
18 return
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 // 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
22 if (link._ecUserscript) {
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
23 return
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
24 }
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
25 link._ecUserscript = true
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
26
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
27 const image = link.querySelector('.media-img')
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
28 let url
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
29
61
753765f3814e Not sure why but sometimes link style is null?
nanaya <me@nanaya.pro>
parents: 60
diff 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: 60
diff changeset
31 // strip all query strings and original :size suffix
60
cd3ffdc31613 Better null check
nanaya <me@nanaya.pro>
parents: 58
diff changeset
32 if (image == null) {
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
33 url = window.getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, '$2')
28
Edho Arief <me@myconan.net>
parents: 26
diff changeset
34 } else {
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
35 url = image.src
28
Edho Arief <me@myconan.net>
parents: 26
diff changeset
36 }
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
37
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
38 const parsedUrl = new URL(url)
63
53d0f935ecb8 Fix orig might be in different format
nanaya <me@nanaya.pro>
parents: 62
diff changeset
39
53d0f935ecb8 Fix orig might be in different format
nanaya <me@nanaya.pro>
parents: 62
diff changeset
40 if (parsedUrl.searchParams.get('name') == null) {
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
41 url = url.replace(/(\..+:).+/, '$1orig')
63
53d0f935ecb8 Fix orig might be in different format
nanaya <me@nanaya.pro>
parents: 62
diff changeset
42 } else {
67
f03e2d169a8a Only delete format if there's extension in filename
nanaya <me@nanaya.pro>
parents: 66
diff changeset
43 if (parsedUrl.pathname.match(/\.[^.]+$/) !== null) {
f03e2d169a8a Only delete format if there's extension in filename
nanaya <me@nanaya.pro>
parents: 66
diff changeset
44 parsedUrl.searchParams.delete('format')
f03e2d169a8a Only delete format if there's extension in filename
nanaya <me@nanaya.pro>
parents: 66
diff changeset
45 }
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
46 parsedUrl.searchParams.set('name', 'orig')
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
47 url = parsedUrl.href
62
a065dafbe010 Update to handle new tweetdeck?/twitter? url
nanaya <me@nanaya.pro>
parents: 61
diff changeset
48 }
a065dafbe010 Update to handle new tweetdeck?/twitter? url
nanaya <me@nanaya.pro>
parents: 61
diff changeset
49
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
50 link.setAttribute('href', url)
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
51 }
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
52
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
53 // loop through passed nodes (or body if called without arguments)
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
54 const run = function (nodes) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
55 if (nodes == null) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
56 nodes = [document.body]
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
57 }
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
58
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
59 for (let i = 0; i < nodes.length; i++) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
60 // first try fixing itself
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
61 fix(nodes[i])
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
62
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
63 // and then find all the links inside
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
64 const links = nodes[i].querySelectorAll('.js-media-image-link')
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
65
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
66 for (let j = 0; j < links.length; j++) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
67 fix(links[j])
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
68 }
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
69 }
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
70 }
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
71
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
72 const onMutate = function (mutations) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
73 for (const mutation in mutations) {
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
74 run(mutation.addedNodes)
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
75 }
66
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
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
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
79 const 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
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
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
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
84 run()
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
85 }).call()