javascript - Firefox Add-on SDK getting the tab ID -


i'm trying build firefox add-on using sdk (version 1.6), i'm running issue tabs extension opening.

i obtain tab acontext (node) on. so, i've been 'getting' node's window, using tab utils in sdk, gettabforcontentwindow(). doesn't work, tab returned gettabforcontentwindow() null. there better, more robust method node's tab?

also, noticed on the tab utils page, states it's 'unstable'. should avoid using tab utils sdk?

below code 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);         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; } 

you should keep in mind that:

  • not requests originate window. (background requests)
  • not requests originate window originate tab. when run code can see e.g. requests stuff chrome://browser/skin/tabbrowser/tab-separator.png browser window loads (in case used in ui). keep in mind not top-level windows browser windows (browser.xul).
  • even if tab navigation, the initial load (acontentlocation.spec [http://wikipedia.org/]) originate new, not initialized content window being constructed, is not yet associated tab. tab @ point still about:blank , new window (wikipedia) swapped in once clear window can constructed: request wasn't blocked, dns resolved, redirects followed , final response produced data , data can made window. e.g. if network down, wikipedia window abandoned before ever being assigned tab , instead connection error document , window created.

the last point kinda documented in the nsicontentpolicy interface itself:

 /**    * should resource @ location loaded?    * shouldload called before loading resource @ acontentlocation    * determine whether start load @ all.    *    * @param acontenttype      type of content being tested. 1    *                          1 of type_* constants.    *    * @param acontentlocation  location of content being checked; must    *                          not null    *    * @param arequestorigin    optional. location of resource    *                          initiated load request; can null if    *                          inapplicable    *    * @param acontext          optional. nsidomnode or nsidomwindow    *                          initiated request, or can qi    *                          1 of those; can null if inapplicable.    *                          note navigation events (new windows ,    *                          link clicks), new window.    *    * @param amimetypeguess    optional. guess requested content's    *                          mime type, based on information available    *                          request initiator (e.g., object's type    *                          attribute); not reliably reflect    *                          actual mime type of requested content    *    * @param aextra            optional argument, pass-through non-gecko    *                          callers pass data callees.    *    * @param arequestprincipal optional argument, defines principal    *                          caused load. optional    *                          non-gecko code: gecko code should set    *                          argument.  navigation events,    *                          principal of page caused load.    *    * @return accept or reject_*    *    * @note shouldload can called while dom , layout of document    * involved in inconsistent state.  means implementors of    * method must not of following:    * 1)  modify dom in way (e.g. setting attributes no-no).    * 2)  query dom properties depend on layout (e.g. offset*    *     properties).    * 3)  query dom properties depend on style (e.g. computed style).    * 4)  query dom properties depend on current state of dom    *     outside "context" node (e.g. lengths of node lists).    * 5)  [javascript implementations only] access properties of sort on    *     object without using xpcnativewrapper (either explicitly or    *     implicitly).  due various dom0 things, leads item 4.    * if of these things in shouldload implementation, expect    * unpredictable behavior, possibly including crashes, content not showing    * up, content showing doubled, etc.  if need of things    * above, 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); 

conclusion

the tab utils work expected far can tell, , code @notidart's answer. expectation new document assigned tab wrong.

also, noticed on tab utils page, states it's 'unstable'. should avoid using tab utils sdk?

well, personal taste. unstable means api can change again or removed again in later version of browser. alternatives, using window.gbrowser @ least unstable sdk api (also can change @ point), imho there nothing gained avoiding sdk api, provided doesn't turned out buggy in way (but if does, can still switch else).


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -