Firefox插件扩展中的XMLHttpRequest跨域

时间:2018-01-13 22:54:25

标签: javascript firefox xmlhttprequest firefox-addon firefox-addon-sdk

我正在开发一个addon chrome和firefox。

我的问题只出现在firefox上,而不是出现在chrome上。

我想在domain2.com的一个标签页(不在后台脚本中)中使用url domain1.com创建XMLHttpRequest

var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function(event) {
    if (this.readyState === XMLHttpRequest.DONE) {
        if (this.status === 200) {
            console.log("Answer: %s", this.responseText);
        } else {
            console.log("Answer status: %d (%s)", this.status, this.statusText);
        }
    }
};  
xmlHttp.open("GET", theUrl, true);
xmlHttp.send(null);

在Chrome中我得到200

但Firefox不会执行请求(答案状态:0())

如果我用domain2.com替换domain1.com,它正在使用firefox

在我的content_scripts匹配中,我添加了domain1.com和domain2.com

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

chrome和firefox之间的区别: 在firefox中我们需要添加

"permissions": [
"activeTab",
"storage",
"*://*.domain1.com/*"

除了firefox上的chrome中的匹配

  "content_scripts": [
{
  "matches": [
    "*://*.domain2.com/*",
    "*://*.domain1.com/*"
  ]