w3c规定,当请求的header匹配以下不安全字符时,将被终止,具体参考如下:
Accept-Charset
Accept-Encoding
Connection
Content-Length
Cookie
Cookie2
Content-Transfer-Encoding
Date
Expect
Host
Keep-Alive
Referer
TE
Trailer
Transfer-Encoding
Upgrade
User-Agent
Via
插件解决方案
chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
var headers = details.requestHeaders;
// 遍历headers ,查出含有我们定义标识头的请求头,去掉自定义的标识头覆盖同名w3c
for(let [key,name] of Object.entries(headers)) {
// code ...
}
// details.requestHeaders.push({
// name: 'xxxxx",
// value: 'yyyyy'
// })
console.log(headers)
return {requestHeaders: details.requestHeaders}
},{urls: ["要伪造的地址"]},
// 请求头拦截处理函数
function headersInterceptors(details){
details.requestHeaders = function(requestHeaders) {
let headers = [];
for (let item of requestHeaders) {
if (!["origin", "user-agent", "accept-language", "referer"].includes(item.name.toLowerCase())) {
if(item.name.indexOf(config.prefix) === 0) {
item.name = zslFn.ltrim(item.name, config.prefix)
}
headers.push(item)
}
}
return headers
}(details.requestHeaders)
// let delNum = 0;
// for (let [key, {name, value}] of Object.entries(details.requestHeaders)) {
// if (name.indexOf(config.prefix) === 0) {
// details.requestHeaders.splice(key - delNum, 1, )
// ++delNum
// details.requestHeaders.push({
// name: zslFn.ltrim(name, config.prefix),
// value: value
// })
// }
// }
return {requestHeaders: details.requestHeaders}
}
// 发送请求头前监听器
// 方法一,try
try {
chrome.webRequest.onBeforeSendHeaders.addListener(headersInterceptors, {
urls: ["<all_urls>"]
}, ["requestHeaders", "blocking", "extraHeaders"])
} catch (t) {
chrome.webRequest.onBeforeSendHeaders.addListener(headersInterceptors, {
urls: ["<all_urls>"]
}, ["requestHeaders", "blocking"])
}
OR
// 方法2, chrome接口参数 (推荐用该方法)
chrome.webRequest.onBeforeSendHeaders.addListener(headersInterceptors, {
urls: ["<all_urls>"]
}, ["requestHeaders", "blocking", chrome.webRequest.OnBeforeSendHeadersOptions.EXTRA_HEADERS])
// 移除监听器
chrome.webRequest.onBeforeSendHeaders.removeListener(headersInterceptors, {
urls: ["<all_urls>"]
}, ["requestHeaders", "blocking", chrome.webRequest.OnBeforeSendHeadersOptions.EXTRA_HEADERS])