我正在使用 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);
此代码的工作原理是它检测到正确的 iframe 加载并正确执行重定向。但是,我收到以下错误:
Security Error: Content at http://url.com may not load or link to jar:file:///.../pages/test.html.
我怎样才能绕过这个限制?
最佳答案
实际上,伙计,我真的想太多了。
当我改为使用 loadContext 时,它已经解决了。现在,当您获得 loadContext 时,您将获得任何浏览器元素(选项卡浏览器、框架或 iframe)的 contentWindow,然后像您正在做的那样中止 http 请求,然后 loadContext.associatedWindow.document.location = self.data('pages/tests.html');
完毕
将代码粘贴到此处,删除所有私有(private)内容。您可能需要 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
}
注意:现在先试试这个,我还没有测试它,如果你在尝试重定向时遇到安全错误,那么创建一个 chrome.manifest 文件并将其放在根目录中。如果它引发了安全错误,那么您肯定需要一个 chrome.manifest 文件,这将毫无疑问地修复它。今晚晚些时候当我有时间时,我会自己测试一下。
chrome.manifest 应该如下所示:
content kaboom-data ./resources/kaboom/data/ contentaccessible=yes
然后在上面的代码方式中将重定向行从
goodies.contentWindow.location = self.data.url('pages/test.html');
至goodies.contentWindow.location = 'chrome://kaboom-data/pages/test.html');
.
关于firefox-addon - 尝试从 Firefox 插件 (SDK) 中的资源加载内容时出现安全错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21947483/