Search code examples
javascriptgoogle-chrome-extension

How to exit Chrome from an extension?


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.


Solution

  • 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.