Prevent Accidental Tab Closure with "ctrl + w" [recommended for sploop.io and moomoo.io]

Prevents accidental tab closure when you press ctrl+w

  1. // ==UserScript==
  2. // @name Prevent Accidental Tab Closure with "ctrl + w" [recommended for sploop.io and moomoo.io]
  3. // @namespace http://tampermonkey.net/
  4. // @version 2
  5. // @description Prevents accidental tab closure when you press ctrl+w
  6. // @author Lore
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. window.addEventListener('beforeunload', function(e) {
  16. e.preventDefault();
  17. e.returnValue = '';
  18. });
  19.  
  20. window.addEventListener('keydown', function(e) {
  21. if (e.ctrlKey && e.key === 'w') {
  22. e.preventDefault();
  23. var confirmed = confirm('Are you sure you would like to close this tab or was this a accident?');
  24. if (confirmed) {
  25. window.removeEventListener('beforeunload');
  26. window.close();
  27. }
  28. }
  29. });
  30. })();