Closed Bug 975623 Opened 10 years ago Closed 10 years ago

active tab is null when reloading pagemod using menuitems

Categories

(Add-on SDK Graveyard :: General, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED WORKSFORME

People

(Reporter: nick, Unassigned)

Details

I use Myk's fork of menuitems [0] along with page mod to mode the currently active tab in my addon [1].  Occasionally, when I reload the page, my add on ceases to function until I close Firefox.

The stack trace looks like:

console.error: webglinspector:
  Message: Error: The `include` option must always contain atleast one rule
  Stack:
    PageMod@resource://gre/modules/XPIProvider.jsm -> jar:file:///Users/Nicholas/mozilla/profiles/r2d2b2g/extensions/jid1-wzJMQZdgCoDRJA@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/page-mod.js:105
Trait@resource://gre/modules/XPIProvider.jsm -> jar:file:///Users/Nicholas/mozilla/profiles/r2d2b2g/extensions/jid1-wzJMQZdgCoDRJA@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/deprecated/traits.js:114
exports.PageMod@resource://gre/modules/XPIProvider.jsm -> jar:file:///Users/Nicholas/mozilla/profiles/r2d2b2g/extensions/jid1-wzJMQZdgCoDRJA@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/page-mod.js:211
.onCommand@resource://gre/modules/XPIProvider.jsm -> jar:file:///Users/Nicholas/mozilla/profiles/r2d2b2g/extensions/jid1-wzJMQZdgCoDRJA@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-wzjmqzdgcodrja-at-jetpack/webglinspector/lib/main.js:16
lazy@resource://gre/modules/XPIProvider.jsm -> jar:file:///Users/Nicholas/mozilla/profiles/r2d2b2g/extensions/jid1-wzJMQZdgCoDRJA@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/event/core.js:110
emit@resource://gre/modules/XPIProvider.jsm -> jar:file:///Users/Nicholas/mozilla/profiles/r2d2b2g/extensions/jid1-wzJMQZdgCoDRJA@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/event/core.js:83
addMenuitems/<.onTrack/<@resource://gre/modules/XPIProvider.jsm -> jar:file:///Users/Nicholas/mozilla/profiles/r2d2b2g/extensions/jid1-wzJMQZdgCoDRJA@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-wzjmqzdgcodrja-at-jetpack/menuitems/lib/menuitems.js:96


Why would the active tab be undefined during a reload?

[0] https://github.com/mykmelez/menuitems-jplib
[1] https://github.com/benvanik/WebGL-Inspector/blob/master/core/extensions/firefox/lib/main.js
Summary: curious bug when reloading pagemod uing menuitems → active tab is null when reloading pagemod using menuitems
it's not activeTab that is undefined (that would throw a reference error), it's `activeTab.url` that is not available during certain periods of the tab life-cycle. it is known, expected behavior:

https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs#Manipulate_a_tab

for some tips how to overcome that problem, check out other parts of documentation:

https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs#open

btw, your whole approach might not be the best way to do this, as that PageMod would attach to all tabs that have the same url. you might wanna look at the tab.attach() method:

https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs#attach%28options%29
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → WORKSFORME
:zombie, thanks for replying.  The tab.attach method does not allow for attaching CSS to content.  My add-on injects one CSS file and 2 JS files.
Resolution: WORKSFORME → FIXED
Resolution: FIXED → WORKSFORME
Also, I need to guarantee that my scripts are injected before any other scripts can execute.
Disregard comment #2.  I don't understand why tabs.activeTab.url would be null if the tab was loaded when the page mod was constructed (unless the PageMod constructor is asynchronous, and I'm reloading the tab before the PageMod is constructed).
Anyways, I need to work on better steps to reproduce.
hey Nick, sorry if you think this issue isn't resolved, feel free to re-open the bug, with more specific steps to reproduce.

> attaching CSS to content.  My add-on injects one CSS file and 2 JS files.
you can attach your CSS using standard DOM methods 

> Also, I need to guarantee that my scripts are injected before any other
> scripts can execute.
i don't think you can get that kind of guarantee with high-level APIs like this, as all this is done using (async?) events under the hood. and even if it works today, there is no guarantee it wont break some time in the near future (and especially with e10s).

> Disregard comment #2.  I don't understand why tabs.activeTab.url would be
> null if the tab was loaded when the page mod was constructed (unless the
> PageMod constructor is asynchronous, and I'm reloading the tab before the
> PageMod is constructed).
PageMod constructor isn't asynchronous, but there is no guarantee in what state the tab/document is when that `MenuItem.onCommand` is called. you could test `activTab.url`, and if it is undefined, set up onReady event handler to do your business then.
> set up onReady event handler to do your business then.

It's too late at that point.  From MDN [0]:

> This event is emitted when the DOM for a tab's content is ready. It is equivalent to the DOMContentLoaded event for the given content page.

I need my code to run before any other script, as it monkey patches HTMLCanvasElement.prototype.getContext.

[0] https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs#ready
as far as i understand your code and your stated intentions:
1) on menu click, 
2) you set up s PageMod to attach to the activeTab (via the url), 
3) and then reload the tab, 
4) which executes your content-script as early as possible (contentScriptWhen: start).

your problem is happening in step 2, when activeTab.url is sometimes undefined. if you detect that condition, and then delay that and the next step to happen in the onReady event, your content script will still execute as soon as possible after the tab reload.

if i didn't explain that clearly, here is a quick PR on github:
https://github.com/benvanik/WebGL-Inspector/pull/107

(sorry for not testing this, building the extension has lots of dependencies, one of which seems to be unix)
Ah, thanks for clarifying and for the PR!
You need to log in before you can comment on or make changes to this bug.