Ctrl+Won't

Prevents accidental Ctrl+W from closing current tab while you are typing something (input/textarea tag is active). You can change the whit list of websites (on which this script will be "enabled").

  1. // ==UserScript==
  2. // @name Ctrl+Won't
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-03-17
  5. // @description Prevents accidental Ctrl+W from closing current tab while you are typing something (input/textarea tag is active). You can change the whit list of websites (on which this script will be "enabled").
  6. // @author Andrew15-5
  7. // @match *://*/*
  8. // @icon https://i.ytimg.com/vi/Qa5xfIbMaqw/maxresdefault.jpg
  9. // @grant none
  10. // @license AGPL-3.0
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15. const whitelist = [
  16. 'github.com',
  17. 'discord.com',
  18. 'stackoverflow.com',
  19. 'greasyfork.org',
  20. ];
  21. if (!whitelist.includes(location.hostname)) {
  22. return;
  23. }
  24. addEventListener(
  25. 'beforeunload',
  26. function (e) {
  27. if (location.hostname === 'discord.com') {
  28. if (
  29. document.activeElement.localName !== 'div' ||
  30. document.activeElement.getAttribute('role') !== 'textbox'
  31. ) {
  32. return;
  33. }
  34. } else if (
  35. !['input', 'textarea'].includes(document.activeElement.localName)
  36. ) {
  37. return;
  38. }
  39. e.stopPropagation();
  40. e.preventDefault();
  41. return false;
  42. },
  43. true,
  44. );
  45. })();