【问题标题】:What kind of requests will invoke Chrome webRequest API onBeforeRequest?什么样的请求会调用 Chrome webRequest API onBeforeRequest?
【发布时间】:2015-08-06 10:39:39
【问题描述】:

我以为每个请求都会调用 onBeforeRequest,但似乎不是。以下是来自 google doc 的示例,我插入 console.log 以查看是否有任何请求调用 onBeforeRequest。

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
      console.log(details.url);
      return {cancel: details.url.indexOf("://www.evil.com/") != -1};
    },
    {urls: ["<all_urls>"]},
    ["blocking"]);

某些网站(例如 Facebook)不会在控制台上打印任何内容。

onBeforeRequest 会捕获什么样的请求?

【问题讨论】:

    标签: google-chrome-extension


    【解决方案1】:

    您的权限是什么? API 要求您对要捕获的所有 URL 具有主机权限。

    例如,上面的示例需要以下内容:

    "permissions" : [
      "webRequest",
      "webRequestBlocking",
      "<all_urls>"
    ]
    

    【讨论】:

    • 也许它正在通过推送。我无法捕获在套接字等中收到的推送通知。一些网站无需执行其他 ajax 即可收到完整的更新。
    • @ZigMandel 是的,它不会捕获 WebSocket 流量,但 OP 说“不会打印 anything
    • 那么如何捕获 websockets?也许我最后应该在这里问一个问题:)
    • @ZigMandel 遗憾的是,我认为只有 chrome.debuggerchrome.devtools 会公开这些内容。见this question
    • 我只添加了“http://*/*”到我的许可,这就是它无法阻止 Facebook 的原因。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    相关资源
    最近更新 更多