如何从扩展程序中退出Chrome? [英] How to exit Chrome from an extension?

查看:120
本文介绍了如何从扩展程序中退出Chrome?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试使用这段代码关闭所有窗口:

我正在使用Chrome扩展,在某些时候我需要退出浏览器的进程。 >

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

它适用于Windows和Linux,但不适用于Mac(因为在Mac上,关闭所有窗口并不会) t意味着关闭浏览器)。



有没有办法从扩展中关闭浏览器?



谢谢。

解决方案

安装Chrome的最新版本(如果来自 dev channel 或使用Canary)并创建一个使用 chrome.processes API。



它似乎浏览器的进程ID为0.因此,以下代码将终止Chrome:

  chrome.processes.terminate(0) ; 

但是,由于没有记录,我建议获取进程列表,循环遍历列表并终止浏览器的进程:

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

可在所有Chrome版本中使用的替代方法:



这些方法不是很方便,所有的二进制代码和/或外部应用程序都可以工作。因此,我建议使用我在答案中列出的方法。


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.

解决方案

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.

这篇关于如何从扩展程序中退出Chrome?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