从chrome扩展中拦截HTTP请求正文

p3d*_*ola 17 javascript google-chrome google-chrome-extension

我知道chrome.webRequest.onBeforeRequest允许拦截,分析和阻止请求,但它只允许访问请求头,而不是请求体(据我所知).

示例用例:考虑拦截表单值.

似乎有一个API更改提议在这里提出了这一点.

还有另一种方法可以实现吗?

谢谢.

Mar*_*moy 15

此功能现已添加到API中,请参阅文档.

要访问正文,您需要执行以下操作:

chrome.webRequest.onBeforeRequest.addListener(
    function(details)
    {
        console.log(details.requestBody);
    },
    {urls: ["https://myurlhere.com/*"]},
    ['requestBody']
);
Run Code Online (Sandbox Code Playgroud)


小智 8

这是我所做的

  1. 我用requestBody来获取帖子请求正文
  2. 我使用了decoder将正文解析为字符串

这是一个例子

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"]
);
Run Code Online (Sandbox Code Playgroud)