fixes and optimizations

This commit is contained in:
uwaa 2025-01-10 10:52:08 +00:00
parent 350e88221f
commit b79a921932

59
rot.js
View file

@ -2,27 +2,6 @@
// one rotten apple spoils the bunch
(() => {
let oldPushState = history.pushState;
history.pushState = function pushState() {
let ret = oldPushState.apply(this, arguments);
window.dispatchEvent(new Event('pushstate'));
window.dispatchEvent(new Event('locationchange'));
return ret;
};
let oldReplaceState = history.replaceState;
history.replaceState = function replaceState() {
let ret = oldReplaceState.apply(this, arguments);
window.dispatchEvent(new Event('replacestate'));
window.dispatchEvent(new Event('locationchange'));
return ret;
};
window.addEventListener('popstate', () => {
window.dispatchEvent(new Event('locationchange'));
});
})();
function getPromiseFromEvent(item, event) {
return new Promise((resolve) => {
const listener = () => {
@ -70,16 +49,13 @@ audio.style = "display:none;";
localStorage.audiovolume = (audio.volume = localStorage.audiovolume ? localStorage.audiovolume / 100 : 0.5) * 100;
function setMusic(url) {
if (audio.src == url || (!url && audio.src === ""))
if (audio.src == url || (!url && audio.src === window.location.toString()))
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);
@ -95,12 +71,11 @@ function playMusic() {
}
function setImage(url) {
if (!url)
url = '/static/background.jpg';
if (url) {
console.log("Setting background: " + url);
document.getElementById("app-loaded").style.setProperty("--body-background-image", "url(" + url + ")");
}
}
//Audio control events
function volumeSet(number) {
@ -213,9 +188,8 @@ function findProfileThemingPost() {
}
//Switch-based monkey patching router bullshit
{
let lastPath = window.location.pathname;
addEventListener('locationchange', (event) => {
let lastPath = null;
function updateRot() {
let newPath = window.location.pathname;
if (lastPath == newPath)
return;
@ -266,7 +240,28 @@ function findProfileThemingPost() {
applyMainTheme();
break;
}
});
}
//Monkey patches
addEventListener('locationchange', updateRot);
const oldPushState = history.pushState;
history.pushState = function pushState() {
const ret = oldPushState.apply(this, arguments);
updateRot();
return ret;
};
const oldReplaceState = history.replaceState;
history.replaceState = function replaceState() {
const ret = oldReplaceState.apply(this, arguments);
updateRot();
return ret;
};
window.addEventListener('popstate', () => {
updateRot();
});
})();
console.log("rot.js loaded");