修复:Chrome 点击离开时出现原生"离开网站"弹窗闪烁

主动调用 remove 移除 beforeunload 监听后再导航,
Chrome 不再触发原生确认框,Edge 行为不变
This commit is contained in:
2026-03-01 00:16:34 +08:00
parent 1caaec5601
commit 0f5b8a4f52

View File

@@ -951,6 +951,10 @@
// ── 退出房间 ─────────────────────────────────────
async function leaveRoom() {
// 主动移除 beforeunload 监听,防止 Chrome 触发"离开网站?"原生弹窗
window.removeEventListener('beforeunload', sendLeaveBeacon);
clearTimeout(visibilityTimer);
try {
await fetch(window.chatContext.leaveUrl, {
method: 'POST',
@@ -970,6 +974,7 @@
}, 500);
}
// ── 关闭/离开页面时自动调用 leave结算勤务时长 ──────────────────────
// 使用 sendBeacon 确保浏览器关闭时请求也能发出(比 fetch 更可靠)
function sendLeaveBeacon() {