Used
Chrome 37.0.2062.120, Chrome Extension. MacBook Pro, MacOS 10.9.3.
Permissions granted, new tabs are being opened normally.
I use Chrome Notifications and if user clicks the specific button in the notification, new tab is opened.
Problem
When Chrome is "hidden" (not closed completely, but kinda works in background, when you click red bubble with a cross in the left top corner of Mac Chrome), opening new tab is not triggering browser to "restore" (open).
Thus, browser remains hidden and no tab is opened (visibly not opened, technically the callback is invoked).
Code
Opening from background.js
// if user triggers new tab open from Chrome Notification
chrome.tabs.create({url:someUrl})
Editions
Edition 1. Tabs are just visibly not opened, technically the callback is invoked
Edition 2. The code below is better as it focuses the opened window if it is not focused at the moment of tab creation. However, does not solve the original problem.
chrome.tabs.create({url:someUrl}, function(tab){
chrome.windows.update(tab.windowId, {focused: true})
})
Edition 3. Here we go, it throws an error in background:
Unchecked runtime.lastError while running tabs.create: No current window
Solution
Many thanks to Xan.
function new_tab(url){
// trying to create a tab
chrome.tabs.create({url:url}, function(tab){
if(!tab) {
// probably no window available
chrome.windows.create({url:url},function(win){
// better to focus after window creation callback
chrome.windows.update(win.id, {focused: true})
})
} else {
// better to focus after tab creation callback
chrome.windows.update(tab.windowId, {focused: true})
}
})
}
Of course this function needs to be modified if you need to pass extra options or callbacks.