Reputation: 12803
I'd like for my addon to reload an internal page (index.html) upon browser startup, if its tab exists. This is the code I am using, but nothing happens at all. Am I doing the right thing? The docs seem to say that this is the proper way to do it.
exports.main = function(options) {
var tabs = require('sdk/tabs');
if (options.loadReason === 'startup') {
for (var i = tabs.length - 1; i >= 0; i--) {
var tab = tabs[i];
if (tab.url !== self.data.url('index.html')) {
continue;
}
tab.once('ready', runScript.bind(null, tab));
tab.reload();
}
}
};
It is hard to debug because when doing cfx run
the loadReason is always install
.
Upvotes: 2
Views: 141
Reputation: 16558
Use const { loadReason } = require("sdk/self");
and check if (loadReason == "startup")
Upvotes: 0
Reputation: 33192
Hmm. Tested this and it appears that at the time main()
is called the session restore changes are not yet propagated (in a way the SDK understands).
Using a setTimeout(..., 0)
fixed that behavior:
const self = require("sdk/self");
const selfTabUrl = self.data.url("index.html");
console.log("self", selfTabUrl);
function runScript(tab) {
console.log("runScript", tab.url);
}
function reloadTab(options) {
var tabs = require('sdk/tabs');
for (var i = tabs.length - 1; i >= 0; i--) {
var tab = tabs[i];
if (tab.url !== selfTabUrl) {
continue;
}
tab.once('ready', runScript.bind(null, tab));
tab.reload();
console.log("reload", tab.url);
return true;
}
return false;
}
exports.main = function(options) {
if (options.loadReason === 'startup') {
if (!reloadTab()) {
console.log("didn't find a tab on first attempt; retrying");
require("sdk/timers").setTimeout(reloadTab, 0);
}
}
};
This works in Firefox Stable (29) and avoids the setTimeout
should the behavior be changed later.
Regarding the debugging:
To avoid having cfx run
setting up and re-installing (instead of re-using a profile and just starting the extension) use the --profiledir=
switch, e.g. (on a *nix system)
$ mkdir p # run once
$ cfx run --profiledir=$PWD/p
Upvotes: 3