#7 Added mutation observer
This commit is contained in:
parent
d522f7c96f
commit
3de1e3443a
8
enhanced-aniwatch.code-workspace
Normal file
8
enhanced-aniwatch.code-workspace
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"folders": [
|
||||
{
|
||||
"path": "."
|
||||
}
|
||||
],
|
||||
"settings": {}
|
||||
}
|
|
@ -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 => {
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -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
3
utils/helpers.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
function isHtmlElement(object) {
|
||||
return object instanceof HTMLElement;
|
||||
}
|
Loading…
Reference in a new issue