diff --git a/Firefox/content_scripts/updatePage.js b/Firefox/content_scripts/updatePage.js new file mode 100644 index 0000000..6ac0afb --- /dev/null +++ b/Firefox/content_scripts/updatePage.js @@ -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(); +} \ No newline at end of file diff --git a/Firefox/main.js b/Firefox/main.js index 13105ac..aa54858 100644 --- a/Firefox/main.js +++ b/Firefox/main.js @@ -25,6 +25,7 @@ function main() { modifyHtml(finalElements); + watchPageUpdates(); } //Is addon actived => launch the main function on the page diff --git a/Firefox/manifest.json b/Firefox/manifest.json index bdfacca..4a9e305 100644 --- a/Firefox/manifest.json +++ b/Firefox/manifest.json @@ -35,6 +35,7 @@ "/content_scripts/textAtRoot.js", "/content_scripts/isAddonActived.js", "/content_scripts/isItLetter.js", + "/content_scripts/updatePage.js", "main.js" ] }