我正在尝试使用SDK(版本1.6)构建Firefox加载项,但我遇到了扩展程序正在打开的Tabs问题。
我想获取aContext
(节点)所在的标签。要做到这一点,我一直在'节点的窗口,然后使用SDK中的Tab Utils,特别是getTabForContentWindow()
。这有时不起作用,从getTabForContentWindow()
返回的选项卡为空。是否有更好,更健壮的方法来获取节点的标签?
另外,我在the Tab Utils page注意到,它声明它“不稳定”。我应该避免使用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;
}
答案 0 :(得分:2)
这是您可以尝试的方式。从内容窗口中,您将获得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窗口。
答案 1 :(得分:1)
你应该记住:
chrome://browser/skin/tabbrowser/tab-separator.png
之类的东西(在这种情况下用于UI)。另请注意,并非所有顶级窗口都是浏览器窗口(browser.xul
)。aContentLocation.spec [http://wikipedia.org/]
)也会来自正在构建的新的未完全初始化的内容窗口,是尚未与任何标签相关联。此时的标签仍然是about:blank
,只有在明确可以完全构建此窗口时,才会交换新窗口(wikipedia
):请求未被阻止,DNS为已解决,重定向随后,最终响应实际产生数据,并且数据可以制作成窗口。例如。如果我的网络出现故障,wikipedia
窗口将在被分配到选项卡之前被放弃,而是创建连接错误文档和窗口。最后一点也是the nsIContentPolicy
interface本身记录的 kinda :
/**
* 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页面上注意到,它表示它不稳定了#39;我应该避免使用Tab Utils SDK吗?
嗯,这取决于您的个人品味。 unstable 本质上意味着API可以再次更改,甚至可以在更高版本的浏览器中再次删除。使用window.gBrowser
的替代方法至少与SDK API一样不稳定(也可以随时更改),因此恕我直言,没有什么可以避免这个SDK API,提供它在某种程度上并没有变成越野车(但如果确实如此,你仍然可以切换到别的东西)。