diff --git a/background.js b/background.js new file mode 100644 index 0000000..e69de29 diff --git a/content.js b/content.js new file mode 100644 index 0000000..0f62801 --- /dev/null +++ b/content.js @@ -0,0 +1,106 @@ +const body = document.body; + +const bannedWord = "*Banned by StopSlogan*"; + +const slogans = {}; +let slogansKeys = []; +const bannedSlogans = []; + +// const url = "https://en.wikipedia.org/wiki/List_of_political_slogans"; +const url = + "https://en.wikipedia.org/w/api.php?" + + new URLSearchParams({ + origin: "*", + + action: "parse", + page: "List_of_political_slogans", + format: "json" + }); +//en.wikipedia.org/w/api.php?action=parse&format=json&origin=*&page=Most%20common%20words%20in%20Spanish' + +function parseNode(startNode) { + if (startNode.tagName == "LI") { + const key = startNode.innerText.toLowerCase(); + slogans[key] = true; + slogansKeys.push(key); + } + startNode.childNodes.forEach(node => { + parseNode(node); + }); +} + +const callback = function(mutationsList, observer) { + // Use traditional 'for loops' for IE 11 + for (let mutation of mutationsList) { + if (mutation.type === "childList") { + const node = mutation.target; + checkNodeRecursively(node); + } + } +}; + +fetch(url) + .then(response => response.json()) + .then(parsed => parsed.parse.text) + .then(text => { + const domParser = new DOMParser(); + let doc = domParser.parseFromString(text["*"], "text/html"); + parseNode(doc); + checkNodeRecursively(body); + const observer = new MutationObserver(callback); + observer.observe(body, { + childList: true, + subtree: true + }); + }); + +function checkNode(startNode) { + if (startNode.nodeType == Node.TEXT_NODE) { + const value = startNode.nodeValue.toLowerCase(); + if (deepCheck(value)) { + startNode.nodeValue = "*Banned by StopSlogan*"; + bannedSlogans.push(value); + } + } else { + if (startNode.nodeType == Node.ELEMENT_NODE) { + if (startNode.tagName == "INPUT" || startNode.tagName == "TEXTAREA") { + if (deepCheck(startNode.value)) { + startNode.value = bannedWord; + } + } else { + if (startNode.childNodes.length == 0) { + if (deepCheck(startNode.innerText)) { + startNode.innerText = bannedWord; + } + } + } + } + } +} + +function deepCheck(text) { + if (text == bannedWord || !text) { + return false; + } + text = text.toLowerCase(); + + if (slogans[text]) { + return true; + } + for (var i = 0; i < slogansKeys.length; i++) { + const key = slogansKeys[i]; + if (text.includes(key)) { + return true; + } + } + return false; + //TODO: Levenshtein Distance Algorithm +} + +function checkNodeRecursively(startNode) { + checkNode(startNode); + + startNode.childNodes.forEach(node => { + checkNodeRecursively(node); + }); +} diff --git a/logo.png b/logo.png new file mode 100644 index 0000000..546064e Binary files /dev/null and b/logo.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..5c23614 --- /dev/null +++ b/manifest.json @@ -0,0 +1,19 @@ +{ + "manifest_version": 2, + "name": "StopSlogan", + "version": "0.1", + "content_scripts": [ + { + "matches": [""], + "js": ["content.js"] + } + ], + "background": { + "scripts": ["scripts/background.js"], + "persistent": false + }, + "browser_action": { + "default_popup": "popup.html" + }, + "permissions": ["activeTab"] +} diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..844e21f --- /dev/null +++ b/popup.html @@ -0,0 +1,26 @@ + + + + Stop Slogan + + + +
+ Currently banned slogans: + +
+
+ +