繁体   English   中英

Firefox Add-on SDK获取选项卡ID

[英]Firefox Add-on SDK getting the tab ID

我正在尝试使用SDK(版本1.6)构建Firefox加载项,但是我遇到了扩展程序正在打开的Tabs问题。

我想获得aContext (节点)所在的选项卡。 为此,我一直在“获取”节点的窗口,然后使用SDK中的Tab Utils,特别是getTabForContentWindow() 这有时不起作用,从getTabForContentWindow()返回的Tab为null。 是否有更好,更强大的方法来获取节点的Tab?

此外,我在Tab Utils页面上注意到它表示它“不稳定”。 我应该避免使用Tab Utils SDK吗?

以下是main.js中的代码:

const {Cc, Ci, Cr, Cu, Cm, components} = require("chrome");
const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm");
var winUtils = require('sdk/window/utils');
var tabUtils = require('sdk/tabs/utils');

let policy =
{
    classDescription: "my content policy",
    classID: components.ID("{2DA54ECA-FBDD-11E3-B3B1-695C1D5D46B0}"),
    contractID: "@www.com/policy;1",
    xpcom_categories: ["content-policy"],

    init: function()
    {
        let registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
        registrar.registerFactory(this.classID, this.classDescription, this.contractID, this);

        let catMan = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
        for each (let category in this.xpcom_categories)
            catMan.addCategoryEntry(category, this.contractID, this.contractID, false, true);
    },

    // nsIContentPolicy interface implementation
    shouldLoad: function(aContentType, aContentLocation, aRequestOrigin, aContext, aMimeTypeGuess, aExtra, aRequestPrincipal) {

        console.log("*****");
        console.log("aContentLocation.spec [" + aContentLocation.spec + "] ");
        console.log("aContentType [" + aContentType + "] ");
        if (aContext instanceof components.interfaces.nsIDOMNode) {            
            var node = aContext.QueryInterface(components.interfaces.nsIDOMNode);
            var win = getWindow(node);
            if (win) {
                console.log("window found" );
                var selectedTab = tabUtils.getTabForContentWindow(win);                
                if (selectedTab) {
                    console.log("tab found" );
                    var tabId = tabUtils.getTabId(selectedTab);
                    console.log("Node's tabId:" + tabId);
                } else {
                    console.log("tab undefined" );
                }
            } else {
                console.log("win undefined" );
            } 
        }
        return Ci.nsIContentPolicy.ACCEPT;

    },

    shouldProcess: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra) {
        return Ci.nsIContentPolicy.ACCEPT;
    },

    // nsIFactory interface implementation
    createInstance: function(outer, iid) {
        if (outer)
            throw Cr.NS_ERROR_NO_AGGREGATION;
        return this.QueryInterface(iid);
    },

    // nsISupports interface implementation
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIFactory])
};

policy.init();

var scheduleCheckFilterUpdates = function() {
    var tabs = require("sdk/tabs");
    tabs.open("http://wikipedia.org");
}
require('sdk/timers').setTimeout(scheduleCheckFilterUpdates, 1000);

function getWindow(node) {
    if ("ownerDocument" in node && node.ownerDocument)
        node = node.ownerDocument;

    if ("defaultView" in node)
        return node.defaultView;

    return null;
}

这是你可以尝试的一种方式。 从内容窗口中,您将获得dom窗口。 然后使用dom窗口执行getTabForContentWindow:

//var node = focused element?
var contentWin = node.ownerDocument.defaultView;
var DOMWin = contentWin.QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIWebNavigation)
    .QueryInterface(Ci.nsIDocShellTreeItem)
    .rootTreeItem
    .QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIDOMWindow);
if (DOMWin.gBrowser && DOMWin.gBrowser.tabContainer) {
    var tab = DOMWin.gBrowser._getTabForContentWindow(contentWin);
} else {
    //the node is in a firefox window with no tabs
}

它可能是一个没有标签的poup窗口。

