Think about it like this, every time you call one of the chrome.* API functions, it's like you're actually putting it in a setTimeout rather than calling it directly (although the delay is very short). For example, if you wrote this code:
var foo = 0; setTimeout(function() { foo = foo + 1; console.log("in setTimeout: " + foo)}, 0); foo = foo + 1; console.log("after setTimeout: " + foo); the output would be: after setTimeout: 1 in setTimeout: 2 So if you really wanted the "after" to happen second, you'd have to change your code a bit: var foo = 0; setTimeout(function() { foo = foo + 1; console.log("in setTimeout: " + foo); after();}, 0); function after() { foo = foo + 1; console.log("after setTimeout: " + foo); } Now you'd get: in setTimeout: 1 after setTimeout: 2 Erik On Wed, Nov 25, 2009 at 3:42 PM, Randall Crock <rlcr...@gmail.com> wrote: > I am using the debugger and adding a breakpoint on the line following > chrome.tabs.getSelected() where you have the second console.log(). How do I > wait until the asynchronous call has completed before moving on? > > > > Randy > > > > *From:* Erik Kay [mailto:erik...@google.com] > *Sent:* Wednesday, November 25, 2009 6:40 PM > *To:* Rlcrock > *Cc:* Chromium-extensions > *Subject:* Re: [chromium-extensions] Re: browser_action & tabs > > > > When are you checking t? If your code looks like this: > > > > var t = null; > > chrome.tabs.getSelected(null, function(tab){ > > t = tab; > console.log(tab);}); > > console.log("t == " + t); > > > > Then it will always print out "t == null". The reason is that the callback > function happens asynchronously. Even though it linearly looks like the > callback happens before the console.log, it actually happens some time > later. > > > > Erik > > > > > > On Wed, Nov 25, 2009 at 3:27 PM, Rlcrock <rlcr...@gmail.com> wrote: > > Ok, so using the debug tools, I can print out the tab using console.log > (), but when I try to assign to local variables, they do not get > changed at all. > > var t = null; > > chrome.tabs.getSelected(null, function(tab){ > > t = tab; > console.log(tab);}); > > When it runs, the console will have a correct trace, but t will still > be null. > > > On Nov 25, 4:02 pm, Rafael Weinstein <rafa...@chromium.org> wrote: > > Stepping won't work because the function you provide gets called later. > > > > Setting a break point inside that function should work. > > > > Does it not? > > > > > > > > > On Wed, Nov 25, 2009 at 12:15 PM, Rlcrock <rlcr...@gmail.com> wrote: > > > I am having trouble getting getSelected to read at all. > > > > > my code is: > > > > > var url, title; > > > chrome.tabs.getSelected(null, function(tab){ > > > url = tab.url; > > > title = tab.title; > > > console.log(tab);}); > > > > > but it never seems to execute the function as I step through in the > > > debugger, and url and title are always undefined. Is there something > > > I am doing wrong with my request? > > > > > On Nov 24, 1:49 pm, lorenx <lor...@gmail.com> wrote: > > >> hi, > > >> i found out that the extension behavior about the tab properties > > >> differ between the two modes, production and development: > > >> if i use the popup normally i don't need to wait for the onupdated > > >> listener (the page is already loaded) but if i'm in dev mode, the > > >> popup is called now from chrome-extension://<extension-id>/popup.html, > > >> i need the listener to ensure the title not to be undefined. > > > > >> i'm try to fix both the behavior in one solution but it is not so > > >> essential... > > >> thanks. > > > > >> On Nov 23, 11:34 pm, lorenx <lor...@gmail.com> wrote: > > > > >> > perfect! > > >> > and... sorry but... how to combine both? > > > > >> > something like: > > > > >> >chrome.tabs.getSelected(null, function(tab){ > > >> > chrome.tabs.onUpdated.addListener(function(tab.id, > changeInfo, tab2) > > >> > { > > >> > // code here... > > >> > }); > > > > >> > }); > > > > >> > or: > > > > >> > chrome.tabs.onUpdated.addListener(function(<what_here?>, changeInfo, > > >> > tab) { > > >> > chrome.tabs.getSelected(null, function(tab){ > > >> > // code here... > > >> > }); > > > > >> > }); > > > > >> > On Nov 23, 8:34 pm, Rafael Weinstein <rafa...@chromium.org> wrote: > > > > >> > > You need to listen to chrome.tabs.onUpdated event. > > > > >> > >http://code.google.com/chrome/extensions/tabs.html#event-onUpdated > > > > >> > > something like > > > > >> > > chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) > { > > >> > > if (changeInfo.status == 'complete') { > > >> > > tabUrl = tab.url; > > >> > > tabTitle = tab.title; > > >> > > createLink(); > > >> > > } > > > > >> > > On Sun, Nov 22, 2009 at 11:59 AM, lorenx <lor...@gmail.com> > wrote: > > >> > > > ok, i did it. thank you! > > > > >> > > > but the tab is loading and the title is still not available... > > >> > > > how to wait for the tab to be completly loaded? > > > > >> > > > On Nov 22, 5:27 am, donaddon <don...@verizon.net> wrote: > > >> > > >> I'm not sure I understand what you are expecting, but > getSelected is > > >> > > >> an asynchronous call. So you can't expect tabUrl and tabTitle > to be > > >> > > >> set at the time you are currently calling createLink. They > will only > > >> > > >> be set when the callback function is called. You may be > looking to do > > >> > > >> something like this: > > > > >> > > >>chrome.tabs.getSelected(null, function(tab) { > > >> > > >> tabUrl = tab.url; > > >> > > >> tabTitle = tab.title; > > >> > > >> createLink(); > > > > >> > > >> }); > > > > >> > > >> On Nov 21, 6:21 am, lorenx <lor...@gmail.com> wrote: > > > > >> > > >> > hi all, > > >> > > >> > i have a problem getting current tab info. > > > > >> > > >> > var tabUrl; > > >> > > >> > var tabTitle; > > >> > > >> > function getTabInfo() { > > >> > > >> > chrome.tabs.getSelected(null, function(tab){ > > >> > > >> > console.dir(tab); > > > > >> > > >> > tabUrl = tab.url; > > >> > > >> > tabTitle = tab.title; > > >> > > >> > });} > > > > >> > > >> > function createLink() { > > >> > > >> > console.log(tabUrl + '|' + tabTitle);} > > > > >> > > >> > getTabInfo(); > > >> > > >> > createLink(); > > > > >> > > >> > what's wrong with this code? > > >> > > >> > the tab object is output after createLink() is called... > > >> > > >> > should i have to set up some communication system between > components? > > >> > > >> > thanks. > > > > >> > > > -- > > > > >> > > > You received this message because you are subscribed to the > Google Groups "Chromium-extensions" group. > > >> > > > To post to this group, send email to > chromium-extensi...@googlegroups.com. > > >> > > > To unsubscribe from this group, send email to > chromium-extensions+unsubscr...@googlegroups.com<chromium-extensions%2bunsubscr...@googlegroups.com> > . > > >> > > > For more options, visit this group athttp:// > groups.google.com/group/chromium-extensions?hl=. > > > > > -- > > > > > You received this message because you are subscribed to the Google > Groups "Chromium-extensions" group. > > > To post to this group, send email to > chromium-extensi...@googlegroups.com. > > > To unsubscribe from this group, send email to > chromium-extensions+unsubscr...@googlegroups.com<chromium-extensions%2bunsubscr...@googlegroups.com> > . > > > > For more options, visit this group athttp:// > groups.google.com/group/chromium-extensions?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > "Chromium-extensions" group. > To post to this group, send email to chromium-extensi...@googlegroups.com. > To unsubscribe from this group, send email to > chromium-extensions+unsubscr...@googlegroups.com<chromium-extensions%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/chromium-extensions?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Chromium-extensions" group. To post to this group, send email to chromium-extensi...@googlegroups.com. To unsubscribe from this group, send email to chromium-extensions+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/chromium-extensions?hl=en.