This commit is contained in:
uwaa 2025-01-10 08:23:07 +00:00
parent 01cfdb1540
commit 5718d09d74

305
rot.js
View file

@ -24,34 +24,33 @@
})(); })();
function getPromiseFromEvent(item, event) { function getPromiseFromEvent(item, event) {
return new Promise((resolve) => { return new Promise((resolve) => {
const listener = () => { const listener = () => {
item.removeEventListener(event, listener); item.removeEventListener(event, listener);
resolve(); resolve();
} }
item.addEventListener(event, listener); item.addEventListener(event, listener);
}) });
} }
// MIT Licensed // MIT Licensed
// Author: jwilson8767 // Author: jwilson8767
function waitUntil(selector) { function waitUntil(selector) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const el = document.querySelector(selector); const el = document.querySelector(selector);
if (el) {resolve(el);} if (el) resolve(el);
new MutationObserver((mutationRecords, observer) => { new MutationObserver((mutationRecords, observer) => {
// Query for elements matching the specified selector // Query for elements matching the specified selector
Array.from(document.querySelectorAll(selector)).forEach((element) => { Array.from(document.querySelectorAll(selector)).forEach((element) => {
resolve(element); resolve(element);
//Once we have resolved we don't need the observer anymore. //Once we have resolved we don't need the observer anymore.
observer.disconnect(); observer.disconnect();
}); });
}) }).observe(document.documentElement, {
.observe(document.documentElement, { childList: true,
childList: true, subtree: true
subtree: true });
}); });
});
} }
@ -60,121 +59,191 @@ function sex(sex) {
} }
function rand(list) { function rand(list) {
return list == undefined ? undefined : list[list.length*Math.random()|0] return list ? list[list.length * Math.random() | 0] : null;
} }
function audioplay(obj) { // Rot music player
obj.play().catch( const audio = document.createElement("audio");
()=>{ audio.loop = true;
getPromiseFromEvent(window,'click').then( audio.id = "user-music";
()=>{audio.play()} audio.style = "display:none;";
) localStorage.audiovolume = (audio.volume = localStorage.audiovolume ? localStorage.audiovolume / 100 : 0.5) * 100;
}
) function setMusic(url) {
if (audio.src == url)
return;
if (!audio.paused || audio.src != "") {
console.log("Stopping music");
audio.pause();
// This line will cause a lot of errors.
// Setting src to "" will cause any pending .play()s to fail.
audio.src = "";
}
if (url) {
console.log("Setting music: " + url);
audio.src = url;
}
} }
function volumeChange (number) { function playMusic() {
localStorage.audiovolume = audio.volume = Math.round(audio.volume * 100 + (number%101))/100 //Starts playing the music if it isn't muted and isn't already playing
document.getElementById("user-audio-percentage").innerHTML = Math.floor(audio.volume * 100) + "%" if (audio.src && audio.src != "" && audio.paused && !audio.muted)
audio.play().catch(() => getPromiseFromEvent(window, 'click').then(audio.play));
} }
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 + ")");
}
//Audio control events
function volumeSet(number) { function volumeSet(number) {
localStorage.audiovolume = audio.volume = Math.round((number%101))/100 localStorage.audiovolume = Math.round((audio.volume = number) * 100);
document.getElementById("user-audio-percentage").innerHTML = Math.floor(audio.volume * 100) + "%" updateVolumeLabel();
}
function volumeAdd(number) {
volumeSet(Math.min(1, Math.max(0, audio.volume + number)));
}
function updateVolumeLabel() {
document.getElementById("user-audio-percentage").innerHTML = Math.round(audio.volume * 100) + "%";
} }
// Add rot music. //Initialize audio controls
audio = document.createElement("audio") waitUntil("#music-controls").then((controls) => {
audio.loop = true updateVolumeLabel();
audio.id = "user-music" controls.querySelector("#music-up").onclick = () => volumeAdd(0.05);
audio.style = "display:none;" controls.querySelector("#music-down").onclick = () => volumeAdd(-0.05);
localStorage.audiovolume = audio.volume = localStorage.audiovolume ? localStorage.audiovolume : 0.5
// Rot player!
waitUntil("#music-controls").then ( (controls) => {
document.getElementById("user-audio-percentage").innerHTML = Math.floor(localStorage.audiovolume * 100) + "%"
controls.querySelector("#music-up").onclick = () => volumeChange(5)
controls.querySelector("#music-down").onclick = () => volumeChange(-5)
}) })
waitUntil("#music-slider").then ( (slider) => { waitUntil("#music-slider").then((slider) => {
document.getElementById("user-audio-percentage").innerHTML = (slider.value = Math.floor(localStorage.audiovolume * 100)) + "%" updateVolumeLabel();
slider.oninput = () => volumeSet(slider.value) slider.oninput = () => volumeSet(slider.value / 100);
}) })
waitUntil("#music-mute").then ( (box) => { waitUntil("#music-mute").then((box) => {
audio.muted = box.checked = localStorage.audiomuted === "true" audio.muted = box.checked = localStorage.audiomuted === "true";
box.addEventListener('click', () => { box.addEventListener('click', () => {
localStorage.audiomuted = audio.muted = box.checked localStorage.audiomuted = audio.muted = box.checked;
playMusic();
}) })
}); });
let prev; //Theme application
let choice; function applySpecialTheme() {
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);
}
console.log("rot custom js loaded") function applyUserTheme() {
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) {
//Configured by post
let statusBody = posts[0].nextElementSibling.querySelector(".StatusBody")
let musicContainer = statusBody.querySelector(".audio-container")
if (musicContainer)
setMusic(rand(musicContainer.children).src);
else
setMusic(null);
let imageContainer = statusBody.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);
}
}
playMusic();
}, 1000);
}
//Switch-based monkey patching router bullshit
addEventListener('locationchange',(event) => { addEventListener('locationchange',(event) => {
f = window.location.toString().split("/").reverse(); let pathSpl = window.location.pathname.split("/");
prev = choice switch (pathSpl.length) {
choice = f[0].split('?')[0].split('#')[0] case 1:
switch (choice) { applySpecialTheme(); //Root
case "about": break;
//audio.src = document.getElementById("pageMusic").getAttribute("href")
//console.log("audio sources set to " + audio.src) case 2:
audio.src = "https://rape.pet/music/ItsFunny.mp3"; switch (pathSpl[1]) {
audioplay(audio) case "about":
break; case "announcements":
default: case "lists":
if (prev == choice) return; case "bookmarks":
audio.pause() applySpecialTheme();
// This line will cause a lot of errors. break;
// Setting src to "" will cause any pending .play()s ot fail.
audio.src = "" default:
setTimeout( () => { applyUserTheme();
posts = [] break;
if (document.getElementsByClassName("pin").length != 0) {
posts = [...document.getElementsByClassName("pin")]
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) {
base = posts[0].nextElementSibling.querySelector(".StatusBody")
music = base.querySelector(".audio-container")
image = base.querySelector(".image-container")
if (music) {
audio.src = rand(music.children).src
audio.id = "user-music"
audioplay(audio)
}
if (image) {
image = "url(" + rand(image.getElementsByTagName("img")).src + ")"
document.getElementById("app-loaded").style.setProperty("--body-background-image",image)
}
} else if (document.getElementsByClassName("user-profile-field").length != 0) {
// Music setup.
fields = document.getElementsByClassName("user-profile-field-name")
music = [...fields].filter( (x)=>x.title.toLowerCase().replace(/\s+/g, '')=="music" )
if (music.length !=0) {
music = rand(music).nextElementSibling.title
audio.src = music
audio.id = "user-music"
audioplay(audio)
}
// Image setup. Unneeded. Pleroma sets it for you.
//if (default_bg == undefined) {
// default_bg = document.getElementById("app-loaded").style.getPropertyValue("--body-background-image")
//}
image = [...fields].filter( (x)=>x.title.toLowerCase().replace(/\s+/g, '')=="image" )
if (image.length != 0) {
image = image.at(0).nextElementSibling.title
image = "url(" + image + ")"
document.getElementById("app-loaded").style.setProperty("--body-background-image",image)
}
} }
break;
case 3:
switch (pathSpl[1]) {
case "main":
applySpecialTheme(); //Main timelines
break;
case "users":
applyUserTheme();
break;
case "notice":
//Continue playing
break;
}, 1000); default:
break; applySpecialTheme();
break;
}
break;
default:
applySpecialTheme();
break;
} }
}); });
console.log("rot.js loaded");