I have a chrome extension that blocks a few websites. I’m trying to make a toggle switch so that it will stop blocking the sites when the switch is toggled off. Currently, I have the html and css implemented into a popup menu, but no code that actually turns off the script and removes the blocks. How can I implement some code that will turn off the script?
I am using version 111.0.5563.146 of google chrome, manifest version 3.
Here is the code I currently have:
manifest.json
{
"manifest_version": 3,
"name": "On-Off Switch",
"version": "1.0.0",
"action": {
"default_popup": "popup.html"
},
"background": {
"service_worker": "background.js"
},
"declarative_net_request": {
"rule_resources": [
{
"id": "ruleset_1",
"enabled": false,
"path": "ruleset_1.json"
}
]
},
"permissions": [
"declarativeNetRequest",
"storage"
]
}
popup.html
<!DOCTYPE html>
<html>
<head>
<title>popup</title>
</head>
<body>
<p>popup</p>
<button id="on">On</button>
<button id="off">Off</button>
<script src="popup.js"></script>
</body>
</html>
popup.js
document.getElementById("on").onclick = () => {
chrome.storage.local.set({active: true});
}
document.getElementById("off").onclick = () => {
chrome.storage.local.set({active: false});
}
ruleset_1.json
[
{
"id": 1,
"action": { "type": "block" },
"condition": { "urlFilter": "stackoverflow.com", "resourceTypes": ["main_frame"] }
},
{
"id": 2,
"action": { "type": "block" },
"condition": { "urlFilter": "google.com/search*", "resourceTypes": ["main_frame"] }
}
]
background.js
async function runtime_on_installed(details) {
if (details.reason == "install") {
// Store the extension's current state
// which is determined by the "declarative_net_request" key in manifest.json
let ruleset_ids = await chrome.declarativeNetRequest.getEnabledRulesets();
if (ruleset_ids.length == 0) {
// Extension is currently inactive
await chrome.storage.local.set({active: false});
}
else if (ruleset_ids.length == 1) {
// Extension is currently active
await chrome.storage.local.set({active: true});
}
}
else if (details.reason == "update") {
// Restore the extension's stored state
// https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#method-updateEnabledRulesets
// "Note that the set of enabled static rulesets is persisted across sessions but not across extension updates, i.e. the rule_resources manifest key will determine the set of enabled static rulesets on each extension update."
let { active } = chrome.storage.local.get("active");
let ruleset_ids = await chrome.declarativeNetRequest.getEnabledRulesets();
if (active && ruleset_ids.length == 0) {
// Extension is supposed to be active, but is inactive
chrome.declarativeNetRequest.updateEnabledRulesets({enableRulesetIds: ["ruleset_1"]});
}
else if (!active && ruleset_ids.length == 1) {
// Extension is supposed to be inactive, but is active
chrome.declarativeNetRequest.updateEnabledRulesets({disableRulesetIds: ["ruleset_1"]});
}
}
}
function storage_on_changed(changes, area_name) {
if (area_name == "local") {
if (changes.active.oldValue == false && changes.active.newValue == true) {
chrome.declarativeNetRequest.updateEnabledRulesets({enableRulesetIds: ["ruleset_1"]});
}
else if (changes.active.oldValue == true && changes.active.newValue == false) {
chrome.declarativeNetRequest.updateEnabledRulesets({disableRulesetIds: ["ruleset_1"]});
}
}
}
chrome.runtime.onInstalled.addListener(runtime_on_installed);
chrome.storage.onChanged.addListener(storage_on_changed);
At the moment, neither block works. Just before the stackoverflow.com
block worked, but the google.com/search*
did not. Maybe it has to do with the way I’m defining the rules?
2
Answers
Here is a sample of it.
manifest.json
popup.html
popup.js
background.js
This is based on NorioYamamoto’s answer (that’s why I’ve upvoted it), but uses static rules.
manifest.json
ruleset_1.json
background.js
popup.html
popup.js