英文:
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"。
我希望禁止显示此消息并简单关闭页面。
我尝试添加 beforeunload
和 unload
事件处理程序,如下所示:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论