fixes and optimizations
This commit is contained in:
parent
350e88221f
commit
b79a921932
1 changed files with 186 additions and 191 deletions
147
rot.js
147
rot.js
|
@ -2,28 +2,7 @@
|
|||
// 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) {
|
||||
function getPromiseFromEvent(item, event) {
|
||||
return new Promise((resolve) => {
|
||||
const listener = () => {
|
||||
item.removeEventListener(event, listener);
|
||||
|
@ -31,11 +10,11 @@ function getPromiseFromEvent(item, event) {
|
|||
}
|
||||
item.addEventListener(event, listener);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// MIT Licensed
|
||||
// Author: jwilson8767
|
||||
function waitUntil(selector) {
|
||||
// MIT Licensed
|
||||
// Author: jwilson8767
|
||||
function waitUntil(selector) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const el = document.querySelector(selector);
|
||||
if (el) resolve(el);
|
||||
|
@ -51,91 +30,87 @@ function waitUntil(selector) {
|
|||
subtree: true
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function sex(sex) {
|
||||
function sex(sex) {
|
||||
return sex // Sex
|
||||
}
|
||||
}
|
||||
|
||||
function rand(list) {
|
||||
function rand(list) {
|
||||
return list ? list[list.length * Math.random() | 0] : null;
|
||||
}
|
||||
}
|
||||
|
||||
// Rot music player
|
||||
const audio = document.createElement("audio");
|
||||
audio.loop = true;
|
||||
audio.id = "user-music";
|
||||
audio.style = "display:none;";
|
||||
localStorage.audiovolume = (audio.volume = localStorage.audiovolume ? localStorage.audiovolume / 100 : 0.5) * 100;
|
||||
// Rot music player
|
||||
const audio = document.createElement("audio");
|
||||
audio.loop = true;
|
||||
audio.id = "user-music";
|
||||
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 === ""))
|
||||
function setMusic(url) {
|
||||
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);
|
||||
audio.src = url;
|
||||
playMusic();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function playMusic() {
|
||||
function playMusic() {
|
||||
//Starts playing the music if it isn't muted and isn't already playing
|
||||
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';
|
||||
}
|
||||
|
||||
function setImage(url) {
|
||||
if (url) {
|
||||
console.log("Setting background: " + url);
|
||||
document.getElementById("app-loaded").style.setProperty("--body-background-image", "url(" + url + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Audio control events
|
||||
function volumeSet(number) {
|
||||
//Audio control events
|
||||
function volumeSet(number) {
|
||||
localStorage.audiovolume = Math.round((audio.volume = number) * 100);
|
||||
updateVolumeLabel();
|
||||
}
|
||||
function volumeAdd(number) {
|
||||
}
|
||||
function volumeAdd(number) {
|
||||
volumeSet(Math.min(1, Math.max(0, audio.volume + number)));
|
||||
}
|
||||
function updateVolumeLabel() {
|
||||
}
|
||||
function updateVolumeLabel() {
|
||||
document.getElementById("user-audio-percentage").innerHTML = Math.round(audio.volume * 100) + "%";
|
||||
}
|
||||
}
|
||||
|
||||
//Initialize audio controls
|
||||
waitUntil("#music-controls").then((controls) => {
|
||||
//Initialize audio controls
|
||||
waitUntil("#music-controls").then((controls) => {
|
||||
updateVolumeLabel();
|
||||
controls.querySelector("#music-up").onclick = () => volumeAdd(0.05);
|
||||
controls.querySelector("#music-down").onclick = () => volumeAdd(-0.05);
|
||||
})
|
||||
})
|
||||
|
||||
waitUntil("#music-slider").then((slider) => {
|
||||
waitUntil("#music-slider").then((slider) => {
|
||||
updateVolumeLabel();
|
||||
slider.oninput = () => volumeSet(slider.value / 100);
|
||||
})
|
||||
})
|
||||
|
||||
waitUntil("#music-mute").then((box) => {
|
||||
waitUntil("#music-mute").then((box) => {
|
||||
audio.muted = box.checked = localStorage.audiomuted === "true";
|
||||
box.addEventListener('click', () => {
|
||||
localStorage.audiomuted = audio.muted = box.checked;
|
||||
playMusic();
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
//Theme application
|
||||
function applyMainTheme() {
|
||||
//Theme application
|
||||
function applyMainTheme() {
|
||||
console.log("Applying main theme");
|
||||
setTimeout(() => {
|
||||
let pageMusic = document.querySelector('meta[name="pageMusic"]')?.content || document.getElementById("pageMusic")?.getAttribute("href");
|
||||
|
@ -145,9 +120,9 @@ function applyMainTheme() {
|
|||
let pageImage = document.querySelector('meta[name="pageImage"]')?.content || document.getElementById("pageImage")?.getAttribute("href");
|
||||
setImage(pageImage);
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
function applyUserTheme() {
|
||||
function applyUserTheme() {
|
||||
console.log("Applying user theme");
|
||||
setMusic(null);
|
||||
setTimeout(() => {
|
||||
|
@ -185,9 +160,9 @@ function applyUserTheme() {
|
|||
return;
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
function findProfileThemingPost() {
|
||||
function findProfileThemingPost() {
|
||||
try {
|
||||
let pinnedPosts = document.getElementsByClassName("pin");
|
||||
if (pinnedPosts.length == 0)
|
||||
|
@ -210,12 +185,11 @@ function findProfileThemingPost() {
|
|||
console.error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Switch-based monkey patching router bullshit
|
||||
{
|
||||
let lastPath = window.location.pathname;
|
||||
addEventListener('locationchange', (event) => {
|
||||
//Switch-based monkey patching router bullshit
|
||||
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");
|
Loading…
Reference in a new issue