尝试从 Firefox 插件 (SDK) 中的资源加载内容时出现安全错误

im_*_*ble 6 firefox-addon firefox-addon-sdk

我正在使用 SDK 创建一个 Firefox 插件。我的目标很简单,拦截特定的 iframe 并加载我自己的 HTML 页面(与我的插件一起打包为资源)而不是最初请求的内容。

到目前为止,我有以下代码:

var httpRequestObserver = 
{
    observe: function(subject, topic, data)
    {
        var httpChannel, requestURL;

        if (topic == "http-on-modify-request") {
            httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
            requestURL = httpChannel.URI.spec;

            var newRequestURL, i;

            if (/someurl/.test(requestURL)) {
                var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);

                httpChannel.redirectTo(ioService.newURI(self.data.url('pages/test.html'), undefined, undefined));
            }

            return;
        }
    }
};

var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
observerService.addObserver(httpRequestObserver, "http-on-modify-request", false);
Run Code Online (Sandbox Code Playgroud)

此代码的工作原理是它检测到正确的 iframe 加载并正确执行重定向。但是,我收到以下错误:

安全错误:http ://url.com 上的内容可能无法加载或链接到 jar:file:///.../pages/test.html。

我怎样才能绕过这个限制?

Noi*_*art 5

事实上,我真的想太多了。

当我改为使用 loadContext 时,它已经解决了。现在,当您获得 loadContext 时,您将获得任何浏览器元素(选项卡浏览器、框架或 iframe)的 contentWindow,然后像您正在做的那样中止 http 请求,然后loadContext.associatedWindow.document.location = self.data('pages/tests.html');

完毕

我将代码粘贴到此处,删除所有私有内容。您可能需要 chrome.manifest 进行测试并将代码粘贴回此处

Cu.import('resource://gre/modules/Services.jsm');

var httpRequestObserver = {
    observe: function (subject, topic, data) {
        var httpChannel, requestURL;

        if (topic == "http-on-modify-request") {
            httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
            requestURL = httpChannel.URI.spec;

            var newRequestURL, i;

            if (/someurl/.test(requestURL)) {
                var goodies = loadContextGoodies(httpChannel);
                if (goodies) {
                    httpChannel.cancel(Cr.NS_BINDING_ABORTED);
                    goodies.contentWindow.location = self.data.url('pages/test.html');
                } else {
                    //dont do anything as there is no contentWindow associated with the httpChannel, liekly a google ad is loading or some ajax call or something, so this is not an error
                }
            }

            return;
        }
    }
};
Services.obs.addObserver(httpRequestObserver, "http-on-modify-request", false);


//this function gets the contentWindow and other good stuff from loadContext of httpChannel
function loadContextGoodies(httpChannel) {
    //httpChannel must be the subject of http-on-modify-request QI'ed to nsiHTTPChannel as is done on line 8 "httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);"
    //start loadContext stuff
    var loadContext;
    try {
        var interfaceRequestor = httpChannel.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor);
        //var DOMWindow = interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow); //not to be done anymore because: https://developer.mozilla.org/en-US/docs/Updating_extensions_for_Firefox_3.5#Getting_a_load_context_from_a_request //instead do the loadContext stuff below
        try {
            loadContext = interfaceRequestor.getInterface(Ci.nsILoadContext);
        } catch (ex) {
            try {
                loadContext = subject.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext);
            } catch (ex2) {
            }
        }
    } catch (ex0) {
    }

    if (!loadContext) {
        //no load context so dont do anything although you can run this, which is your old code
        //this probably means that its loading an ajax call or like a google ad thing
        return null;
    } else {
        var contentWindow = loadContext.associatedWindow;
        if (!contentWindow) {
            //this channel does not have a window, its probably loading a resource
            //this probably means that its loading an ajax call or like a google ad thing
            return null;
        } else {
            var aDOMWindow = contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor)
                .getInterface(Ci.nsIWebNavigation)
                .QueryInterface(Ci.nsIDocShellTreeItem)
                .rootTreeItem
                .QueryInterface(Ci.nsIInterfaceRequestor)
                .getInterface(Ci.nsIDOMWindow);
            var gBrowser = aDOMWindow.gBrowser;
            var aTab = gBrowser._getTabForContentWindow(contentWindow.top); //this is the clickable tab xul element, the one found in the tab strip of the firefox window, aTab.linkedBrowser is same as browser var above //can stylize tab like aTab.style.backgroundColor = 'blue'; //can stylize the tab like aTab.style.fontColor = 'red';
            var browser = aTab.linkedBrowser; //this is the browser within the tab //this is where the example in the previous section ends
            return {
                aDOMWindow: aDOMWindow,
                gBrowser: gBrowser,
                aTab: aTab,
                browser: browser,
                contentWindow: contentWindow
            };
        }
    }
    //end loadContext stuff
}
Run Code Online (Sandbox Code Playgroud)

注意:现在先尝试一下,我还没有测试它,如果在尝试重定向时遇到安全错误,请创建一个 chrome.manifest 文件并将其放在根目录中。如果它引发安全错误,那么您肯定需要一个 chrome.manifest 文件,毫无疑问这将修复它。今晚晚些时候,当我有时间的时候,我会亲自测试一下。

chrome.manifest 应该如下所示:

content kaboom-data ./resources/kaboom/data/ contentaccessible=yes
Run Code Online (Sandbox Code Playgroud)

然后在上面的代码中将重定向行从 更改goodies.contentWindow.location = self.data.url('pages/test.html');goodies.contentWindow.location = 'chrome://kaboom-data/pages/test.html');