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 white list of websites (on which this script will be "enabled").

目前为 2024-03-16 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Ctrl+Won't
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-03-16
  5. // @description Prevents accidental Ctrl+W from closing current tab while you are typing something (input/textarea tag is active). You can change the white 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 white_list = [
  16. 'duckduckgo.com',
  17. 'github.com',
  18. 'discord.com',
  19. 'stackoverflow.com',
  20. 'greasyfork.org',
  21. ];
  22. if (!white_list.includes(location.hostname)) {
  23. return;
  24. }
  25. addEventListener(
  26. 'beforeunload',
  27. function (e) {
  28. if (location.hostname === 'discord.com') {
  29. if (
  30. document.activeElement.localName !== 'div' ||
  31. document.activeElement.getAttribute('role') !== 'textbox'
  32. ) {
  33. return;
  34. }
  35. } else if (
  36. !['input', 'textarea'].includes(document.activeElement.localName)
  37. ) {
  38. return;
  39. }
  40. e.stopPropagation();
  41. e.preventDefault();
  42. return false;
  43. },
  44. true,
  45. );
  46. })();