Skip to content

Commit 7004dc7

Browse files
committed
Fix custom.css detection for >= 4.4.0. Enhance version detection to detect all pattern above 4.3.X correctly
1 parent 102bf37 commit 7004dc7

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

TangerineUI.user.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
// @match https://piaille.fr/*
77
// @match https://eldritch.cafe/*
88
// @match https://h-i.social/*
9+
// @match https://diaspodon.fr/*
910
// @match https://AddYourInstanceUrlHere.tld/*
1011
// @downloadURL https://github.com/Write/TangerineUI-Userscript/raw/main/TangerineUI.user.js
1112
// @updateURL https://github.com/Write/TangerineUI-Userscript/raw/main/TangerineUI.user.js
1213
// @homepageURL https://github.com/Write/TangerineUI-Userscript
1314
// @grant none
14-
// @version 2.2.3
15+
// @version 2.2.3.1
1516
// @author @Write on Github for the UserScript
1617
// @author @nileane for TangerineUI's CSS
1718
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACE0lEQVR4AZXOA2xeUQAF4BvNW8xGi7WFY227v81otu0Fs23btq3attunvrN7//bOPMmX98414bFa4edyda12u7te0m8d1UKhRyNVT32idrtcGE6+jdMpBzjtUjOFf2KTZJddchKWsWPR22HprKLATBwnYOM6CfNni+Bjs2d0j02ZKLDOCVZrhx9xGNuT7aY2MAvmdKCjQwXP0UMidm4ToPYMiSKwcmkn+Hqbsd1L7LqWqTZ9M5injyXf4oN7O3HkQCdkGRAEFWdOCNi9vROKAnx8L4Ovt+uaZhCbpn6eTVsPpjBfRkN9F3ivLFfQ0qJ+6SXFCupqv86zvcSWVjPTml4DprhQRm21At7ZgbU1X3tBnoxGegHvlvSa2cSaUjXWklIJhh/Ae2Ge5DuA94JcyfdC3q2plVOJObncYU4qA1NcIPkO4H2soxLjXZW80wNEesDXeVNSqYcY44s1pvhiMDcvt+LB7Tbw/qPrF1rx+F77l25IKh1KjNEFw40xBfhfhpiCs4RFE/JxgDEqR6Xwj5qptZaot/0JjyE867UhPBOcMeJThCH8U6QhLNNgjMj06iMyjfQ/0RSROSQ2Nrc3+TG60I/J+tAPKgWfsI/7NaMe9SXfxBSbO4j8Kdrgdx5d0FuBQrd3zbrgN28Y2quofPK3aILeDtb6v1yt8X/5Suv/qo0Sdf6vijT+r05q/F9Hkl/kM/y+SpUd6mHfAAAAAElFTkSuQmCC
@@ -31,7 +32,7 @@
3132

3233
/* colorScheme used for new (>= 4.3) instances */
3334
/* Either 'tangerine', 'purple', 'cherry' or 'lagoon' */
34-
const colorScheme = 'tangerine';
35+
const colorScheme = 'cherry';
3536

