Skip to content
This repository has been archived by the owner on Sep 10, 2020. It is now read-only.

Commit

Permalink
Loading blacklist from Github with a local fallback, #16
Browse files Browse the repository at this point in the history
  • Loading branch information
keraf committed Sep 21, 2017
1 parent a71507c commit 04b8f17
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
65 changes: 36 additions & 29 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 04b8f17

Please sign in to comment.