[英]How do I close a firefox tab from a greasemonkey script?
I have a greasemonkey user script with this single line of code...我有一个带有这行代码的greasemonkey用户脚本......
window.close();
but firefox does not allow a user script to close a window (as reported by an error message in the error console)但是 firefox 不允许用户脚本关闭窗口(如错误控制台中的错误消息所报告的那样)
Is there a work around to this problem?这个问题有解决方法吗?
You need to change configuration settings of Firefox (about:config) to allow this.您需要更改 Firefox (about:config) 的配置设置以允许此操作。
Now your script can close the TAB with 'window.close()'现在您的脚本可以使用“window.close()”关闭选项卡
eg.例如。
function closeTab(){
window.open('', '_self', '');
window.close();
}
由于 Firefox 以与外部网站上的脚本代码相同的权限对待 Greasemonkey 代码,因此不可能只允许 Greasemonkey 代码能够关闭窗口,而不允许常规脚本。
By now some of the -monkies allow the use of @grant option to officially unlock commands like window.close() without going to about:config .到目前为止,一些-monkies允许使用@grant选项来正式解锁像window.close()这样的命令,而无需转到about:config 。 For example, in Tampermonkey :
例如,在Tampermonkey 中:
// @grant window.close
// @grant window.focus
(The latter grant allows you to re-focus the browser on your window.) This would remove the error. (后者授予允许你重新聚焦你的窗口浏览器)。这将消除错误。
EDIT: As @baptx correctly mentions in the comments, the browser's security options should be set to allow scripts to close windows, too.编辑:正如@baptx在评论中正确提到的那样,浏览器的安全选项也应该设置为允许脚本关闭窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.