From 203b1ba676c8d8b6d623fafcbf04d41ed7dfb452 Mon Sep 17 00:00:00 2001 From: uwaa Date: Fri, 10 Jan 2025 10:29:15 +0000 Subject: [PATCH] optimize --- .gitattributes | 1 + rot.js | 199 +++++++++++++++++++++++++++---------------------- 2 files changed, 111 insertions(+), 89 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..79d93c8 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.js eol=lf \ No newline at end of file diff --git a/rot.js b/rot.js index a1c8015..4876210 100644 --- a/rot.js +++ b/rot.js @@ -70,9 +70,9 @@ audio.style = "display:none;"; localStorage.audiovolume = (audio.volume = localStorage.audiovolume ? localStorage.audiovolume / 100 : 0.5) * 100; function setMusic(url) { - if (audio.src == url) + if (audio.src == url || (!url && audio.src === "")) return; - + if (!audio.paused || audio.src != "") { console.log("Stopping music"); audio.pause(); @@ -80,10 +80,11 @@ function setMusic(url) { // Setting src to "" will cause any pending .play()s to fail. audio.src = ""; } - + if (url) { console.log("Setting music: " + url); audio.src = url; + playMusic(); } } @@ -96,7 +97,7 @@ function playMusic() { function setImage(url) { if (!url) url = '/static/background.jpg'; - + console.log("Setting background: " + url); document.getElementById("app-loaded").style.setProperty("--body-background-image", "url(" + url + ")"); } @@ -135,117 +136,137 @@ waitUntil("#music-mute").then((box) => { //Theme application function applySpecialTheme() { + console.log("Applying special theme"); setTimeout(() => { let pageMusic = document.querySelector('meta[name="pageMusic"]')?.content || document.getElementById("pageMusic")?.getAttribute("href"); setMusic(pageMusic); playMusic(); - + let pageImage = document.querySelector('meta[name="pageImage"]')?.content || document.getElementById("pageImage")?.getAttribute("href"); setImage(pageImage); }, 1000); } function applyUserTheme() { + console.log("Applying user theme"); setMusic(null); setTimeout(() => { - let posts = []; - let pinnedPosts = document.getElementsByClassName("pin"); - if (pinnedPosts.length != 0) { - posts = [...pinnedPosts] - posts = posts.filter( // I hate this. It keeps getting worse. - (x) => x.nextElementSibling - .querySelector(".StatusBody") - .querySelector(".text") - .innerHTML - .toLowerCase() - .replace( /(<([^>]+)>)/ig, '') - .search(/profile theming post/ig) != -1 - ) - } - - if (posts.length != 0) { + let ptp = findProfileThemingPost(); + if (ptp) { //Configured by post - let statusBody = posts[0].nextElementSibling.querySelector(".StatusBody") - - let musicContainer = statusBody.querySelector(".audio-container") + let musicContainer = ptp.querySelector(".audio-container") if (musicContainer) setMusic(rand(musicContainer.children).src); else setMusic(null); - - let imageContainer = statusBody.querySelector(".image-container") + + let imageContainer = ptp.querySelector(".image-container") if (imageContainer) setImage(rand(imageContainer.getElementsByTagName("img")).src); else setImage(null); - } else { - let fields = [...document.getElementsByClassName("user-profile-field-name")] - if (fields.length != 0) { - //Configured by fields - let musicFields = fields.filter((x) => x.title.toLowerCase().replace(/\s+/g, '') == "music") - if (musicFields.length > 0) - setMusic(rand(musicFields).nextElementSibling.title); - else - setMusic(null); - - let imageFields = fields.filter((x) => x.title.toLowerCase().replace(/\s+/g, '') == "image") - if (imageFields.length > 0) - setImage(rand(imageFields).nextElementSibling.title); - else - setImage(null); - } + return; + } + + let fields = [...document.getElementsByClassName("user-profile-field-name")] + if (fields.length != 0) { + //Configured by fields + let musicFields = fields.filter((x) => x.title.toLowerCase().replace(/\s+/g, '') == "music") + if (musicFields.length > 0) + setMusic(rand(musicFields).nextElementSibling.title); + else + setMusic(null); + + let imageFields = fields.filter((x) => x.title.toLowerCase().replace(/\s+/g, '') == "image") + if (imageFields.length > 0) + setImage(rand(imageFields).nextElementSibling.title); + else + setImage(null); + return; } - playMusic(); }, 1000); } -//Switch-based monkey patching router bullshit -addEventListener('locationchange',(event) => { - let pathSpl = window.location.pathname.split("/"); - switch (pathSpl.length) { - case 1: - applySpecialTheme(); //Root - break; - - case 2: - switch (pathSpl[1]) { - case "about": - case "announcements": - case "lists": - case "bookmarks": - applySpecialTheme(); - break; - - default: - applyUserTheme(); - break; - } - break; - - case 3: - switch (pathSpl[1]) { - case "main": - applySpecialTheme(); //Main timelines - break; - - case "users": - applyUserTheme(); - break; - - case "notice": - //Continue playing - break; +function findProfileThemingPost() { + try { + let pinnedPosts = document.getElementsByClassName("pin"); + if (pinnedPosts.length == 0) + return null; - default: - applySpecialTheme(); - break; - } - break; - - default: - applySpecialTheme(); - break; + let ptp = [...pinnedPosts].find( // I hate this. It keeps getting worse. + (x) => x.nextElementSibling + .querySelector(".StatusBody") + .querySelector(".text") + .innerHTML + .replace(/(<([^>]+)>)/ig, '') + .search(/profile theming post/ig) != -1 + ) + if (!ptp) + return null; + + return ptp.nextElementSibling.querySelector(".StatusBody"); + } catch (e) { + //Future-proofing + console.error(e); + return null; } -}); +} + +//Switch-based monkey patching router bullshit +{ + let lastPath = window.location.pathname; + addEventListener('locationchange', (event) => { + let newPath = window.location.pathname; + if (lastPath == newPath) + return; + lastPath = newPath; + + let pathSpl = newPath.split("/"); + switch (pathSpl.length) { + case 1: + applySpecialTheme(); //Root + break; + + case 2: + switch (pathSpl[1]) { + case "about": + case "announcements": + case "lists": + case "bookmarks": + applySpecialTheme(); + break; + + default: + applyUserTheme(); + break; + } + break; + + case 3: + switch (pathSpl[1]) { + case "main": + applySpecialTheme(); //Main timelines + break; + + case "users": + applyUserTheme(); + break; + + case "notice": + //Continue playing + break; + + default: + applySpecialTheme(); + break; + } + break; + + default: + applySpecialTheme(); + break; + } + }); +} console.log("rot.js loaded"); \ No newline at end of file