我正在为Firefox编写我的第一个webExtension。它的目标是一个网站上的一个特殊功能,基本上需要重写服务器响应中的 "redirectURL "值到HTTP POST中(我想让它在某些按钮被点击后停止刷新页面)。
我在这方面遇到了麻烦。到目前为止,我已经尝试了webRequest.onHeadersReceived,但 "redirectURL "值并不在头文件中。我还尝试了过滤ResponseData,但 "redirectURL "值也不在响应的内容中。我用浏览器开发工具捕获了完整的事务,以下是相关部分。
"response": {
"status": 302,
"statusText": "Found",
"httpVersion": "HTTP/2.0",
"headers": [
{
"name": "date",
"value": "Fri, 05 Jul 2019 13:06:18 GMT"
},
{
"name": "content-type",
"value": "text/html; charset=utf-8"
},
{
"name": "location",
"value": "https://www.strava.com/clubs/2140/members"
},
{
"name": "cache-control",
"value": "no-cache, no-store"
},
{
"name": "via",
"value": "1.1 linkerd"
},
{
"name": "x-download-options",
"value": "noopen"
},
{
"name": "pragma",
"value": "no-cache"
},
{
"name": "status",
"value": "302 Found"
},
{
"name": "expires",
"value": "Sat, 01 Jan 2000 00:00:00 GMT"
},
{
"name": "x-request-id",
"value": "5ff9a2b9-e9b4-453e-9e4e-d42e1ffdd09c"
},
{
"name": "referrer-policy",
"value": "strict-origin-when-cross-origin"
},
{
"name": "x-frame-options",
"value": "SAMEORIGIN,DENY"
},
{
"name": "x-content-type-options",
"value": "nosniff"
},
{
"name": "x-permitted-cross-domain-policies",
"value": "none"
},
{
"name": "x-xss-protection",
"value": "1; mode=block"
},
{
"name": "content-encoding",
"value": "gzip"
},
{
"name": "X-Firefox-Spdy",
"value": "h2"
}
],
"cookies": [],
"content": {
"mimeType": "text/html; charset=utf-8",
"size": 119109,
"comment": "Response bodies are not included."
},
"redirectURL": "https://www.strava.com/clubs/1234/members", <-- I want to re-write this
"headersSize": 597,
"bodySize": 29832
},
先谢谢你!
重定向URL在 location
头部. 你在HAR中标记的那一行只是表明浏览器采取的行动。
因此,你可以拦截响应,并改变所述头。
function modifyRedir(details)
{
for (let header of details.responseHeaders) {
if (header.name.toLowerCase() == "location")
{
header.value = "<some URL>";
}
}
return {responseHeaders: details.responseHeaders};
}
browser.webRequest.onHeadersReceived.addListener(
modifyRedir,
{
urls: ["<URL this should be applied to>"]
},
["blocking", "responseHeaders"]
);
或者,你也可以直接覆盖重定向的内容。
function modifyRedir(details)
{
return {redirectUrl: "<some URL>"};
}