"chrome.tabs.create" works, but "chrome.tabs.update" does not

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 works as is, I get JS 3 and 4 warnings. OK.

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

When I comment on expression 1 and comment in statement 2, I get a JS warning of 3, and that is.

According to http://code.google.com/chrome/extensions/tabs.html#method-update , I don’t need to pass the tabId object because it "defaults to the selected tab of the current window". The url object is defined as the β€œURL to go through the tab,” as I noticed when I ran chrome.tabs.create in instruction 1.

Why is my chrome.tabs.update instruction not working?

+6
source share
2 answers

You must specify the tabId parameter. Set to 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 received an "object".

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

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

+5
source

You must specify which tab (tab.id) you want to update.

 chrome.tabs.update(id,{url:"http://en.wikipedia.org"}); 
0
source

Source: https://habr.com/ru/post/914330/


All Articles