Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"chrome.tabs.create" works, but "chrome.tabs.update" doesn't

function doit() {
 alert(3);
 // Statement 1
 // chrome.tabs.create({url:"http://www.google.com"});
 // Statement 2
 // chrome.tabs.update({url:"http://en.wikipedia.org"});
 alert(4);
}


chrome.browserAction.onClicked.addListener(doit);

When the script runs as is, I get JS alerts of 3 and 4. OK.

When I comment in statement 1 and run the script, I get a JS alert of 3, then Google opens in a new, active tab, then I get a JS alert of 4. As expected.

When I comment out statement 1 and comment in statement 2, I get a JS alert of 3, and that's it.

According to http://code.google.com/chrome/extensions/tabs.html#method-update, I do not need to pass a tabId object, because it "defaults to the selected tab of the current window." The url object is defined as "A URL to navigate the tab to", as I noticed when I ran the chrome.tabs.create in statement 1.

Why does my chrome.tabs.update statement not work?

like image 399
mdslup Avatar asked Apr 27 '12 12:04

mdslup


People also ask

How do I refresh all tabs in Chrome 2021?

Keyboard shortcut to reload tabs is bound to Alt - Shift - R. Reload all tabs in all windows (enabled via options)

How do I restore Chrome tabs?

Chrome keeps the most recently closed tab just one click away. Right-click a blank space on the tab bar at the top of the window and choose Reopen closed tab. You can also use a keyboard shortcut to accomplish this: CTRL + Shift + T on a PC or Command + Shift + T on a Mac.


1 Answers

The tabId parameter has to be specified. Supply null if you want to use the default settings. Otherwise, the following error occurs:

Error in event handler for 'browserAction.onClicked': Error: Invalid value for argument 1. Expected 'integer' but got 'object'.

This message is logged at the background page. To access this console, follow the four steps in this answer.

Corrected code: chrome.tabs.update(null, {url:"http://en.wikipedia.org"});

like image 92
Rob W Avatar answered Sep 24 '22 02:09

Rob W