-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
blackstormx
committed
Jul 28, 2020
0 parents
commit 3cd13d1
Showing
5 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"manifest_version": 2, | ||
"name": "StopSlogan", | ||
"version": "0.1", | ||
"content_scripts": [ | ||
{ | ||
"matches": ["<all_urls>"], | ||
"js": ["content.js"] | ||
} | ||
], | ||
"background": { | ||
"scripts": ["scripts/background.js"], | ||
"persistent": false | ||
}, | ||
"browser_action": { | ||
"default_popup": "popup.html" | ||
}, | ||
"permissions": ["activeTab"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Stop Slogan</title> | ||
</head> | ||
<style> | ||
body { | ||
background: rgb(24, 24, 24); | ||
font-size: 1em; | ||
} | ||
.container { | ||
width: 200px; | ||
} | ||
.h { | ||
color: whitesmoke; | ||
font-size: 0.8em; | ||
} | ||
</style> | ||
<body> | ||
<div class="container"> | ||
<span class="h">Currently banned slogans: </span> | ||
|
||
<div></div> | ||
</div> | ||
</body> | ||
</html> |