javascript 拦截来自 chrome 扩展的 HTTP 请求正文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11593853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intercept HTTP request body from chrome extension
提问by p3drosola
I'm aware that chrome.webRequest.onBeforeRequest
allows a request to be intercepted, analyzed and blocked, but it only allows access to the request headers, and not the request body (as far as i know).
我知道chrome.webRequest.onBeforeRequest
允许拦截、分析和阻止请求,但它只允许访问请求标头,而不允许访问请求正文(据我所知)。
Sample use case: think intercepting form values.
示例用例:考虑拦截表单值。
It seems there is a API change proposal heresuggesting exactly this.
似乎这里有一个 API 更改提议正是在暗示这一点。
Is there another way this could be accomplished?
有没有另一种方法可以实现?
Thanks.
谢谢。
采纳答案by Marmoy
This functionality has been added to the API now, see the documentation.
此功能现已添加到 API 中,请参阅文档。
In order to access the body you need to do the following:
为了访问身体,您需要执行以下操作:
chrome.webRequest.onBeforeRequest.addListener(
function(details)
{
console.log(details.requestBody);
},
{urls: ["https://myurlhere.com/*"]},
['requestBody']
);
回答by Taher Elsheikh
Here is what I did
这是我所做的
- I used the
requestBody
to get the post requests body - I used a
decoder
the parse the body into a string
- 我用
requestBody
来获取帖子请求正文 - 我使用了
decoder
将正文解析为字符串
Here is an example
这是一个例子
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if(details.method == "POST")
// Use this to decode the body of your post
var postedString = decodeURIComponent(String.fromCharCode.apply(null,
new Uint8Array(details.requestBody.raw[0].bytes)));
console.log(postedString)
},
{urls: ["<all_urls>"]},
["blocking", "requestBody"]
);
回答by patrickgamer
While you may not be able to intercept, you can use standard AJAX approach to duct-tape it. Instead of making the href request see if you can make an asynchronous call and save it to an HTML object that isn't presented. Then scrape/read/parse/whatever your body criteria is, and if it passes, push that body object back up to the current window/page.
虽然您可能无法拦截,但您可以使用标准的 AJAX 方法将其粘贴起来。而不是让 href 请求查看您是否可以进行异步调用并将其保存到未呈现的 HTML 对象中。然后刮取/读取/解析/无论您的主体标准是什么,如果通过,则将该主体对象推回当前窗口/页面。
Storing the content in a suppressed element and then using that same element for content would allow you to avoid making duplicate calls. The downside is that you will get the full content for stuff you won't end up using. That may or may not be a bandwidth/speed performance issue.
将内容存储在被抑制的元素中,然后将相同的元素用于内容可以避免进行重复调用。缺点是您将获得最终不会使用的内容的完整内容。这可能是也可能不是带宽/速度性能问题。