Aaron, you're a rock star :).  Apart from the asynchronous problem,
I also see now that I had another error in my code:

I was saying window.windowId where I should have said window.id.
Only for "tab" objects should one use tab.windowId.

Thanks

On Oct 28, 6:00 pm, Aaron Boodman <[email protected]> wrote:
> cc:chromium-extensions
> bcc:chromium-discuss
>
>
>
>
>
> On Wed, Oct 28, 2009 at 5:55 PM, reikred <[email protected]> wrote:
>
> > Alright, so I bit the bullet and turned my javascript into an
> > extension so that I can use chrome.windows and chrome.tabs API.
>
> > Now the problem is that new tabs come up in the same window where I'm
> > running the extension, rather in a new window I just created. Keep in
> > mind, I'm going to create a bunch of windows, each with a bunch of
> > tabs, so this has to work.
>
> > Here's an example of the main code of the extension, for a simple case
> > where I want to open one new window and then add a second tab to that
> > window:
>
> > <script type="text/javascript">
> > chrome.windows.create({url: "http://www.google.com/"});
> > chrome.windows.getLastFocused(function (window) {
> >    chrome.tabs.create({url: "http://www.gmail.com/";, windowId:
> > window.windowId});
> > });
> > </script>
>
> > I'm trying to do this using getLastFocused() and a callback, since I
> > could not figure out how to pass the window.windowId out of the first
> > windows.create call. The result is not what I wanted: The "gmail" tab
> > ends up in the window where I run the extension, rather next to the
> > new "google" tab in the new window.
>
> > Any ideas?? I'm totally lost here.
>
> All the windows and tabs APIs are asynchronous and take a callback.
> The callback is fired when the function is complete. If you execute
> code before the callback is run, you will see the state of the world
> before the API you requested has been executed.
>
> So in this case, when you call getLastFocused() you get the current
> window because the window you asked to be created hasn't been yet. In
> order to make this work, you should move the call to getLastFocused()
> into a callback to create, like this:
>
> chrome.windows.create({url: ... }, function(win) {
>   chrome.windows.getLastFocused(...
>
> });
>
> Note that you shouldn't need the getLastFocused() call at all. You
> should be able to just do:
>
> chrome.windows.create({url:...}, function(win) {
>   chrome.tabs.create({url:..., windowId: win.id});
>   chrome.tabs.create({url:..., windowId: win.id});
>   chrome.tabs.create({url:..., windowId: win.id});
>
> });
>
> hth,
>
> - a
--~--~---------~--~----~------------~-------~--~----~
Chromium Discussion mailing list: [email protected] 
View archives, change email options, or unsubscribe: 
    http://groups.google.com/group/chromium-discuss
-~----------~----~----~----~------~----~------~--~---

Reply via email to