-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
156 lines (143 loc) · 4.09 KB
/
background.js
File metadata and controls
156 lines (143 loc) · 4.09 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Background service worker for the Chrome extension
console.log('Background service worker loaded')
// Extension installation event
chrome.runtime.onInstalled.addListener(details => {
console.log('Extension installed:', details.reason)
if (details.reason === 'install') {
// Set default storage values
chrome.storage.sync.set({
extensionEnabled: true,
clickCount: 0
})
}
})
// Handle extension icon click
chrome.action.onClicked.addListener(tab => {
console.log('Extension icon clicked on tab:', tab.url)
})
// Listen for messages from popup or content scripts
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('Message received:', request)
if (request.action === 'getStatus') {
sendResponse({ status: 'Background script is running' })
}
if (request.action === 'pageLoaded') {
console.log('Page loaded notification from content script:', {
url: request.url,
timestamp: new Date(request.timestamp).toISOString(),
tabId: sender.tab?.id
})
// Verify the setting was applied
chrome.contentSettings.javascript.get(
{
primaryUrl: request.url
},
details => {
console.log('Current JavaScript setting for', request.url, ':', details)
}
)
}
// Add handler to check blocking status
if (request.action === 'checkBlocking') {
chrome.contentSettings.javascript.get(
{
primaryUrl: request.url
},
details => {
console.log('JavaScript setting for', request.url, ':', details)
if (details) {
sendResponse({
blocked: details.setting === 'block',
setting: details.setting,
source: details.source
})
} else {
sendResponse({
blocked: false,
setting: undefined,
source: undefined,
error: 'No details returned from contentSettings API'
})
}
}
)
return true // Keep message channel open for async response
}
// Add handler to manually apply blocking
if (request.action === 'applyBlocking') {
const domain = request.domain
chrome.contentSettings.javascript.set(
{
primaryPattern: `https://${domain}/*`,
setting: 'block'
},
() => {
if (chrome.runtime.lastError) {
console.error(
`Failed to block JavaScript on ${domain}:`,
chrome.runtime.lastError
)
} else {
console.log(
`✅ JavaScript blocking rule applied for https://${domain}/*`
)
}
}
)
}
// Add handler to disable blocking
if (request.action === 'disableBlocking') {
const domain = request.domain
chrome.contentSettings.javascript.set(
{
primaryPattern: `https://${domain}/*`,
setting: 'allow'
},
() => {
if (chrome.runtime.lastError) {
console.error(
`Failed to remove JavaScript blocking on ${domain}:`,
chrome.runtime.lastError
)
} else {
console.log(
`✅ JavaScript blocking rule removed for ${domain}/*`
)
// Verify the setting was cleared
chrome.contentSettings.javascript.get(
{
primaryUrl: `https://${domain}/*`
},
details => {
console.log(
`Current JavaScript setting for ${domain} after clearing:`,
details
)
}
)
}
}
)
}
if (request.action === 'clearRule') {
const domain = request.domain
chrome.contentSettings.javascript.clear(
{
primaryPattern: `${domain}/*`
},
() => {
if (chrome.runtime.lastError) {
console.error(
`Failed to clear JavaScript rule on ${domain}:`,
chrome.runtime.lastError
)
} else {
console.log(
`✅ JavaScript blocking rule cleaned for ${domain}/*`
)
}
}
)
}
return true // Keep the message channel open for async responses
})