forked from ahkohd/Inline-Search
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
84 lines (71 loc) · 2.38 KB
/
Copy pathbackground.js
File metadata and controls
84 lines (71 loc) · 2.38 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
/**
* background.js
*
* Use your favourite search engines on the fly
* without having to open new tab.
*
* @author Tsz Hin Leung
* @copyright Copyright (c) 2023
* @license MIT license
*/
console.log("Welcome to Inline Search")
function removeRulesFromTab(tabId){
chrome.declarativeNetRequest.getSessionRules(
function(rules){
var ruleIdArray = rules.reduce(function(ruleIdArray, rule){
if(rule.condition.tabIds[0] == tabId)
ruleIdArray.push(rule.id)
return ruleIdArray
}, [])
chrome.declarativeNetRequest.updateSessionRules(
{
removeRuleIds: ruleIdArray
}
)
}
)
}
chrome.runtime.onMessage.addListener(
function(request, sender) {
switch(request.operation){
case "open":
var ruleId = Math.floor(Math.random() * chrome.declarativeNetRequest.MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES) + 1
chrome.declarativeNetRequest.updateSessionRules(
{
addRules: [{
"id": ruleId,
"priority": 1,
"action": {
"type": "modifyHeaders",
"requestHeaders": [{
"header": "User-Agent",
"operation": "set",
"value": "Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.91 Mobile Safari/537.36"
}]
},
"condition": {
"tabIds": [sender.tab.id],
"urlFilter": "*://*/*",
"resourceTypes": ["sub_frame"]
}
}]
}
)
break;
case "close":
removeRulesFromTab(sender.tab.id)
break;
}
}
);
chrome.tabs.onRemoved.addListener(
function(tabId){
removeRulesFromTab(tabId)
}
)
chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo){
if (changeInfo.url)
removeRulesFromTab(tabId)
}
)