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;
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");