1

I'm writing a restartless Firefoxextension where I have to enumerate all open tabs and work with them.

Here's the code-part that throws the error:

getInfoString : function ()
    {
        infos = "";
        HELPER.alerting("url", "URL-Function");
        var winMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
        HELPER.alerting("url", "Mediator initialized");
        var mrw = winMediator.getEnumerator(null);
        while(mrw.hasMoreElements())
        {
            var win = mrw.getNext();
            var t = win.gBrowser.browsers.length;
            HELPER.alerting("url", "browsers: " + t);
            for (var i = 0; i < t; i++)
            {
                var b = win.gBrowser.getBrowserAtIndex(i);
                if(b.currentURI.spec.substr(0,3) != "http")
                {
                    continue;
                }
                HELPER.alerting(b.title,b.currentURI.spec);
                var doc = b.contentDocument;
                var src = doc.documentElement.innerHTML;
                infos = infos + src
                HELPER.alerting("doc", src);
            }
        }

        return infos;
    }

I have a JavascriptDebugger-Addon running while testing this and Firefox executes everything fine to the line

HELPER.alerting("url", "browsers: " + t);

But AFTER this line, the debugger-addons throws an error, saying that:

win.gBrowser is undefined

... pointing to the line:

var t = win.gBrowser.browsers.length;

But before it throws the error I get my alertmessage which gives me the correct number of tabs. So the error is thrown after the line was executed and not directly WHEN it was executed. Does anyone has an idea how to fix this, because the extension stops working after the error has been thrown.

Greetz

P.S.: If someone has a better headline for this, feel free to edit it.

nmaier
  • 32,336
  • 5
  • 63
  • 78
Husky110
  • 723
  • 2
  • 10
  • 27
  • I won't add an answer because I know nothing about Firefox extensions but this [mdn article](https://developer.mozilla.org/en-US/docs/Code_snippets/Tabbed_browser) seems to talk about this. – Álvaro González Nov 05 '13 at 13:20
  • it is a function from there, just a bit adjusted. ;) – Husky110 Nov 05 '13 at 14:23

2 Answers2

1

Using winMediator.getEnumerator(null) would give you all types of window, that may or may not be browser windows. You should try changing the following line

var mrw = winMediator.getEnumerator(null);

with

var mrw = winMediator.getEnumerator('navigator:browser');
Kashif
  • 1,238
  • 10
  • 15
-1

I finally figured out that this behavior can happen sometimes. I just rearranged the code a bit, removing some alerts inside the for-loop and it works just fine again. So if someone has this error too, just rearrange your code and it should work like a charm again.

Husky110
  • 723
  • 2
  • 10
  • 27
  • 1
    Sorry, this answer is not correct. See the answer @Kashif provided. `alert` will create a window itself, BTW, a window that doesn't have `gBrowser`. However there are many other window types that a user could have open, most of which won't have `gBrowser` as well. – nmaier Nov 05 '13 at 15:02
  • sure. I didn't say that his answer wasn't correct. It just didn't match to my error. Rearrange fixed it, believe it or not. – Husky110 Nov 05 '13 at 15:05
  • Btw it is not a nice behavior to down-vote an answer that worked, just because you don't "like" it. – Husky110 Nov 05 '13 at 15:06
  • Then open for example the Browser Console, or a Scratchpad and rerun your code to see it burn again. And no, your answer is wrong not just something I don't like. I don't down-vote lightly. – nmaier Nov 05 '13 at 15:06
  • the error was, that i had no idea why it would throw an error for a line thet was already processed a line before there error has been thrown and it worked it out. – Husky110 Nov 07 '13 at 11:31