#7 Added mutation observer

This commit is contained in:
Serraniel 2020-07-29 10:35:57 +02:00
parent d522f7c96f
commit 3de1e3443a
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3
5 changed files with 58 additions and 61 deletions

View file

@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}

View file

@ -1,29 +1,24 @@
const starIcon = "star";
registerScript(() => {
registerScript(node => {
// run the scripts
handleListAfterLoad();
// because of late loading in the request list we have to run the codes each time the list changes
//document.querySelector("md-list").addEventListener("DOMNodeInserted", event => handleListAfterLoad(event), false);
if (isHtmlElement(node)) {
changeFollowedStarColor(node);
changeOwnBorderColor(node);
}
});
function handleListAfterLoad() {
changeFollowedStarColor();
changeOwnBorderColor();
}
function changeFollowedStarColor() {
function changeFollowedStarColor(node) {
// find stars
let followedItems = Array.from(document.querySelectorAll("i")).filter(i => i.innerText == starIcon);
let followedItems = Array.from(node.querySelectorAll("i")).filter(i => i.innerText == starIcon);
// change color
followedItems.forEach(item => item.style.color = aniBlue);
}
function changeOwnBorderColor() {
function changeOwnBorderColor(node) {
// find items -> all
let requestItems = document.querySelectorAll("md-list-item");
let requestItems = node.querySelectorAll("md-list-item");
// change border color if profile link is not "false"
requestItems.forEach(item => {

View file

@ -1,29 +1,29 @@
{
"name": "Enhanced Aniwatch",
"version": "0.1.0.0",
"description": "Contains several enhancments for https://aniwatch.me",
"manifest_version": 2,
"author": "Serraniel",
"homepage_url": "https://github.com/Serraniel/EnhancedAniwatch",
"content_scripts": [
{
"matches": [
"*://aniwatch.me/*"
],
"js": [
"utils/colors.js",
"utils/aniwatchCore.js"
],
"run_at": "document_start"
},
{
"matches": [
"*://aniwatch.me/*"
],
"js": [
"enhancements/animeRequests.js"
],
"run_at": "document_end"
}
]
"name": "Enhanced Aniwatch",
"version": "0.1.0.0",
"description": "Contains several enhancments for https://aniwatch.me",
"manifest_version": 2,
"author": "Serraniel",
"homepage_url": "https://github.com/Serraniel/EnhancedAniwatch",
"content_scripts": [{
"matches": [
"*://aniwatch.me/*"
],
"js": [
"utils/colors.js",
"utils/helpers.js",
"utils/aniwatchCore.js"
],
"run_at": "document_start"
},
{
"matches": [
"*://aniwatch.me/*"
],
"js": [
"enhancements/animeRequests.js"
],
"run_at": "document_end"
}
]
}

View file

@ -1,27 +1,18 @@
let __scripts = [];
function registerScript(func){
function registerScript(func) {
__scripts.push(func);
}
function runScripts(){
console.log("RUN");
__scripts.forEach(script => script());
}
function awaitPageLoaded(){
let preLoader = document.getElementById('preloader');
let loop = setInterval(() => {
if(preLoader.style.display==="none"){
clearInterval(loop);
runScripts();
let observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
for (let i = 0; i < mutation.addedNodes.length; i++) {
__scripts.forEach(script => script(mutation.addedNodes[i]));
}
}, 100);
}
});
});
// RUN AT INITIALIZATION
window.addEventListener("hashchange", event => runScripts(), false);
document.addEventListener("DOMContentLoaded", event => awaitPageLoaded(), false);
document.querySelector('.main-section').
observer.observe(document.documentElement || document.body, {
childList: true,
subtree: true
});

3
utils/helpers.js Normal file
View file

@ -0,0 +1,3 @@
function isHtmlElement(object) {
return object instanceof HTMLElement;
}