Reputation: 55
I'm attempting to see if the tab 'http://google.com' exists. If it does then I want to make it the active page. Otherwise, the tab 'http://google.com' doesn't exist, and I want to create it.
backround.js
chrome.runtime.onMessage.addListener(function(response, sender, sendResponse){
chrome.tabs.create({'url': 'http://google.com'}, function(tab) {
alert('Tab Created ' + tab.id);
var oms = tab.id;
chrome.tabs.update(oms, {url:"http://en.wikipedia.org"});
});
});
This creates the webpage, gets the tabid
and sets the tabid
as a variable.
Upvotes: 2
Views: 2486
Reputation: 4508
Your current attempt seems to be trying to create a tab with Google, and then navigate it to Wikipedia. This doesn’t agree with your initial paragraph. Instead, I think you want something similar to the following:
chrome.runtime.onMessage.addListener(function(response, sender, sendResponse){
chrome.tabs.query({'url': 'http://google.com'}, function(tabs) {
if ( tabs.length > 0 ) {
chrome.tabs.update(tabs[0].id,{'active':true});
} else {
chrome.tabs.create({'url':'http://google.com'});
}
});
});
Note that this won’t find http://www.google.com, or http://google.com/otherstuff. You’ll probably want to use a match pattern.
You commented having trouble with updating the tab to be selected
. The documentation says that selected
is deprecated in favor of highlighted
. This also hints what else may have happened: a highlighted
tab is not necessarily active (specifically, you can have several tabs highlighted
).
Upvotes: 3