-
Notifications
You must be signed in to change notification settings - Fork 43
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,14 +45,14 @@ chrome.runtime.onConnect.addListener((port) => { | |
}); | ||
|
||
// Fetch and add rules to declarativeNetRequest | ||
function fetchProtectionRules(url,status){ | ||
fetch(url) | ||
async function fetchProtectionRules(url,status){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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, | ||
|
@@ -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"); | ||
|
@@ -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!"); } | ||
} | ||
|
@@ -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"); | ||
} | ||
} | ||
); | ||
|
@@ -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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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