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

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.

EDIT: As @baptx correctly mentions in the comments, the browser's security options should be set to allow scripts to close windows, too.


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();
} 

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.