How to check if a specific page is already open in Google Chrome?
Look inside the Google Mail Checker extension, which has this functionality:
function goToInbox() { chrome.tabs.getAllInWindow(undefined, function(tabs) { for (var i = 0, tab; tab = tabs[i]; i++) { if (tab.url && isGmailUrl(tab.url)) { chrome.tabs.update(tab.id, {selected: true}); return; } } chrome.tabs.create({url: getGmailUrl()}); });}
In particular, you pass getAllInWindow the windowId (or undefined for the current window) and a function, which receives the array of Tab objects. You don't modify the properties of the tab directly; rather you pass its id to the update function in order to manipulate it.
To make Josh Lee's answer work using version 2 manifest, you have to add permission to the tabs in the manifest.json file:
..."permissions": [ "tabs"]...
I have no clue how this kind of construction adds 'security' to the web ...
Hope it helps the beginners!
With additional to #Josh Lee 's answer.
function openMyTab(mURL) { if(!mURL){ console.log("No url passed"); return; } chrome.tabs.getAllInWindow(undefined, function(tabs) { for (var i = 0;i<tabs.length; i++) {// remove (tabs[i].url.indexOf(mURL)!=-1) and // use tabs[i].url===url if you want exact match below if (tabs[i].url && (tabs[i].url.indexOf(mURL)!=-1)) { console.log("URL Match found",tabs[i].url); chrome.tabs.update(tabs[i].id, {url:url,selected: true}); return; } } console.log("URL not found. Creating new tab"); chrome.tabs.create({url: url}); });