javascript - 谷歌浏览器 : How to determine which window has generated WebRequest?

标签 javascript google-chrome google-chrome-extension

我编写了 chome 扩展,用于处理浏览器中的所有请求:

manifest.json:

{
  "name": "MyExtension",
  "version": "0.1",
  "description": "All requests are under control!",
   "permissions": [
        "tabs",
        "webRequest",
        "http://*/*"
  ],
  "background": {
    "scripts": ["background.js"]
  },

  "manifest_version": 2
}

背景.js:

chrome.webRequest.onCompleted.addListener(
  function(details) {
  console.log(details);
  console.log(chrome.tabs.getCurrent());

  },
   {urls: ["http://*/*"],
   types: ["image"]});

但是现在,我想知道,哪个页面(选项卡?)创建了这个请求?

例如:

Request 1 - generated by google.com page,
Request 2 - generated by stackoverflow.com.

我该如何解决这个任务?

最佳答案

以下代码将获取谁生成了 Web 请求的选项卡\页面详细信息。

onCompleted Listener 有一个 tabId 属性来标识选项卡,您可以检索该选项卡的所有详细信息。

chrome.webRequest.onCompleted.addListener(

function (details) {
    chrome.tabs.get(details.tabId, function (tab) {
        console.log("This  " + JSON.stringify(details) + " Web request is from this " + tab.id + " tab and its details are" + JSON.stringify(tab));
    });
}, {
    urls: ["http://*/*"],
    types: ["image"]
});

示例输出

This  {"frameId":0,"fromCache":true,"ip":"74.125.236.63","method":"GET","parentFrameId":-1,"requestId":"563","statusCode":200,"statusLine":"HTTP/1.1 200 OK","tabId":64,"timeStamp":1359389270317.956,"type":"image","url":"http://www.google.co.in/images/srpr/logo3w.png"} Web request is from this 64 tab and its details are{"active":true,"highlighted":true,"id":64,"incognito":false,"index":4,"pinned":false,"selected":true,"status":"loading","title":"Google","url":"http://www.google.co.in/","windowId":1} 

关于javascript - 谷歌浏览器 : How to determine which window has generated WebRequest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14564540/

相关文章:

css - 旋转图像在 Firefox 中不起作用

javascript - 如何确定您的扩展后台脚本在哪个浏览器中执行?

css - :focus-visible? 的 chrome 问题(在正常鼠标焦点上显示焦点可见样式)

javascript - 是什么让 Firebug/Chrome 控制台将自定义对象视为数组?

google-chrome-extension - 为什么dart.io之类的只能在命令行应用中使用?

javascript - 网页和 chrome 扩展的 localStorage 不同

javascript - "npm start"在 ghost (node.js) 中给出错误

javascript - 如何在时间变化时旋转图像?

javascript - typescript :将方法添加到类的数组

javascript - 动态 HTML 显示当前日期