diff --git a/background.js b/background.js index ca924ee..9ea897c 100644 --- a/background.js +++ b/background.js @@ -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(); diff --git a/content.js b/content.js index 3df0a03..05cd9cc 100644 --- a/content.js +++ b/content.js @@ -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); }); } @@ -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(); @@ -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); + } +}); diff --git a/popup.html b/popup.html index 5170216..69019f0 100644 --- a/popup.html +++ b/popup.html @@ -44,7 +44,7 @@ max-height: 340px; } #page2 > .list { - height: calc(100% - 220px); + height: calc(100% - 260px); } .list { overflow: auto;