在devtool中启用Cross-Origin XMLHttpRequest

时间:2014-08-24 09:15:03

标签: javascript google-chrome-extension xmlhttprequest google-chrome-devtools

我正在使用devtool Chrome扩展程序,并希望记录当前网站的所有XHR请求。所以在我的注入脚本ea "messageback-script"中我有这个:

(function(open) {
  XMLHttpRequest.prototype.open = function(method, url, async) {


console.log('The URL is: ' + url); 


  };

})(XMLHttpRequest.prototype.open);

我已在manifest.json

中提供了所需的权限
{
    "name": "DevToolsPanel",
    "version": "0.1",
    "description": "Log XHR requests",
    "devtools_page": "devtools.html",
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "permissions": [
        "tabs", 
        "http://*/*",
        "https://*/*"
    ],
    "manifest_version": 2
} 

问题是我在控制台日志中看不到任何内容(空白)。我做错了什么?

1 个答案:

答案 0 :(得分:3)

经过检查the code you are using,我可以说这个脚本是通过chrome.tabs.executeScript评估的,使其成为内容脚本。

内容脚本存在于isolated context中,因此修改XHR原型只会影响内容脚本中的XHR。有canonical question描述了这个问题和解决方法。

但是,由于您正在编写devtools扩展,因此您可以通过chrome.devtools.inspectedWindow.eval()获得更简单的解决方案,该解决方案直接在页面自己的上下文中执行代码。

// In the devtool panel code
chrome.devtools.inspectedWindow.eval("/* Your code here*/");