简体   繁体   English

如何从greasemonkey脚本关闭firefox选项卡?

[英]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) 的配置设置以允许此操作。

Steps:脚步:

  1. Go to address bar and type about:config转到地址栏并输入about:config
  2. Go to parameter dom.allow_scripts_to_close_windows转到参数dom.allow_scripts_to_close_windows
  3. Set its value as true将其值设置为true

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.

相关问题 如何从Greasemonkey脚本创建Firefox插件? - How do I create a Firefox addon from a Greasemonkey script? 如何通过Greasemonkey脚本在Firefox中存储数组? - How do I store an array in Firefox from a Greasemonkey script? 如何将Greasemonkey脚本发布为Firefox附加组件? - How do I publish a Greasemonkey script as a Firefox add-on? 如何从Greasemonkey脚本“安静地”重新加载页面? - How do I “quietly” reload a page from a Greasemonkey script? 将Firefox 30之前的Greasemonkey脚本迁移到GM4 +时,如何替换unsafeWindow? - How do I replace unsafeWindow when migrating a pre-Firefox 30 Greasemonkey script to GM4+? 如何将Greasemonkey脚本转换为无重启的Firefox插件? - How can I convert a Greasemonkey script into a restartless Firefox addon? 如何使用Greasemonkey脚本防止页面在Firefox中缓存 - How to prevent page from caching in Firefox using Greasemonkey script 如何从Firefox中的greasemonkey脚本中允许跨源请求? - How do I allow Cross-Origin Requests from greasemonkey scripts in Firefox? 如何从 Greasemonkey 脚本中取消选中 HTML 页面上的所有“md-checkboxes”(不是真正的复选框)? - How do I uncheck all “md-checkboxes” (NOT real checkboxes) on a HTML page from a Greasemonkey script? 如何从链接中获取URL参数以在Greasemonkey脚本中形成新链接? - How do I get URL parameters from a link to form a new link in a Greasemonkey script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM