Can Tampermonkey force tab to close despite "Changes that you made may not be saved"

huangapple go评论82阅读模式
英文:

Can Tampermonkey force tab to close despite "Changes that you made may not be saved"

问题

在某些条件下,我的用户脚本允许用户通过按钮点击关闭页面,但当我使用 window.close() 时,会出现消息:"Leave Site? Changes that you made might not be saved"。

我希望禁止显示此消息并简单关闭页面。

我尝试添加 beforeunloadunload 事件处理程序,如下所示:

document.querySelector('#mybutt').addEventListener('click', () => {
    window.close();
});
window.addEventListener('beforeunload', (event) => {
    console.log('This will suppress the Leave/Cancel prompt');
});
window.addEventListener('unload', (event) => {
    console.log('This will suppress the Leave/Cancel prompt');
});

确实,Leave/Cancel提示不再出现,但在控制台中我现在有错误消息 Scripts may close only the windows that were opened by them.

是否有解决方法?

英文:

Under certain conditions my userscript allows users to close the page via button press, but when I issue window.close() I get the message: "Leave Site? Changes that you made might not be saved".

I wish to suppress this message and simply close the page.

I tried adding beforeunload and unload event handlers, as follows:

document.querySelector('#mybutt').addEventListener('click', () => {
    window.close();
});
window.addEventListener('beforeunload', (event) => {
    console.log('This will suppress the Leave/Cancel prompt');
});
window.addEventListener('unload', (event) => {
    console.log('This will suppress the Leave/Cancel prompt');
});

and, sure enough, the Leave/Cancel prompt did not appear - but in the console I now have the error Scripts may close only the windows that were opened by them.

Is there a work-around?

答案1

得分: 1

是的,有的。看起来有点奇怪,但它有效。

假设你有一个触发显示消息并带有关闭页面按钮的代码。在代码的那一部分,你会显示消息,添加:

document.querySelector('#myMsgDiv').style.display = 'block';
location.href = "javascript:(" + function() {
    window.onbeforeunload = null;
    window.onunload = null;
    console.log('load/unload hit');
} + ")()";

出于某种原因,我还必须将相同的代码添加到按钮点击处理程序中:

document.querySelector('#btnCloseWindow').addEventListener('click', () => {
    location.href = "javascript:(" + function() {
        window.onbeforeunload = null;
        window.onunload = null;
        console.log('load/unload hit');
    } + ")()";
    window.close();
});

页面将自动关闭。

参考链接:

https://forums.mozillazine.org/viewtopic.php?f=38&t=2083577

英文:

Yes there is. Looks odd, but it works.

Suppose you have code that triggers a message to displayed with a button to close the page. In that code, at the point where you would display the message, add:

document.querySelector('#myMsgDiv').style.display = 'block';
location.href = "javascript:(" + function() {
	window.onbeforeunload = null;
	window.onunload = null;
	console.log('load/unload hit');
} + ")()";

For whatever reason, I had to also add the same code into the button-click handler:

document.querySelector('#btnCloseWindow').addEventListener('click', () => {
	location.href = "javascript:(" + function() {
		window.onbeforeunload = null;
		window.onunload = null;
		console.log('load/unload hit');
	} + ")()";
	window.close();
});

The page will automatically close.

References:

https://forums.mozillazine.org/viewtopic.php?f=38&t=2083577

huangapple
  • 本文由 发表于 2023年7月14日 01:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定