How do I close a firefox tab from a greasemonkey script?

萝らか妹 提交于 2019-11-29 17:21:47

问题


I have a greasemonkey user script with this single line of code...

window.close();

but firefox does not allow a user script to close a window (as reported by an error message in the error console)

Is there a work around to this problem?


回答1:


You need to change configuration settings of Firefox (about:config) to allow this.

Steps:

  1. Go to address bar and type about:config
  2. Go to parameter dom.allow_scripts_to_close_windows
  3. Set its value as true

Now your script can close the TAB with 'window.close()'

eg.

function closeTab(){
    window.open('', '_self', '');
    window.close();
} 



回答2:


Since Firefox treats Greasemonkey code with the same privilages as the script code on external websites, it is not possible to only allow Greasemonkey code to be able to close the windows, but not regular scripts.




回答3:


By now some of the -monkies allow the use of @grant option to officially unlock commands like window.close() without going to about:config. For example, in 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.



来源:https://stackoverflow.com/questions/330337/how-do-i-close-a-firefox-tab-from-a-greasemonkey-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!