-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
76 lines (73 loc) · 2.52 KB
/
background.js
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
// Initialize the dynamic rule
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1, 2],
addRules: [{
id: 1,
priority: 1,
action: {
type: 'modifyHeaders',
requestHeaders: [{
header: 'X-Glif-Private',
operation: 'set',
value: 'true'
}]
},
condition: {
urlFilter: '*glif.app/api/run-glif*',
resourceTypes: ['xmlhttprequest']
}
}]
});
// Listen for messages from the content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'UPDATE_PRIVACY') {
const isPrivate = request.isPrivate;
// Update the dynamic rules based on privacy state
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1, 2],
addRules: isPrivate ? [
{
id: 1,
priority: 1,
action: {
type: 'modifyHeaders',
requestHeaders: [{
header: 'X-Glif-Private',
operation: 'set',
value: 'true'
}]
},
condition: {
urlFilter: '*glif.app/api/run-glif*',
resourceTypes: ['xmlhttprequest']
}
},
{
id: 2,
priority: 2,
action: {
type: 'modifyHeaders',
requestHeaders: [{
header: 'X-Glif-Public',
operation: 'set',
value: 'false'
}]
},
condition: {
urlFilter: '*glif.app/api/run-glif*',
method: 'POST',
resourceTypes: ['xmlhttprequest']
}
}
] : []
}).then(() => {
console.log('[Background] Updated privacy rules:', { isPrivate });
sendResponse({ success: true });
}).catch(error => {
console.error('[Background] Error updating rules:', error);
sendResponse({ success: false, error: error.message });
});
// Keep the message channel open for the async response
return true;
}
});