Skip to content

Commit

Permalink
commie
Browse files Browse the repository at this point in the history
  • Loading branch information
blackstormx committed Jul 29, 2020
1 parent 430ad13 commit 5e496ce
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 48 deletions.
3 changes: 3 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ parseWiki.onchange = () => {
chrome.storage.sync.get("customBanWord", function(items) {
customBanWord.placeholder = "Current: " + items.customBanWord;
});
chrome.storage.sync.get("parseWiki", function(items) {
parseWiki.checked = items.parseWiki;
});

set.onclick = () => {
saveNewBanWord();
Expand Down
122 changes: 75 additions & 47 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,77 @@ const body = document.body;

let bannedWord = "*Banned by StopSlogan*";
let parseWiki = true;
const slogans = {};
let slogansKeys = [];
const bannedSlogans = [];
const url =
"https://en.wikipedia.org/w/api.php?" +
new URLSearchParams({
origin: "*",

chrome.storage.sync.get("parseWiki", function(items) {
console.log(items);
parseWiki = items.parseWiki;
next();
});
action: "parse",
page: "List_of_political_slogans",
format: "json"
});

function next() {
chrome.storage.sync.get("customBanWord", function(items) {
bannedWord = items.customBanWord;
startScan();
function chromeStorage(sKey) {
return new Promise(function(resolve, reject) {
chrome.storage.local.get(sKey, function(items) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
reject(chrome.runtime.lastError.message);
} else {
resolve(items[sKey]);
}
});
});
}

function startScan() {
const slogans = {};
let slogansKeys = [];
const bannedSlogans = [];

chrome.storage.sync.get("savedCustomSlogans", function(items) {
items.savedCustomSlogans.forEach(slogan => {
chromeStorage("parseWiki")
.then(result => {
parseWiki = result;
return "customBanWord";
})
.then(result => {
bannedWord = result;
return "savedCustomSlogans";
})
.then(result => {
result.forEach(slogan => {
slogansKeys.push(slogan);
slogans[slogan] = true;
});
});

// const url = "https://en.wikipedia.org/wiki/List_of_political_slogans";
const url =
"https://en.wikipedia.org/w/api.php?" +
new URLSearchParams({
origin: "*",
// chrome.storage.sync.get("parseWiki", function(items) {
// parseWiki = items.parseWiki;
// });

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'
// chrome.storage.sync.get("customBanWord", function(items) {
// bannedWord = items.customBanWord;
// });

chrome.storage.sync.get("savedCustomSlogans", function(items) {
console.log(items);
items.savedCustomSlogans.forEach(slogan => {
slogansKeys.push(slogan);
slogans[slogan] = true;
});
});

// const url = "https://en.wikipedia.org/wiki/List_of_political_slogans";

//en.wikipedia.org/w/api.php?action=parse&format=json&origin=*&page=Most%20common%20words%20in%20Spanish'

function parseNode(startNode) {
function startScan() {
function parseWikiNodes(startNode) {
if (startNode.tagName == "LI") {
const key = startNode.innerText.toLowerCase();
slogans[key] = true;
slogansKeys.push(key);
}
startNode.childNodes.forEach(node => {
parseNode(node);
parseWikiNodes(node);
});
}

Expand All @@ -61,21 +86,24 @@ function startScan() {
}
};

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
if (parseWiki) {
fetch(url)
.then(response => response.json())
.then(parsed => parsed.parse.text)
.then(text => {
const domParser = new DOMParser();
let doc = domParser.parseFromString(text["*"], "text/html");
parseWikiNodes(doc);
checkNodeRecursively(body);
const observer = new MutationObserver(callback);
observer.observe(body, {
childList: true,
subtree: true
});
});
});

} else {
checkNodeRecursively(body);
}
function checkNode(startNode) {
if (startNode.nodeType == Node.TEXT_NODE) {
const value = startNode.nodeValue.toLowerCase();
Expand Down Expand Up @@ -132,9 +160,9 @@ function startScan() {
// chrome.runtime.sendMessage(bannedSlogans, function (response) {
// console.log(response.farewell);
// });
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action == "getBannedSlogans") {
sendResponse(bannedSlogans);
}
});
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action == "getBannedSlogans") {
sendResponse(bannedSlogans);
}
});
2 changes: 1 addition & 1 deletion popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
max-height: 340px;
}
#page2 > .list {
height: calc(100% - 220px);
height: calc(100% - 260px);
}
.list {
overflow: auto;
Expand Down

0 comments on commit 5e496ce

Please sign in to comment.