-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
67 lines (59 loc) · 1.85 KB
/
Copy pathbackground.js
File metadata and controls
67 lines (59 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const BLOCK_RULE_ID = 1;
const blockingRule = {
id: BLOCK_RULE_ID,
priority: 1,
action: { type: "redirect", redirect: { url: chrome.runtime.getURL("block/blocked.html") }},
condition: {
urlFilter: ".ai|chatgpt|gemini|quillbot",
resourceTypes: ["main_frame"]
}
};
// Ensure a clean state on startup by removing any existing rule with BLOCK_RULE_ID
chrome.runtime.onStartup.addListener(() => {
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [],
removeRuleIds: [BLOCK_RULE_ID]
}, () => {
console.log("Cleared existing rules on startup.");
});
});
// Function to enable blocking by adding the rule
function enableBlocking() {
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [blockingRule],
removeRuleIds: [BLOCK_RULE_ID] // Remove any existing rule with the same ID
}, () => {
console.log("Blocking rule added.");
});
}
// Function to disable blocking by removing the rule
function disableBlocking() {
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [],
removeRuleIds: [BLOCK_RULE_ID]
}, () => {
console.log("Blocking rule removed.");
});
}
// Set initial blocking status when extension loads
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({ blockingEnabled: false }); // Default to disabled
});
chrome.storage.local.get("blockingEnabled", (data) => {
const isEnable = data.blockingEnabled ?? false
if (isEnable) {
enableBlocking();
} else {
disableBlocking();
}
});
// Listen for changes to blockingEnabled status
chrome.storage.onChanged.addListener((changes) => {
if (changes.blockingEnabled) {
if (changes.blockingEnabled.newValue) {
enableBlocking();
} else {
disableBlocking();
}
}
});