你应该记住:

  • 并非所有请求都来自窗口。 (背景要求)
  • 并非所有源自窗口的请求实际上都来自选项卡。 当您运行代码时,您可以看到例如chrome://browser/skin/tabbrowser/tab-separator.png浏览器窗口本身加载的内容请求(在这种情况下将在UI中使用)。 另请注意,并非所有顶级窗口都是浏览器窗口( browser.xul )。
  • 即使它是一个标签导航,那么初始加载aContentLocation.spec [http://wikipedia.org/] )将来自正在构建的新的未完全初始化的内容窗口,该窗口尚未与任何关联标签 此时的选项卡仍然about:blank ,只有在显然可以完全构建此窗口时才会交换新窗口( wikipedia ):请求未被阻止,DNS已解决,重定向随后和最终响应实际产生的数据和数据可以成为一个窗口。 例如,如果我的网络关闭, wikipedia窗口将在被分配到选项卡之前被放弃,而是创建连接错误文档和窗口。

最后一点也有点记录在nsIContentPolicy接口本身:

 /**
   * Should the resource at this location be loaded?
   * ShouldLoad will be called before loading the resource at aContentLocation
   * to determine whether to start the load at all.
   *
   * @param aContentType      the type of content being tested. This will be one
   *                          one of the TYPE_* constants.
   *
   * @param aContentLocation  the location of the content being checked; must
   *                          not be null
   *
   * @param aRequestOrigin    OPTIONAL. the location of the resource that
   *                          initiated this load request; can be null if
   *                          inapplicable
   *
   * @param aContext          OPTIONAL. the nsIDOMNode or nsIDOMWindow that
   *                          initiated the request, or something that can QI
   *                          to one of those; can be null if inapplicable.
   *                          Note that for navigation events (new windows and
   *                          link clicks), this is the NEW window.
   *
   * @param aMimeTypeGuess    OPTIONAL. a guess for the requested content's
   *                          MIME type, based on information available to
   *                          the request initiator (e.g., an OBJECT's type
   *                          attribute); does not reliably reflect the
   *                          actual MIME type of the requested content
   *
   * @param aExtra            an OPTIONAL argument, pass-through for non-Gecko
   *                          callers to pass extra data to callees.
   *
   * @param aRequestPrincipal an OPTIONAL argument, defines the principal that
   *                          caused the load. This is optional only for
   *                          non-gecko code: all gecko code should set this
   *                          argument.  For navigation events, this is
   *                          the principal of the page that caused this load.
   *
   * @return ACCEPT or REJECT_*
   *
   * @note shouldLoad can be called while the DOM and layout of the document
   * involved is in an inconsistent state.  This means that implementors of
   * this method MUST NOT do any of the following:
   * 1)  Modify the DOM in any way (e.g. setting attributes is a no-no).
   * 2)  Query any DOM properties that depend on layout (e.g. offset*
   *     properties).
   * 3)  Query any DOM properties that depend on style (e.g. computed style).
   * 4)  Query any DOM properties that depend on the current state of the DOM
   *     outside the "context" node (e.g. lengths of node lists).
   * 5)  [JavaScript implementations only] Access properties of any sort on any
   *     object without using XPCNativeWrapper (either explicitly or
   *     implicitly).  Due to various DOM0 things, this leads to item 4.
   * If you do any of these things in your shouldLoad implementation, expect
   * unpredictable behavior, possibly including crashes, content not showing
   * up, content showing up doubled, etc.  If you need to do any of the things
   * above, do them off timeout or event.
   */
  short shouldLoad(in nsContentPolicyType aContentType,
                   in nsIURI        aContentLocation,
                   in nsIURI        aRequestOrigin,
                   in nsISupports   aContext,
                   in ACString      aMimeTypeGuess,
                   in nsISupports   aExtra,
                   [optional] in nsIPrincipal  aRequestPrincipal);

结论

据我所知,Tab Utils按预期工作,@ Notidart的答案中的代码也是如此。 期望将新文档立即分配给错误的选项卡。

此外,我在Tab Utils页面上注意到它表示它“不稳定”。 我应该避免使用Tab Utils SDK吗?

嗯,这取决于你的个人品味。 unstable本质上意味着API可以再次更改,甚至可以在更高版本的浏览器中再次删除。 替代方案,比如使用window.gBrowser至少和SDK API一样不稳定 (也可以在任何时候改变),所以恕我直言,没有什么可以避免这个SDK API,只要它没有变成bug在某种程度上(但如果确实如此,你仍然可以切换到别的东西)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM