optimize
This commit is contained in:
parent
75b3ed1520
commit
203b1ba676
2 changed files with 111 additions and 89 deletions
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.js eol=lf
|
75
rot.js
75
rot.js
|
@ -70,7 +70,7 @@ 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 != "") {
|
||||
|
@ -84,6 +84,7 @@ function setMusic(url) {
|
|||
if (url) {
|
||||
console.log("Setting music: " + url);
|
||||
audio.src = url;
|
||||
playMusic();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,6 +136,7 @@ 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);
|
||||
|
@ -146,39 +148,26 @@ function applySpecialTheme() {
|
|||
}
|
||||
|
||||
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 {
|
||||
return;
|
||||
}
|
||||
|
||||
let fields = [...document.getElementsByClassName("user-profile-field-name")]
|
||||
if (fields.length != 0) {
|
||||
//Configured by fields
|
||||
|
@ -193,15 +182,46 @@ function applyUserTheme() {
|
|||
setImage(rand(imageFields).nextElementSibling.title);
|
||||
else
|
||||
setImage(null);
|
||||
return;
|
||||
}
|
||||
}
|
||||
playMusic();
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
function findProfileThemingPost() {
|
||||
try {
|
||||
let pinnedPosts = document.getElementsByClassName("pin");
|
||||
if (pinnedPosts.length == 0)
|
||||
return null;
|
||||
|
||||
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
|
||||
addEventListener('locationchange',(event) => {
|
||||
let pathSpl = window.location.pathname.split("/");
|
||||
{
|
||||
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
|
||||
|
@ -246,6 +266,7 @@ addEventListener('locationchange',(event) => {
|
|||
applySpecialTheme();
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
console.log("rot.js loaded");
|
Loading…
Reference in a new issue