3637
/* Github tag to use for mastodon instance < 4.3 and >= 4.3
3738
/* To find tags name, go here : https://github.com/nileane/TangerineUI-for-Mastodon/releases
@@ -98,12 +99,27 @@
9899
let isVersionDetected = false;
99100
let nonce;
100101

102+
function isVersionAtLeast(version, minVersion) {
103+
const parseVersion = (v) => v.split('.').map(num => parseInt(num, 10));
104+
const current = parseVersion(version);
105+
const minimum = parseVersion(minVersion);
106+
107+
for (let i = 0; i < Math.max(current.length, minimum.length); i++) {
108+
const currentPart = current[i] || 0;
109+
const minimumPart = minimum[i] || 0;
110+
111+
if (currentPart > minimumPart) return true;
112+
if (currentPart < minimumPart) return false;
113+
}
114+
return true; // Equal versions
115+
}
116+
101117
/* Version detection */
102118
onElemAvailable('script[id^=initial-state]').then((selector) => {
103119
isVersionDetected = true;
104120
const mastodonVersion = JSON.parse(selector.innerText).meta.version
105121
log("Mastodon Version Detected is : " + mastodonVersion);
106-
if (mastodonVersion.includes('4.3.') || mastodonVersion.includes('4.4.') || mastodonVersion.includes('4.5.')) {
122+
if (isVersionAtLeast(mastodonVersion, '4.3.0')) {
107123
log("Version above or equal 4.3.0 found.")
108124
if (isPurple)
109125
styleUrl = tangerine_above_or_equals_4_3_0 + "-purple.min.css"
@@ -170,7 +186,7 @@
170186
isVersionDetected = true;
171187
const mastodonVersion = JSON.parse(node.innerText).meta.version
172188
log("(mutationObserver) Mastodon Version Detected is : " + mastodonVersion);
173-
if (mastodonVersion.includes('4.3.') || mastodonVersion.includes('4.4.') || mastodonVersion.includes('4.5.')) {
189+
if (isVersionAtLeast(mastodonVersion, '4.3.0')) {
174190
log("(mutationObserver) Version above or equal 4.3.0 found.")
175191
if (isPurple)
176192
styleUrl = tangerine_above_or_equals_4_3_0 + "-purple.min.css"
@@ -190,11 +206,11 @@
190206
href = node.href;
191207
const rChunk = /\/packs\/css\/mastodon.*\.chunk\.css/
192208
const rCustom = /custom\.css/
193-
194-
/* Inject after custom.css or mastodon.*.chunk.css, whichever is detected first
195-
* On my system mastodon.*.chunk.css wasn't detected on Chrome
196-
* */
197-
if ((href.match(rCustom) || href.match(rChunk)) && !isInjected) {
209+
const rCustomWithHash = /custom-[a-f0-9]+\.css/
210+
/* Inject after custom.css (old or new format) or mastodon.*.chunk.css, whichever is detected first
211+
* On my system mastodon.*.chunk.css wasn't detected on Chrome
212+
*/
213+
if ((href.match(rCustom) || href.match(rCustomWithHash) || href.match(rChunk)) && !isInjected) {
198214
if (nonce && isVersionDetected) {
199215
nChanges++;
200216
isInjected = true;
@@ -248,22 +264,24 @@
248264
/* Workaround if MutationObserver couldn't detect the item
249265
* Usually in Safari and Chrome (for Chrome, the first load seems to use MutationObserver, but anyway)
250266
* */
251-
onElemAvailable('link[rel^=stylesheet][href*=custom\\.css]').then(() => {
252-
if (!isInjected) {
253-
if (nonce) {
254-
document.head.appendChild(createStyleNode(nonce));
255-
isInjected = true;
256-
log("(requestAnimationFrame) custom.css detected, injected TangerineUI's css asap");
257-
}
258-
else {
259-
isInjected = true;
260-
onElemAvailable('meta[name^=style\\-nonce]').then((selector) => {
261-
let nonce_fallback = selector.content;
262-
document.head.appendChild(createStyleNode(nonce_fallback));
263-
log("Nonce was lookedup with requestAnimationFrame as MutationObserver didn't catch it.");
264-
log('Nonce found is ' + nonce_fallback);
267+
onElemAvailable('link[rel="stylesheet"][href*="custom"]').then((elem) => {
268+
if (elem.href.includes('custom.css') || elem.href.match(/custom-[a-f0-9]+\.css/)) {
269+
if (!isInjected) {
270+
if (nonce) {
271+
document.head.appendChild(createStyleNode(nonce));
272+
isInjected = true;
265273
log("(requestAnimationFrame) custom.css detected, injected TangerineUI's css asap");
266-
});
274+
}
275+
else {
276+
isInjected = true;
277+
onElemAvailable('meta[name^=style\\-nonce]').then((selector) => {
278+
let nonce_fallback = selector.content;
279+
document.head.appendChild(createStyleNode(nonce_fallback));
280+
log("Nonce was lookedup with requestAnimationFrame as MutationObserver didn't catch it.");
281+
log('Nonce found is ' + nonce_fallback);
282+
log("(requestAnimationFrame) custom.css detected, injected TangerineUI's css asap");
283+
});
284+
}
267285
}
268286
}
269287
});

0 commit comments

Comments
 (0)