This commit is contained in:
uwaa 2025-01-10 10:29:15 +00:00
parent 75b3ed1520
commit 203b1ba676
2 changed files with 111 additions and 89 deletions

1
.gitattributes vendored Normal file
View file

@ -0,0 +1 @@
*.js eol=lf

199
rot.js
View file

@ -70,9 +70,9 @@ audio.style = "display:none;";
localStorage.audiovolume = (audio.volume = localStorage.audiovolume ? localStorage.audiovolume / 100 : 0.5) * 100; localStorage.audiovolume = (audio.volume = localStorage.audiovolume ? localStorage.audiovolume / 100 : 0.5) * 100;
function setMusic(url) { function setMusic(url) {
if (audio.src == url) if (audio.src == url || (!url && audio.src === ""))
return; return;
if (!audio.paused || audio.src != "") { if (!audio.paused || audio.src != "") {
console.log("Stopping music"); console.log("Stopping music");
audio.pause(); audio.pause();
@ -80,10 +80,11 @@ function setMusic(url) {
// Setting src to "" will cause any pending .play()s to fail. // Setting src to "" will cause any pending .play()s to fail.
audio.src = ""; audio.src = "";
} }
if (url) { if (url) {
console.log("Setting music: " + url); console.log("Setting music: " + url);
audio.src = url; audio.src = url;
playMusic();
} }
} }
@ -96,7 +97,7 @@ function playMusic() {
function setImage(url) { function setImage(url) {
if (!url) if (!url)
url = '/static/background.jpg'; url = '/static/background.jpg';
console.log("Setting background: " + url); console.log("Setting background: " + url);
document.getElementById("app-loaded").style.setProperty("--body-background-image", "url(" + url + ")"); document.getElementById("app-loaded").style.setProperty("--body-background-image", "url(" + url + ")");
} }
@ -135,117 +136,137 @@ waitUntil("#music-mute").then((box) => {
//Theme application //Theme application
function applySpecialTheme() { function applySpecialTheme() {
console.log("Applying special theme");
setTimeout(() => { setTimeout(() => {
let pageMusic = document.querySelector('meta[name="pageMusic"]')?.content || document.getElementById("pageMusic")?.getAttribute("href"); let pageMusic = document.querySelector('meta[name="pageMusic"]')?.content || document.getElementById("pageMusic")?.getAttribute("href");
setMusic(pageMusic); setMusic(pageMusic);
playMusic(); playMusic();
let pageImage = document.querySelector('meta[name="pageImage"]')?.content || document.getElementById("pageImage")?.getAttribute("href"); let pageImage = document.querySelector('meta[name="pageImage"]')?.content || document.getElementById("pageImage")?.getAttribute("href");
setImage(pageImage); setImage(pageImage);
}, 1000); }, 1000);
} }
function applyUserTheme() { function applyUserTheme() {
console.log("Applying user theme");
setMusic(null); setMusic(null);
setTimeout(() => { setTimeout(() => {
let posts = []; let ptp = findProfileThemingPost();
let pinnedPosts = document.getElementsByClassName("pin"); if (ptp) {
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) {
//Configured by post //Configured by post
let statusBody = posts[0].nextElementSibling.querySelector(".StatusBody") let musicContainer = ptp.querySelector(".audio-container")
let musicContainer = statusBody.querySelector(".audio-container")
if (musicContainer) if (musicContainer)
setMusic(rand(musicContainer.children).src); setMusic(rand(musicContainer.children).src);
else else
setMusic(null); setMusic(null);
let imageContainer = statusBody.querySelector(".image-container") let imageContainer = ptp.querySelector(".image-container")
if (imageContainer) if (imageContainer)
setImage(rand(imageContainer.getElementsByTagName("img")).src); setImage(rand(imageContainer.getElementsByTagName("img")).src);
else else
setImage(null); setImage(null);
} else { return;
let fields = [...document.getElementsByClassName("user-profile-field-name")] }
if (fields.length != 0) {
//Configured by fields let fields = [...document.getElementsByClassName("user-profile-field-name")]
let musicFields = fields.filter((x) => x.title.toLowerCase().replace(/\s+/g, '') == "music") if (fields.length != 0) {
if (musicFields.length > 0) //Configured by fields
setMusic(rand(musicFields).nextElementSibling.title); let musicFields = fields.filter((x) => x.title.toLowerCase().replace(/\s+/g, '') == "music")
else if (musicFields.length > 0)
setMusic(null); setMusic(rand(musicFields).nextElementSibling.title);
else
let imageFields = fields.filter((x) => x.title.toLowerCase().replace(/\s+/g, '') == "image") setMusic(null);
if (imageFields.length > 0)
setImage(rand(imageFields).nextElementSibling.title); let imageFields = fields.filter((x) => x.title.toLowerCase().replace(/\s+/g, '') == "image")
else if (imageFields.length > 0)
setImage(null); setImage(rand(imageFields).nextElementSibling.title);
} else
setImage(null);
return;
} }
playMusic();
}, 1000); }, 1000);
} }
//Switch-based monkey patching router bullshit function findProfileThemingPost() {
addEventListener('locationchange',(event) => { try {
let pathSpl = window.location.pathname.split("/"); let pinnedPosts = document.getElementsByClassName("pin");
switch (pathSpl.length) { if (pinnedPosts.length == 0)
case 1: return null;
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: let ptp = [...pinnedPosts].find( // I hate this. It keeps getting worse.
applySpecialTheme(); (x) => x.nextElementSibling
break; .querySelector(".StatusBody")
} .querySelector(".text")
break; .innerHTML
.replace(/(<([^>]+)>)/ig, '')
default: .search(/profile theming post/ig) != -1
applySpecialTheme(); )
break; 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"); console.log("rot.js loaded");