-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
145 lines (110 loc) · 3.49 KB
/
background.js
File metadata and controls
145 lines (110 loc) · 3.49 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
var ruleCookieId = 199;//此数字随机写即可;
var sourceUrl = ''
// var sourceUrl = 'http://xn.magichznpm.com'
// var targetDomain = 'www.axihe.com'
var targetDomain = ''
// 可以自定义
const defaultDomainToUrl = (domain)=>{
console.log(domain.split('.')[0])
return domain.split('.')[0]
}
var domainToUrlfn = defaultDomainToUrl
// 关闭插件
function closeEvent(){
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [ruleCookieId],
}, function(){
console.log('卸载cookie')
});
}
function getdomain(url){
// 例如'www.axihe.com' 请求地址转域名,需求去掉前面的 www ,然后加上 .
return `.${url.split('.').slice(1).join('.')}`
}
async function getCookieValues(source){
let cookies = await chrome.cookies.getAll(
{url:source}
)
if(!cookies) return;
// 需要加上原来的cookie
const origincookies = await chrome.cookies.getAll(
{domain: getdomain(targetDomain)}
)
if(origincookies?.length){
cookies = [...cookies, ...origincookies]
}
// 类似 "name=KJRiG5DUI; girl=beautiful; "
const cookieValue = cookies.map(({name, value})=>{
return `${name}=${value};`
}).join(' ');
return cookieValue;
}
// 更新请求规则
async function updateRules(){
// 如果没有sourceUrl targetDomain值,就不用给请求头设置cookie
if(!sourceUrl || !targetDomain) return;
const cookieval = await getCookieValues(sourceUrl);
// 如果没有cookieval值,说明不用给请求头设置cookie,那么清除原来的ruleid
if(!cookieval) {
closeEvent()
return;
}
chrome.declarativeNetRequest.updateDynamicRules({
// 更新前,先删除旧的id规则,否则报错重复id
removeRuleIds: [ruleCookieId],
addRules: [
{
id: ruleCookieId,
action: {
type: 'modifyHeaders',
requestHeaders:[
{
header: 'Cookie',
value: cookieval,
operation: 'set',
}
]
},
condition: {
// requestMethods:['post'],
urlFilter: domainToUrlfn(targetDomain),
},
}
],
}, function(){
console.log('update cookie')
});
}
chrome.runtime.onInstalled.addListener(() => {
// 默认不开启
// updateRules()
});
chrome.cookies.onChanged.addListener(function({cause,cookie,removed}){
console.log(cookie.domain)
console.log(cause,cookie,removed)
// 只更新与源地址有关的cookie
if(sourceUrl.includes(cookie.domain)){
updateRules()
}
})
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'updatecookie') {
sourceUrl = request.sourceUrl
targetDomain = request.targetDomain
updateRules()
// sendResponse 一定要调用,否则报错
// https://blog.csdn.net/m0_37729058/article/details/89186257
sendResponse('get msg')
}
})
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'setStatus') {
if(request.value ==='close'){
closeEvent()
sendResponse('close')
return
}
updateRules();
sendResponse('open')
}
})