Skip to content

Commit

Permalink
main commit
Browse files Browse the repository at this point in the history
  • Loading branch information
blackstormx committed Jul 28, 2020
0 parents commit 3cd13d1
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
Empty file added background.js
Empty file.
106 changes: 106 additions & 0 deletions content.js
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);
});
}
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions manifest.json
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"]
}
26 changes: 26 additions & 0 deletions popup.html
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>

0 comments on commit 3cd13d1

Please sign in to comment.