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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Chromium-extensions" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/chromium-extensions?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to