禁用Chrome本地域的同源策略



我正在尝试在我的Mac OS 10.10机器上使用Chrome 45.0.2454.85(64位)对我正在开发的扩展进行本地*.dev样式域的跨源请求。

我无法通过testbox.dev获得消息,因为每次我执行以下代码时,我都会获得status0值,而responseText总是空的。当尝试这些连接时,检查后台页面的视图显示控制台错误net::ERR_CONNECTION_REFUSED

我尝试关闭所有的Chrome实例,然后使用命令open -a Google Chrome --args --disable-web-security重新启动,但仍然不工作。

我尝试了CORS Chrome扩展,所以我至少可以在本地服务器上测试,但这不起作用。

尝试用https://www.corsproxy.com/前缀我的api.example.com URL,但请求从未完成。

尝试使用cors-anywhere.herokuapp.com前缀,但我得到错误origin header required。为了解决这个问题,我尝试使用xhr.setRequestHeader('Origin', http + '//' + window.location.host);发送原点头,但Chrome不允许我继续错误Refused to set unsafe header "Origin"

我尝试添加以下响应到我的服务器的Laravel控制器方法,但没有帮助:

return Response::json($stock, 200, ['Access-Control-Allow-Origin' => '*']);

manifest.json:

{
    "name": "__MSG_appName__",
    "version": "1.0.0",
    "manifest_version": 2,
    "description": "__MSG_appDescription__",
    "icons": {
        "16": "images/icon-16.png",
        "48": "images/icon-48.png",
        "128": "images/icon-128.png"
    },
    "default_locale": "en",
    "background": {
        "scripts": [
            //"scripts/chromereload.js"
            "scripts/background.js"
        ],
        "persistent": false
    },
    "browser_action": {
        "default_icon": {
            "16": "images/icon-16.png",
            "32": "images/icon-32.png",
            "38": "images/icon-38.png",
            "48": "images/icon-48.png",
            "64": "images/icon-64.png",
            "128": "images/icon-128.png"
        },
        "default_title": "Workflow Enhancer"
    },
    "options_page": "options.html",
    "content_scripts": [
        {
            "matches": [
                "http://www.example.com/*",
                "https://www.example.com/*",
                "https://*.freshbooks.com/*",
                "https://*.highrisehq.com/*"
            ],
            "css": [
                "styles/content.css"
            ],
            "js": [
                "scripts/jquery.min.js",
                "scripts/xhrproxy.js",
                "scripts/content.js"
            ],
            "run_at": "document_end",
            "all_frames": false
        }
    ],
    "permissions": [
        "activeTab",
        "<all_urls>",
        "http://*.dev/*",
        "https://*.dev/*",
        "http://testbox.dev/*",
        "https://testbox.dev/*",
        "http://*.example.com/*",
        "https://*.example.com/*"
    ],
    "web_accessible_resources": [
        "*"
    ]
}

background.js

chrome.extension.onConnect.addListener(function(port) {
    if (port.name != 'XHRProxy_')
        return;
    port.onMessage.addListener(function(xhrOptions) {
        var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
        var xhr = new XMLHttpRequest();
        xhr.open(xhrOptions.method || "GET", http + xhrOptions.url, true);
        //xhr.setRequestHeader('Origin', http + '//' + window.location.host);
        xhr.setRequestHeader('X-Requested-With', 'XHRProxy');
        xhr.setRequestHeader('X-API-key', 'JSFLIESLIFDFDHSLFEHSLFHSFH');
        xhr.onreadystatechange = function() {
            if (this.readyState == 4) {
                port.postMessage({
                    status : this.status,
                    data   : this.responseText,
                    xhr    : this
                });
            }
        };
        xhr.send();
    });
});

xhrproxy.js

var proxyXHR = {};
proxyXHR.get = function (url) {
    var port     = chrome.extension.connect({ name: 'XHRProxy_' });
    var settings = {
        method : 'GET',
        url    : url
    };
    var onSuccess;
    var onFailure;
    var self = {
        onSuccess: function (callback) {
            onSuccess = callback;
            return self;
        },
        onFailure: function (callback) {
            onFailure = callback;
            return self;
        }
    };
    port.onMessage.addListener(function (msg) {
        if (msg.status === 200 && typeof onSuccess === 'function') {
            onSuccess(msg.data, msg.xhr);
        } else if (typeof onFailure === 'function') {
            onFailure(msg.data, msg.xhr);
        }
    });
    port.postMessage(settings);
    return self;
};

content.js

// Localhost test domain.
proxyXHR.get('testbox.dev/api/XYZ/quantity')
            .onSuccess(function (data) {
                console.log(data);
            })
            .onFailure(function (data, xhr) {
                console.log("HTTP Error while retrieving data.", data, xhr.status);
            });
// Production server domain....produces same error as local domain test above.
proxyXHR.get('api.example.com/api/XYZ/quantity')
            .onSuccess(function (data) {
                console.log(data);
            })
            .onFailure(function (data, xhr) {
                console.log("HTTP Error while retrieving data.", data, xhr.status);
            });

如果我将URL从testbox.dev更改为我的生产URL api.example.com,我仍然会得到相同的跨源拒绝。

你知道这是怎么回事吗?

net::ERR_CONNECTION_REFUSED不是交叉原点错误。您可能有一些问题与您的https连接。您可以确保您的服务器上有正确的证书,或者切换到http。

注意下面这行:

    var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');

在背景页面中没有多大意义,因为协议将始终是chrome-extension:

最新更新