From 04b8f177a84a4be954f28e2fba6882807c2ff1d3 Mon Sep 17 00:00:00 2001 From: Rafael Keramidas Date: Thu, 21 Sep 2017 13:19:28 +0100 Subject: [PATCH] Loading blacklist from Github with a local fallback, #16 --- package.json | 2 +- src/js/background.js | 65 ++++++++++++++++++++++++-------------------- src/js/popup.js | 2 -- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index d42204b..61ac9f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "no-coin", - "version": "0.3.2", + "version": "0.3.3", "description": "Stop coin miners on the web!", "repository": { "type": "git", diff --git a/src/js/background.js b/src/js/background.js index 022c23b..653801b 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -83,7 +83,34 @@ const removeDomainFromWhitelist = (domain) => { config.whitelist = config.whitelist.filter(w => w.domain !== domain); saveConfig(); -} +}; + +const runBlocker = (blacklist) => { + const blacklistedUrls = blacklist.split('\n'); + + chrome.webRequest.onBeforeRequest.addListener(details => { + // Globally paused + if (!config.toggle) { + return { cancel: false }; + } + + // Is domain white listed + if (isDomainWhitelisted(domains[details.tabId])) { + return { cancel: false }; + } + + return { cancel: true }; + }, { + urls: blacklistedUrls + }, ['blocking']); +}; + +const runFallbackBlocker = () => { + fetch(chrome.runtime.getURL('blacklist.txt')) + .then(resp => { + resp.text().then(text => runBlocker(text)); + }); +}; /** * Main @@ -104,38 +131,18 @@ if (!config.toggle) { changeToggleIcon(false); } -// Load the blacklist and run the request checker -const blacklist = chrome.runtime.getURL("blacklist.txt"); +// Load the blacklist and run the blocker +const blacklist = 'https://raw.githubusercontent.com/keraf/aaaNoCoin/master/src/blacklist.txt'; fetch(blacklist) .then(resp => { - resp.text() - .then(text => { - const blacklistedUrls = text.split('\r\n'); - - chrome.webRequest.onBeforeRequest.addListener(details => { - // Globally paused - if (!config.toggle) { - return { cancel: false }; - } - - // Is domain white listed - if (isDomainWhitelisted(domains[details.tabId])) { - return { cancel: false }; - } - - return { cancel: true }; - }, { - urls: blacklistedUrls - }, ['blocking']); - }) - .catch(err => { - // TODO: Handle this - console.log(err); - }); + if (resp.status === 200) { + resp.text().then(text => runBlocker(text)); + } else { + runFallbackBlocker(); + } }) .catch(err => { - // TODO: Handle this - console.log(err); + runFallbackBlocker(); }); // Communication with the popup and content scripts diff --git a/src/js/popup.js b/src/js/popup.js index e2b6ad7..5d2bebf 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -45,14 +45,12 @@ document.querySelector('.toggle').addEventListener('click', () => { const listElements = document.getElementsByClassName('list'); for (let i = 0; i < listElements.length; i++) { listElements[i].addEventListener('click', (e) => { - console.log(e); chrome.runtime.sendMessage({ type: 'WHITELIST', time: e.target.getAttribute('data-time'), tabId: currentTabId, whitelisted, }, (response) => { - console.log(response); setWhitelistOptions(response); chrome.tabs.reload(currentTabId); });