0

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.

igorpavlov
  • 3,576
  • 6
  • 29
  • 56

2 Answers2

3

You need to make the window focused:

chrome.tabs.create({url:someUrl}, function(tab){
  chrome.windows.update(tab.windowId, {focused: true});
});

In a case that there is no active window, you need to account for that and create one if required:

chrome.tabs.create({url:someUrl}, function(tab){
  if(!tab) {
    // Probably, there was no active window
    chrome.windows.create({url:someUrl, focused:true});
  } else {
    chrome.windows.update(tab.windowId, {focused: true});
  }
});

Note that this will probably not respect the default window state (maximized/normal), but better than nothing. I tested it on an extension with "background" permission.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • Me neither, but I know that it's not going to work for I have already experienced the issue that the OP reported ;) – Rob W Sep 19 '14 at 14:11
  • So, is this state of Chrome the same as all visible windows closed, like a `background`-enabled extension? – Xan Sep 19 '14 at 14:12
  • No, the issue is different. The question of the OP is slightly inaccurate; the tabs *are* opened, but they are not visible. – Rob W Sep 19 '14 at 14:13
  • No. I'll elaborate in an answer, then you'll see why. – Rob W Sep 19 '14 at 14:15
  • See my answer. If you're currently running a Windows VM, could you follow the steps from the bug report and see if it also occurs on Windows? If so, then I will add the Os-Windows label to the bug report. – Rob W Sep 19 '14 at 14:20
  • @RobW Cannot reproduce on Win7 with 37.0.2062.120 m (64-bit) – Xan Sep 19 '14 at 14:24
  • Seems specific to Linux and Mac then. – Rob W Sep 19 '14 at 14:26
  • I added edition #1 regarding to "visibly" and "technically" opened tab. – igorpavlov Sep 19 '14 at 14:31
  • @igorpavlov Have you already tried Xan's answer? I *assumed* that his instructions does not work because it doesn't work on Linux, but I have to admit that I haven't tried it on a Mac yet. – Rob W Sep 19 '14 at 14:41
  • This code actually is useful for me, solves another problem: when window is opened, but not focused and I trigger the tab from notification. However, if Chrome is working in background, neither Chrome window, nor the tab is opened. – igorpavlov Sep 22 '14 at 12:35
  • I assume then, again, that the condition is "no windows open, extension running in `background` mode". Please try to run `chrome.windows.getCurrent(function(win){console.log(chrome.runtime.lastError); console.log(win);})` while this happens. It's possible that you have to open a window first. – Xan Sep 22 '14 at 12:39
  • Updated the post, please see Edition 3. – igorpavlov Sep 22 '14 at 12:45
  • Fixed my answer. Caveats apply, but it works. You need such special measures if you expect to keep running in background. – Xan Sep 22 '14 at 12:55
0

This answer only applies to Linux and Mac (not to Windows where the bug in question does not exist).

A Chrome extension cannot focus a window unless at least a part of Chrome is the currently active window. I reported this bug a month ago with a detailed steps-to-reproduce as Issue 407817: chrome.windows.create({ focused:true }) within a desktop notification does not create a focused window.

Unfortunately, there is nothing that you can do in an extension. If it's a personal extension, you could create a native messaging host and write a native application that puts the focus on the new window (if it is not a personal extension, then you have to ask your users to also install that native messaging host).

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • It is my personal extension. Could you explain how can I use "native messaging host" to solve my issue? – igorpavlov Sep 19 '14 at 14:29
  • @igorpavlov Read the linked documentation to learn more about the native messaging protocol, then write a program (C, Python, Bash, AppleScript (?), whatever you want) that puts focus on the window. [Here is an example of a bash script](https://stackoverflow.com/a/24777120/938089) that uses the native message protocol. On Linux, I could use `xdotool` or `wmctrl` to change the window focus. Use your Google skills to find something similar for Mac. [Here are step-by-step instructions](http://stackoverflow.com/a/25193936) to install a native messaging host on a Mac. – Rob W Sep 19 '14 at 14:39