annotate tweetdeck-fixes.user.js @ 110:b9b84779a672

Combine tweetdeck fixes
author nanaya <me@nanaya.net>
date Sun, 15 Jan 2023 23:36:45 +0900
parents tweetdeck-large-image.user.js@ef21ef445fc6
children 055f5d084706
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
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
3 // @namespace https://nanaya.net
82
86da34e62d29 Update url
nanaya <me@nanaya.pro>
parents: 77
diff changeset
4 // @version 2.0.9
110
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
5 // @description Fixes for TweetDeck
24
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
110
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
9 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/tweetdeck-fixes.user.js
24
Edho Arief <me@myconan.net>
parents:
diff changeset
10 // ==/UserScript==
Edho Arief <me@myconan.net>
parents:
diff changeset
11
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
12 'use strict';
33
Edho Arief <me@myconan.net>
parents: 28
diff changeset
13
110
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
14 /* global GM_addStyle */
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
15 // No weird column alignment and color for scrollbar
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
16 function fixColumn () {
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
17 GM_addStyle(`
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
18 .app-columns {
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
19 display: flex;
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
20 }
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
21 .column {
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
22 top: 0;
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
23 flex: none;
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
24 }
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
25 .app-columns-container.app-columns-container.app-columns-container.app-columns-container {
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
26 bottom: 0;
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
27 background-color: #555;
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
28 }
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
29 `);
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
30 }
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
31
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
32 // No more stupid link for images in tweetdeck
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
33 function origLink () {
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
34 const 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
444b17e657be Standardize code style
nanaya <me@nanaya.pro>
parents: 64
diff changeset
36 if (!link.classList.contains('js-media-image-link')) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
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) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
42 return;
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
43 }
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
44 link._ecUserscript = true;
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
45
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
46 const image = link.querySelector('.media-img');
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
47 let 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
cd3ffdc31613 Better null check
nanaya <me@nanaya.pro>
parents: 58
diff changeset
51 if (image == null) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
52 url = window.getComputedStyle(link).backgroundImage.replace(/^url\(('|")?(.+?)\1\)$/, '$2');
28
Edho Arief <me@myconan.net>
parents: 26
diff changeset
53 } else {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
54 url = image.src;
28
Edho Arief <me@myconan.net>
parents: 26
diff changeset
55 }
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
56
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
57 const 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) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
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) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
63 parsedUrl.searchParams.delete('format');
67
f03e2d169a8a Only delete format if there's extension in filename
nanaya <me@nanaya.pro>
parents: 66
diff changeset
64 }
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
65 parsedUrl.searchParams.set('name', 'orig');
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
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
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
69 link.setAttribute('href', url);
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
70 };
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
71
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
72 // loop through passed nodes (or body if called without arguments)
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
73 const run = function (nodes) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
74 if (nodes == null) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
75 nodes = [document.body];
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
76 }
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
77
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
78 for (let i = 0; i < nodes.length; i++) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
79 // first try fixing itself
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
80 fix(nodes[i]);
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
81
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
82 // and then find all the links inside
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
83 const links = nodes[i].querySelectorAll('.js-media-image-link');
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
84
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
85 for (let j = 0; j < links.length; j++) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
86 fix(links[j]);
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
87 }
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
88 }
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
89 };
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
90
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
91 const onMutate = function (mutations) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
92 for (const mutation in mutations) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
93 run(mutation.addedNodes);
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
94 }
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
95 };
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
96
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
97 // the observer
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
98 const observer = new window.MutationObserver(onMutate);
55
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
99
19c391840d4a Rewrite the whole thing to use observer instead
nanaya <me@nanaya.pro>
parents: 49
diff changeset
100 // start the observer
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
101 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
102 // initial run on existing document
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
103 run();
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
104 }
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
105
110
b9b84779a672 Combine tweetdeck fixes
nanaya <me@nanaya.net>
parents: 109
diff changeset
106 fixColumn();
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
107 origLink();