从 Firefox Addon 面板中获取当前 URL

发布于 2024-12-11 16:55:25 字数 1490 浏览 0 评论 0 原文

因此,我收集了 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨离汐 2024-12-18 16:55:25

我认为你可以使用 Firefox 本地对象:

var url = gBrowser.contentDocument.location;

I think you can use firefox local object:

var url = gBrowser.contentDocument.location;
水溶 2024-12-18 16:55:25

我猜“Firefox Addon panel”指的是Addon SDK的 面板模块?

如果是这样,您可能正在尝试在 内容脚本。相反,您必须将事件发送到主插件的代码(示例),并在主插件的代码中使用 选项卡模块

require("tabs").activeTab.url

[更新]完整的测试用例,对我有用:

// http://stackoverflow.com/questions/7856282/get-current-url-from-within-a-firefox-addon-panel
exports.main = function() {
  var panel = require("panel").Panel({
    contentURL: "data:text/html,<input type=button value='click me' id='b'>",
    contentScript: "document.getElementById('b').onclick = " +
                   "function() {" +
                   "  self.port.emit('myEvent');" +
                   "}"
  });
  panel.port.on("myEvent", function() {
    console.log(require("tabs").activeTab.url)
  })
  require("widget").Widget({
    id: "my-panel",
    contentURL: "http://www.google.com/favicon.ico",
    label: "Test Widget",
    panel: panel
  });  
};

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:

require("tabs").activeTab.url

[update] complete testcase, which works for me:

// http://stackoverflow.com/questions/7856282/get-current-url-from-within-a-firefox-addon-panel
exports.main = function() {
  var panel = require("panel").Panel({
    contentURL: "data:text/html,<input type=button value='click me' id='b'>",
    contentScript: "document.getElementById('b').onclick = " +
                   "function() {" +
                   "  self.port.emit('myEvent');" +
                   "}"
  });
  panel.port.on("myEvent", function() {
    console.log(require("tabs").activeTab.url)
  })
  require("widget").Widget({
    id: "my-panel",
    contentURL: "http://www.google.com/favicon.ico",
    label: "Test Widget",
    panel: panel
  });  
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文