From d522f7c96f1dbf1fc81babc30e687f9b5434770a Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 29 Jul 2020 10:03:25 +0200 Subject: [PATCH 1/4] =?UTF-8?q?Old=20codes=20I=20do=20not=20remember=20but?= =?UTF-8?q?=20better=20commit=20them=20than=20loosing=20it=20=F0=9F=98=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- enhancements/animeRequests.js | 19 +++++++------------ manifest.json | 2 +- utils/aniwatchCore.js | 23 ++++++++++++++++++++--- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/enhancements/animeRequests.js b/enhancements/animeRequests.js index f8cb9c3..c89cb0e 100644 --- a/enhancements/animeRequests.js +++ b/enhancements/animeRequests.js @@ -1,21 +1,16 @@ const starIcon = "star"; -const scripts = [ - changeFollowedStarColor, - changeOwnBorderColor, -] -executeAfterPreload(initScripts); - -function initScripts() { +registerScript(() => { // run the scripts - runScripts(); + 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 => runScripts(event), false); -} + //document.querySelector("md-list").addEventListener("DOMNodeInserted", event => handleListAfterLoad(event), false); +}); -function runScripts() { - scripts.forEach(script => script()); +function handleListAfterLoad() { + changeFollowedStarColor(); + changeOwnBorderColor(); } function changeFollowedStarColor() { diff --git a/manifest.json b/manifest.json index d4623e9..3eda9fe 100644 --- a/manifest.json +++ b/manifest.json @@ -18,7 +18,7 @@ }, { "matches": [ - "*://aniwatch.me/requests" + "*://aniwatch.me/*" ], "js": [ "enhancements/animeRequests.js" diff --git a/utils/aniwatchCore.js b/utils/aniwatchCore.js index 263247a..b0e9ba4 100644 --- a/utils/aniwatchCore.js +++ b/utils/aniwatchCore.js @@ -1,10 +1,27 @@ -function executeAfterPreload(func){ +let __scripts = []; + +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); - func(); + runScripts(); } }, 100); -} \ No newline at end of file +} + +// RUN AT INITIALIZATION +window.addEventListener("hashchange", event => runScripts(), false); +document.addEventListener("DOMContentLoaded", event => awaitPageLoaded(), false); + +document.querySelector('.main-section'). \ No newline at end of file -- 2.43.5 From 3de1e3443a7ab91a7b78d021156a68c9ae691b3e Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 29 Jul 2020 10:35:57 +0200 Subject: [PATCH 2/4] #7 Added mutation observer --- enhanced-aniwatch.code-workspace | 8 +++++ enhancements/animeRequests.js | 23 ++++++-------- manifest.json | 54 ++++++++++++++++---------------- utils/aniwatchCore.js | 31 +++++++----------- utils/helpers.js | 3 ++ 5 files changed, 58 insertions(+), 61 deletions(-) create mode 100644 enhanced-aniwatch.code-workspace create mode 100644 utils/helpers.js diff --git a/enhanced-aniwatch.code-workspace b/enhanced-aniwatch.code-workspace new file mode 100644 index 0000000..876a149 --- /dev/null +++ b/enhanced-aniwatch.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file diff --git a/enhancements/animeRequests.js b/enhancements/animeRequests.js index c89cb0e..861b7b1 100644 --- a/enhancements/animeRequests.js +++ b/enhancements/animeRequests.js @@ -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 => { diff --git a/manifest.json b/manifest.json index 3eda9fe..7a8508d 100644 --- a/manifest.json +++ b/manifest.json @@ -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" + } + ] } \ No newline at end of file diff --git a/utils/aniwatchCore.js b/utils/aniwatchCore.js index b0e9ba4..81a0c13 100644 --- a/utils/aniwatchCore.js +++ b/utils/aniwatchCore.js @@ -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'). \ No newline at end of file +observer.observe(document.documentElement || document.body, { + childList: true, + subtree: true +}); \ No newline at end of file diff --git a/utils/helpers.js b/utils/helpers.js new file mode 100644 index 0000000..cb18055 --- /dev/null +++ b/utils/helpers.js @@ -0,0 +1,3 @@ +function isHtmlElement(object) { + return object instanceof HTMLElement; +} \ No newline at end of file -- 2.43.5 From 3b145e2d4e1cd64965f635d1ef9c53962c998bb0 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 29 Jul 2020 10:44:03 +0200 Subject: [PATCH 3/4] Some fixes for requests Page --- enhancements/animeRequests.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/enhancements/animeRequests.js b/enhancements/animeRequests.js index 861b7b1..1ae0126 100644 --- a/enhancements/animeRequests.js +++ b/enhancements/animeRequests.js @@ -17,15 +17,26 @@ function changeFollowedStarColor(node) { } function changeOwnBorderColor(node) { - // find items -> all - let requestItems = node.querySelectorAll("md-list-item"); + const targetTagName = "MD-LIST-ITEM"; // tagName is upper case - // change border color if profile link is not "false" - requestItems.forEach(item => { + let updateFunc = item => { let profileLink = item.querySelectorAll("a[href*='/profile/']:not([href='/profile/false'])"); if (profileLink.length > 0) { item.style.borderColor = aniBlue } - }); + } + + // are we target tag? + if (node.tagName === targetTagName) { + updateFunc(node); + } else { + // find items -> all + let requestItems = node.querySelectorAll("md-list-item"); + + // change border color if profile link is not "false" + requestItems.forEach(item => { + updateFunc(item); + }); + } } \ No newline at end of file -- 2.43.5 From 3d309ae88ae43584278fca3e6a58c29d62c92f53 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 29 Jul 2020 11:14:38 +0200 Subject: [PATCH 4/4] #7 Fixes in core and star coloring --- enhancements/animeRequests.js | 6 +++--- utils/aniwatchCore.js | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/enhancements/animeRequests.js b/enhancements/animeRequests.js index 1ae0126..d51f342 100644 --- a/enhancements/animeRequests.js +++ b/enhancements/animeRequests.js @@ -1,5 +1,3 @@ -const starIcon = "star"; - registerScript(node => { // run the scripts if (isHtmlElement(node)) { @@ -9,8 +7,10 @@ registerScript(node => { }); function changeFollowedStarColor(node) { + const starIcon = "star"; + // find stars - let followedItems = Array.from(node.querySelectorAll("i")).filter(i => i.innerText == starIcon); + let followedItems = Array.from(node.querySelectorAll("i")).filter(i => i.innerText.trim() === starIcon); // change color followedItems.forEach(item => item.style.color = aniBlue); diff --git a/utils/aniwatchCore.js b/utils/aniwatchCore.js index 81a0c13..e274d01 100644 --- a/utils/aniwatchCore.js +++ b/utils/aniwatchCore.js @@ -4,15 +4,20 @@ function registerScript(func) { __scripts.push(func); } +function runScripts(node) { + __scripts.forEach(script => script(node)); +} + let observer = new MutationObserver(mutations => { mutations.forEach(mutation => { for (let i = 0; i < mutation.addedNodes.length; i++) { - __scripts.forEach(script => script(mutation.addedNodes[i])); + runScripts(mutation.addedNodes[i]); } }); }); observer.observe(document.documentElement || document.body, { childList: true, - subtree: true + subtree: true, + attributes: true }); \ No newline at end of file -- 2.43.5