简体   繁体   English

Chrome扩展程序在浏览器关闭之前执行某些操作

[英]Chrome Extension to do something before browser close

Is there any way I can detect the whole browser closing and then calling a function just before it closes? 有什么办法可以检测到整个浏览器是否关闭,然后在关闭之前调用一个函数? This is for Google Chrome specifically. 这是专门针对Google Chrome浏览器的。 Essentially I need an alert for the user before it closes. 本质上,我需要在用户关闭之前为用户提供警报。 This is going to be for a chrome extension. 这将用于chrome扩展程序。

I have tried 我努力了

window.onbeforeunload = function() {
     alert("can't exit yet");
     return null;
}

and

window.addEventListener("beforeunload", function (e) {
  alert("can't exit yet");
  return null;
});

They both do nothing. 他们俩什么都不做。 Chrome still exits and doesn't alert. Chrome仍会退出,并且不会发出警报。

Thanks so much for the help! 非常感谢你的帮助!

Based on the documentation of on the onbeforeunload event handler : 基于onbeforeunload事件处理程序文档

When this event returns a non-void value, the user is prompted to confirm the page unload. 当此事件返回非空值时,将提示用户确认页面卸载。

and

Since 25 May 2011, the HTML5 specification states that calls to window.alert() , window.confirm() , and window.prompt() methods may be ignored during this event. 从2011年5月25日开始,HTML5规范规定在此事件期间可以忽略对window.alert()window.confirm()window.prompt()方法的调用。

In the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event. 在HTML5规范中,声明在此事件期间可以忽略对window.alert(),window.confirm()和window.prompt()方法的调用。 However, some browsers may display your message in alert box if you return the string on callback: 但是,如果您在回调中返回字符串,则某些浏览器可能会在警报框中显示您的消息:

window.onbeforeunload=function(){
    return "This is message before unload!";
}

while other display generic browser message; 其他则显示通用浏览器消息;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM