因此,我收集了 5 种不同的方法来执行此操作,但没有一种方法可以在面板内工作。 Firefox 在阻止对基本任务的访问方面非常有效。
这是我尝试过的:
-
尝试 1:
var url = window.top.getBrowser().selectedBrowser.contentWindow.location.href;
错误:window.top.getBrowser 不是函数
尝试 2:
var url = window.content.document.location;
错误:访问属性“文档”的权限被拒绝
尝试 3:
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow);
var url = mainWindow.getBrowser().selectedBrowser.contentWindow.location.href;
错误:为 UnnamedClass 类的对象创建包装器的权限被拒绝
尝试 4:
var url = window.content.location.href;
错误:访问属性“href”的权限被拒绝
尝试 5:
var currentWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");
var currBrowser = currentWindow.getBrowser();
var url = currBrowser.currentURI.spec;
错误:获取属性 XPCComponents.classes 的权限被拒绝
在 Chrome 中编写此代码非常简单。不知道为什么这对 FF 来说如此困难。
有人有解决办法吗?
So I've collected 5 different methods to do this, none of which work from within a panel. Firefox is stunningly effective at blocking access to a basic task.
Here's what I've tried:
-
Attempt 1:
var url = window.top.getBrowser().selectedBrowser.contentWindow.location.href;
Error: window.top.getBrowser is not a function
-
Attempt 2:
var url = window.content.document.location;
Error: Permission denied to access property 'document'
-
Attempt 3:
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow);
var url = mainWindow.getBrowser().selectedBrowser.contentWindow.location.href;
Error: Permission denied to create wrapper for object of class UnnamedClass
-
Attempt 4:
var url = window.content.location.href;
Error: Permission denied to access property 'href'
-
Attempt 5:
var currentWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");
var currBrowser = currentWindow.getBrowser();
var url = currBrowser.currentURI.spec;
Error: Permission denied to get property XPCComponents.classes
Coding this for Chrome was a breeze. Not sure why this is so tough for FF.
Anyone got a solution?
发布评论
评论(2)
我认为你可以使用 Firefox 本地对象:
I think you can use firefox local object:
我猜“Firefox Addon panel”指的是Addon SDK的 面板模块?
如果是这样,您可能正在尝试在 内容脚本。相反,您必须将事件发送到主插件的代码(示例),并在主插件的代码中使用 选项卡模块:
[更新]完整的测试用例,对我有用:
I guess "Firefox Addon panel" refers to the Addon SDK's panel module?
If so you're probably trying to use those snippets in a content script. Instead you have to send an event to the main addon's code (example), and in the main addon's code use the tabs module:
[update] complete testcase, which works for me: