Closing active tab won't work properly anymore, but randomly

Hey guys,

I’m having an issue with one of my addons.
Worked just fine for more than a year, but stopped doing so probably with a recent Firefox update I guess…

What I’m essentially doing is, highlighting a string within a web page, selecting from a right click context menu and passing the highlighted string towards a registry call which calls a windows batch file:

var menuItem = contextMenu.Item({
label: “SQLPlus system”,
context: contextMenu.SelectionContext(),
contentScript: ‘self.on(“click”, function () {’ +
’ var text = window.getSelection().toString();’ +
’ self.postMessage(text);’ +
’});’,
onMessage: function (selectionText){
var index = tabs.activeTab.index;
tabs.open(“sqlpluscmd://”+selection.text.trim()+"/");
//console.log('active: ’ + tabs.activeTab.url);
tabs.activeTab.close();
tabs.activeTab.index = index;
}});

Now… as mentioned, that worked just fine. Until a couple of weeks ago. The tab being closed isn’t the opened one anymore, in this case “sqlpluscmd://”+selection.text.trim()+"/", but the one where I highlighted the string in!!!

For better explenation…

  • tab0 a string is highlighted
  • tab1 gets opened with url “sqlpluscmd://”+selection.text.trim()+"/"
  • tab1 is the active tab since it’s called with tabs.open
  • tabs.activeTab.close() kicks in, but closes tab0 instead of tab1
  • tab1 stays untouched an active

And since that isn’t enough already, randomly it still closes tab0, as supposed to.
Can’t get a grabb on it… even tried making actively tab1 the active one with something like this:

tabs.open({
url: “sqlpluscmd://”+selection.text.trim()+"/",
onOpen: function onOpen(tab) {
tab.activate();
//console.log(“Tab acivated”);
}
});

Does anyone have a clue what’s going on, what has changed, what I’m doing wrong??
Appriciate any help since that addon is making my daily life at work a looot easier!!

Cheers
David

Why don’t you do something like this:

tabs.open({
    url: "sqlpluscmd://"+selection.text.trim()+"/",
    onOpen: function(tab) {
        tab.close();
    }
}

I think the change in behavior might be because of e10s.

BTW, I don’t know exactly what you are doing, but there must be a better way to trigger a uri.

Tried that, but the tab will be closed before it actually calls sqlpluscmd://"+selection.text.trim()+"/"…

I’m open for suggestions, but I found that a fairly easy way to open a tab while passing on a string.

Cheers

Ok so here is my workaround, which seems to work.
I did implement a timeout… a timeout of exactly 1 millisecond. This way the behaviour is back as expected…

Again… for any other solution I’m open eared. For an explanation as well… Martijn what did you mean by e10s btw??

var menuItem = contextMenu.Item({
label: “SQLPlus system”,
context: contextMenu.SelectionContext(),
contentScript: ‘self.on(“click”, function () {’ +
’ var text = window.getSelection().toString();’ +
’ self.postMessage(text);’ +
’});’,
onMessage: function (selectionText){
var index = tabs.activeTab.index;
tabs.open(“sqlpluscmd://”+selection.text.trim()+"/");
var { setTimeout } = require(“sdk/timers”);
setTimeout(function() {
console.log(“timeout active”);
tabs.activeTab.close();
}, 1)
tabs.activeTab.index = index;
}});

Well, have you tried onActivate instead of onOpen?

I really wouldn’t trust a timeout. It might work on your computer, but might also fire to early on someone else’s slower computer.

BTW, please format your code.

1 Like

Thx MartijnJ…

onActivate worked really well! And you’re propbably right about the timeout, which is why I chose the onActivate solution:

 var menuItem = contextMenu.Item({
  label: "SQLPlus system",
  context: contextMenu.SelectionContext(),
  contentScript: 'self.on("click", function () {' +
                 '  var text = window.getSelection().toString();' +
                 '  self.postMessage(text);' +
                 '});',
  onMessage: function (selectionText){
      var index = tabs.activeTab.index;
         tabs.open({
             url: "sqlpluscmd://"+selection.text.trim()+"/",
             onActivate: function onAct(tab) {
                 tabs.activeTab.index = index;
                 tab.close();
             }
        });
    }
});

Cheers David

Good to hear.
You might still want to checkout the request module:


You might be able to achieve the same thing without a tab opening and closing.

Will do!!!