【问题标题】:How to get the active tab URL in an e10s add-on如何在 e10s 插件中获取活动标签 URL
【发布时间】:2016-09-02 05:47:38
【问题描述】:

对于工具栏按钮单击,我需要获取活动选项卡的 URL 地址。

但是

window.gBrowser.selectedBrowser.contentDocument

收到CPOW 错误。

如何在 e10s 插件中获取活动标签 URL 的 URL 位置?

【问题讨论】:

  • 你在做什么kind of Firefox extensionWebExtensionsAdd-on SDKBootstrapedOverlay/XUL/Legacy)?请edit在您的问题中添加适当的标签。从您对window.gBrowser 的使用来看,它更有可能是 Bootstraped 或 Overlay/XUL/Legacy。
  • 是的,它是 Bootstraped 或 Overlay/XUL/Legacy。我想我不能自己添加新标签?
  • 您始终可以编辑自己的问题,包括添加或删除标签。我添加了引导/无重启和覆盖的标签。

标签: firefox firefox-addon firefox-addon-restartless e10s firefox-addon-overlay


【解决方案1】:

查看可用的对象,在source code 中,您应该从哪里获取活动选项卡的 URI:

来自当前nsIURI

window.gBrowser.currentURI.spec

window.gBrowser.currentURI 对象返回一个 nsIURI,它有许多属性,您可以从中获取 URI,包括:

[nsIURI].spec //Returns a string representation of the URI. 
[nsIURI].asciiSpec //The URI spec with an ASCII compatible encoding. 
[nsIURI].specIgnoringRef //Returns a string representation of the URI without the ref
                         //  (part after the #) portion.

您还可以获得当前选定标签的nsIURI

window.gBrowser.selectedBrowser._documentURI

来自urlbar
当然,您可以将 URL 从 urlbar 中提取出来:

window.document.getElementById('urlbar').value

寻找window:
以上所有假设您已将window 适当地设置为当前活动窗口。例如,通过执行以下操作:

    //  Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
    /* Add-on SDK:
    let window = require('sdk/window/utils').getMostRecentBrowserWindow();
    //*/
    //* Overlay and bootstrap (from almost any context/scope):
    Components.utils.import("resource://gre/modules/Services.jsm"); //Services
    let window=Services.wm.getMostRecentWindow("navigator:browser");        
    //*/

【讨论】:

  • Makyen,同样的插件,但现在我需要通过工具栏按钮将 css 标签插入到活动文档的 head 元素中。没有内容脚本的 e10s 可以吗?还是我应该问新问题?
  • @JohnBernard,这是一个新问题。但是,您认为它需要某种内容脚本是正确的。活动文档与后台脚本的分离是对多进程 (e10s) 插件的主要影响之一。在不使用内容脚本的情况下,只有非常有限的信息量(例如上面的 URL)可供后台进程使用。
  • 非常感谢@Makyen。赞赏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多