5

I am working on a Chrome extension, and at some point I need to exit the browser's process.

I tried closing all windows using this code:

chrome.windows.getCurrent({}, function(window) {
    chrome.windows.remove(window.id);
});

and it works on Windows and Linux but not on Mac (because on Mac, closing all windows doesn't mean closing the browser).

Is there a way to close the browser from the extension?

Thanks.

Lawand
  • 1,094
  • 2
  • 16
  • 40
  • Maybe, you should try not to depend on closing the browser. I would uninstall an extension, which closes my browser. – Christian Kuetbach Aug 16 '13 at 10:12
  • 1
    @ChristianKuetbach its fine if it's expected behaviour. a boss-key comes to mind. – monsto Aug 16 '13 at 15:17
  • One somewhat ugly way would be to use [NativeMessaging](http://developer.chrome.com/extensions/messaging.html#native-messaging) and have your native script kill the Chrome process… or cleaner by using AppleScript to tell the application to quit. (I haven't tried with AppleScript but I'm sure there are ways to make it work) – Timothée Boucher Aug 16 '13 at 16:35

3 Answers3

9

Install the bleeding-edge version of Chrome (get if from the dev channel or use Canary) and create an extension that uses the chrome.processes API.

It seems that the browser's process has ID 0. So, the following code will terminate Chrome:

chrome.processes.terminate(0);

However, since this is not documented, I suggest to get a list of processes, loop through the list and terminate the browser's process:

chrome.processes.getProcessInfo([], false, function(processes) {
    processes.forEach(function(process) {
        if (process.type === 'browser') {
            chrome.processes.terminate(process.id);
        }
    });
});

Alternative methods that work in all Chrome versions:

  • Create a NPAPI plugin and kill Chrome.
  • Host a local server of your choice that terminates your browser on (http) request.
  • Install a local application and use the native message API to request termination of Chrome.

These methods are not very convenient, and all either binary code and/or external applications to work. Therefore, I recommend to use the approach I've outlined in my answer.

user7607751
  • 465
  • 7
  • 27
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • fyi terminate(0) is not allowed (anymore) - there is explicit check in the C++ code that throws exception. NPAPI is deprecated. I assume your current solution terminates everything there is, how would you terminate only the current browser? – Erti-Chris Eelmaa Oct 12 '16 at 07:57
  • @Erti-ChrisEelmaa What do you mean by "current browser" if it is not "the main browser process and all of its child processes"? – Rob W Oct 12 '16 at 09:57
  • you can create for example, 2 chrome "main browser processes", every main chrome process has 10 child processes, and I only want to kill one main browser process(which kills its 10 child processes as well). – Erti-Chris Eelmaa Oct 12 '16 at 11:35
  • @Erti-ChrisEelmaa The native messaging host can look up the parent process ID. E.g. the following Python script as a native messaging host kills whatever calls it (tested on Linux): `import os; import signal; os.kill(os.getppid(), signal.SIGTERM)` – Rob W Oct 12 '16 at 11:55
  • This answer is posted in 2013 and no major changes have been made to date. It is now 2023 and `chrome.processes` is still in Dev channel. Is there any way to know when it will transition from Dev channel to Stable channel? Or is there a way to approach Google? – Loran Jun 06 '23 at 02:54
3

As of 2018:

Modern answer without anything fancy. Just a chrome extension.
Add this bad boy to your popup.js or browseraction.js (call it what you want)

chrome.tabs.query({}, function (tabs) {
  for (var i = 0; i < tabs.length; i++) {
      chrome.tabs.remove(tabs[i].id);
  }
});
window.close();

And ensure that you have it listed inside both your manifest (JSON) and your popup (HTML) file:

HTML

<html>
  <head>
    <script src="js/popup.js"></script>
  </head>
</html>

 

JSON

 ...
"browser_action": {
    "default_icon": "this part is optional.png",
    "default_title": "close chrome",    
    "default_popup": "popup.html",
    "matches": ["*://*", "*:*"]
},
 ...

Here's where I got it from: link & here are the chrome-extension-manifest requirements / correct format: link

255.tar.xz
  • 700
  • 7
  • 23
1

If there is no other tabs opened you could close the current tab by fireing:

chrome.tabs.getCurrent(function(tab) {
    chrome.tabs.remove(tab.id)
})

That'il close the Browser.

Microman
  • 11
  • 1