Skip to content

Commit

Permalink
launch script when new DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
Poucous committed Jun 13, 2022
1 parent 44d5c8d commit 76d9cac
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Firefox/content_scripts/updatePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
function isClassSmartReader(elementsToAnalyse, ourClass) {

/* Check a parent class going down
Args:
elementsToAnalyse: elements to analyse
ourClass: class to search
Returns:
bool: if we found the class or not
*/

for(var i = 0 ; i < elementsToAnalyse.children.length ; i++) {

if(elementsToAnalyse.children[i].className === ourClass) {
return true;
}
}

return false;
}

function watchPageUpdates() {

var observePageUpdates = new MutationObserver(updatePage);
var container = document.body;
var config = {childList: true, subtree:true };

browser.runtime.sendMessage({mode: "state"}, function(message) {

if(message.mode === "enable") {

observePageUpdates.observe(container, config);

} else {

observePageUpdates.disconnect();
}
});

}

function updatePage(mutationList, observer) {
/* Update the page when a new DOM appears
*/

observer.disconnect();

var allElements = [];
var tempElements = [];
var finalElements = [];

//Grouping
assembleElements(document.body.getElementsByTagName("P"), allElements);
assembleElements(document.body.getElementsByTagName("SPAN"), allElements);
assembleElements(document.body.getElementsByTagName("DIV"), allElements);
assembleElements(document.body.getElementsByTagName("BR"), allElements);
assembleElements(document.body.getElementsByTagName("BLOCKQUOTE"), allElements);
assembleElements(document.body.getElementsByTagName("EM"), allElements);

filterElements(allElements, tempElements);

for(var i = 0 ; i < tempElements.length ; i++) {

if(isClassSmartReader(tempElements[i], "smartReader-remove") === false && tempElements[i].className !== "smartReader-remove" && tempElements[i].parentNode.className !== "smartReader-remove") {

finalElements.push(tempElements[i]);
}
}

modifyHtml(finalElements);

watchPageUpdates();
}
1 change: 1 addition & 0 deletions Firefox/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function main() {

modifyHtml(finalElements);

watchPageUpdates();
}

//Is addon actived => launch the main function on the page
Expand Down
1 change: 1 addition & 0 deletions Firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"/content_scripts/textAtRoot.js",
"/content_scripts/isAddonActived.js",
"/content_scripts/isItLetter.js",
"/content_scripts/updatePage.js",
"main.js"
]
}
Expand Down

0 comments on commit 76d9cac

Please sign in to comment.