Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor blockpage.js #121

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions blockpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ chrome.runtime.onConnect.addListener((port) => {
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good that this part is untouched


// Fetch and add rules to declarativeNetRequest
function fetchProtectionRules(url,status){
fetch(url)
async function fetchProtectionRules(url,status){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

let domains = await fetch(url)
.then((res) => res.json())
.then((domains) => {

if(status==="redirect"){
chrome.storage.local.set({ "rules_count": domains.length }); // Save the count(number) of rules
await chrome.storage.local.set({ "rules_count": domains.length }); // Save the count(number) of rules
console.log("Disabling domains!");
chrome.declarativeNetRequest.updateDynamicRules({
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: domains.map((_, index) => index + 1),
addRules: domains.map((domain, index) => ({
id: index + 1,
Expand All @@ -64,11 +64,11 @@ function fetchProtectionRules(url,status){
},
})),
});
}
else if(status==="off"){

} else if(status==="off") {
console.log("Allowing domains!");
// TODO: remove user whitelisting also
chrome.declarativeNetRequest.updateDynamicRules({
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: domains.map((_, index) => index + 1),
addRules: domains.map((domain, index) => ({
id: index + 1,
Expand All @@ -81,18 +81,17 @@ function fetchProtectionRules(url,status){
})),
});
}
});
}

// saveUpdateTime function sets the current date in chrome's local storage
function saveUpdateTime() {
async function saveUpdateTime() {
const tDate = new Date().toLocaleDateString();
chrome.storage.local.set({ run_day: tDate });
await chrome.storage.local.set({ run_day: tDate });
}

function performUpdate(status) {
async function performUpdate(status) {
try {
fetchProtectionRules(DOMAIN_RULES_URL,status);
await fetchProtectionRules(DOMAIN_RULES_URL,status);
console.log("Success: Rules Added");
} catch (err) {
console.log("Error fetching rules");
Expand All @@ -103,19 +102,19 @@ function performUpdate(status) {
// 1. If date is added, it compares it with current date and if they mismatch it runs the update
// 2. If date is not added(or is undefined) then it performs an update[This will be the "first time" update] and sets the date
try {
chrome.storage.local.get(['run_day'], function (result) {
chrome.storage.local.get(['run_day'], async function (result) {
let checkerDate = new Date().toLocaleDateString();
if (result.run_day === undefined) {
try {
saveUpdateTime();
performUpdate("redirect");
await saveUpdateTime();
await performUpdate("redirect");
console.log("First Update Performed!");
} catch (err) { console.log("Error while fetching first-run data:E01!"); }
}
else if (result.run_day !== checkerDate) {
try {
saveUpdateTime();
performUpdate("redirect");
await saveUpdateTime();
await performUpdate("redirect");
console.log("Updated Successfully!");
} catch (err) { console.log("Error while fetching subsequent data: E02!"); }
}
Expand All @@ -126,15 +125,15 @@ try {

// Message passing between popup.js and this script to enable toggle(on/off) functionality
chrome.runtime.onMessage.addListener(
function (request) {
async function (request) {
if (request.NMD_status==="on"){
performUpdate("redirect");
await performUpdate("redirect");
}
else if (request.NMD_status === "off"){
performUpdate("off");
await performUpdate("off");
}
else{
performUpdate("redirect");
await performUpdate("redirect");
}
}
);
Expand All @@ -144,8 +143,8 @@ async function revertRulesDefault() {
// remove all rules
let ruleIds = (await chrome.declarativeNetRequest.getDynamicRules())
.map((rule)=>rule.id);
await chrome.declarativeNetRequest.updateDynamicRules({removeRuleIds: ruleIds});

// regenerated default rules
performUpdate("redirect");
await chrome.declarativeNetRequest.updateDynamicRules({removeRuleIds: ruleIds});
await chrome.storage.local.clear();
await performUpdate("redirect"); //regenerates default rules
}