-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
107 lines (90 loc) · 2.48 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
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
/*--------------------------------------------------------------
>>> BACKGROUND
----------------------------------------------------------------
# Global variable
# Update user agent
# Messages
# Storage
# Get
# On change
--------------------------------------------------------------*/
/*--------------------------------------------------------------
# GLOBAL VARIABLE
--------------------------------------------------------------*/
var extension = {};
/*--------------------------------------------------------------
# UPDATE USER AGENT
--------------------------------------------------------------*/
extension.updateUserAgent = function (string) {
if (typeof string === 'string') {
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [{
'id': 1001,
'priority': 1,
'action': {
'type': 'modifyHeaders',
'requestHeaders': [{
'header': 'User-Agent',
'operation': 'set',
'value': string
}]
},
'condition': {
'urlFilter': '*://*/*',
'resourceTypes': [
'main_frame',
'sub_frame',
'stylesheet',
'script',
'image',
'font',
'object',
'xmlhttprequest',
'ping',
'csp_report',
'media',
'websocket',
'webtransport',
'webbundle',
'other'
]
}
}],
removeRuleIds: [1001]
});
} else {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1001]
});
}
};
/*--------------------------------------------------------------
# MESSAGES
--------------------------------------------------------------*/
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
var action = message.action;
if (action === 'options-page-connected') {
sendResponse({
isPopup: sender.hasOwnProperty('tab') === false
});
}
});
/*--------------------------------------------------------------
# STORAGE
--------------------------------------------------------------*/
/*--------------------------------------------------------------
# GET
--------------------------------------------------------------*/
chrome.storage.local.get(function (items) {
extension.updateUserAgent(items['user-agent']);
});
/*--------------------------------------------------------------
# ON CHANGE
--------------------------------------------------------------*/
chrome.storage.onChanged.addListener(function (changes) {
for (var key in changes) {
if (key === 'user-agent') {
extension.updateUserAgent(changes[key].newValue);
}
}
});