【问题标题】:XHR forbidden in Chrome extension, despite adding the correct permissions?尽管添加了正确的权限,但 Chrome 扩展程序中禁止 XHR?
【发布时间】:2011-09-27 22:38:35
【问题描述】:

我目前正在为我的developer omnibox extensions 添加一个Matlab API Search,并且遇到了一个奇怪的 XHR 问题(想知道 Chrome 最近是否发生了一些变化)。

当扩展程序尝试从 http://www.mathworks.com/help/techdoc/ref/funcalpha.html 获取 API 函数列表时,XHR 失败并显示“Access-Control-Allow-Origin”。我在过去的一些其他扩展中看到过这种失败,但通常是因为忘记在清单中声明正确的权限。但是,我的清单在权限列表中包含“http://www.mathworks.com/”,所以我不明白为什么会失败。我看到有人提到内容脚本中不允许跨域 XHR,但这是背景页面中包含的脚本,而不是内容脚本,所以我很困惑为什么会失败。

注意:我使用的是 Chrome 14(开发频道)。我很想知道这是否是最近在 Chrome 14 中出现的在早期版本的 Chrome 中运行良好的东西,或者我只是在我的代码中做一些愚蠢的事情。如果它在 Chrome 14 中被破坏,任何解决方法的建议将不胜感激。

清单:

{
    "name":"Matlab API Search",
    "description":"Adds support to the omnibox to search the Matlab API.",
    "background_page":"background.html",
    "icons":{"128":"icon128.png", "32":"icon32.png", "16":"icon16.png"},
    "omnibox":{"keyword":"matlab"},
    "permissions":[
        "tabs",
        "http://www.mathworks.com/"
    ],
    "version":"1.0"
}

XHR 调用:

xhr("http://www.mathworks.com/help/techdoc/ref/funcalpha.html",
    function(url, req) {
    // ...
    },
    function(url, req) {
    // ...
    }).send(null);

xhr 函数定义如下:

  function xhr(url, ifexists, ifnotexists, retry_interval) {
    var retry_time = retry_interval || 5;
    var req = new XMLHttpRequest();
    console.log("Fetching: " + url);
    req.open("GET", url);
    req.onreadystatechange=function(){
        if (req.readyState == 4){
            var status=req.status;
            if ((status == 200) || (status == 301) || (status == 302)) {
                ifexists(url, req);
            } else {
                ifnotexists(url, req);
                setTimeout(function() { xhr(url, ifexists, ifnotexists, retry_time + 5).send(null); }, retry_time);
            }
        }
    };
    return req;
  }

注意
我还尝试了以下权限:

http://www.mathworks.com/*
http://*.mathworks.com/*
*://www.mathworks.com/*
*://*.mathworks.com/* 
*://*
http://*/*

(无论如何,我不想使用“所有网站上的所有数据”,但这似乎意味着我声明权限的方式不是问题,这就是为什么我非常困惑)。

版本
14.0.803.0(正式版 90483)开发

更新
我已经提交了这个Chrome bug,因为我认为这实际上是 Chrome 中的一个错误。但是,如果这个问题没有得到解决,我真的很感激任何可以使它工作的解决方法。

【问题讨论】:

  • XHR 是从后台页面还是内容脚本完成的?如果可能,您能否上传一个显示此问题的最小示例(最好是解压缩的扩展)?
  • 这是一个背景页面,不是内容脚本。您可以在链接中查看完整的源代码。
  • 你知道实际的 http 请求/响应是什么样的吗(可能使用 Fiddler 或 WireShark)?我的猜测是 Chrome 将 Origin 标头包含在 http 请求中,这使其成为跨域请求 (w3.org/TR/cors),进而导致它失败。也许 Chrome 扩展程序会开始遵守同源政策?
  • 更新:不,这看起来像是第 3 方 cookie 被阻止的问题,而且 mathworks 在响应中愚蠢地设置了许多 cookie,而实际上根本没有理由使用 cookie。跨度>
  • 我也遇到了跨域请求和以前可以工作的 Chrome 14 的问题,至少它可以在 Chrome 12 上工作。

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


【解决方案1】:

在 URL 的末尾添加 *,它是基于通配符的。

{
    "name":"Matlab API Search",
    "description":"Adds support to the omnibox to search the Matlab API.",
    "background_page":"background.html",
    "icons":{"128":"icon128.png", "32":"icon32.png", "16":"icon16.png"},
    "omnibox":{"keyword":"matlab"},
    "permissions":[
        "tabs",
        "http://www.mathworks.com/*"
    ],
    "version":"1.0"
}

参见http://code.google.com/chrome/extensions/match_patterns.html - 我们的一些文档似乎暗示它可以在没有 * 的情况下工作,但它不应该(参见“不良示例”)。

【讨论】:

  • ... 同样,省略了用于工作的星号。 (我有几个扩展,至少在使用早期版本的 Chrome 测试时,它们运行良好,并且不包含星号)。
猜你喜欢
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 2018-09-24
  • 2019-08-27
  • 2017-04-01
  • 1970-01-01
相关资源
最近更新 更多