Stop Stealing My Shortcuts

Prevent websites from hijacking specific keyboard shortcuts, like Chrome Tab Switcher

  1. // ==UserScript==
  2. // @name Stop Stealing My Shortcuts
  3. // @namespace https://github.com/mariczne/stop-stealing-my-shortcuts
  4. // @version 0.1.2
  5. // @description Prevent websites from hijacking specific keyboard shortcuts, like Chrome Tab Switcher
  6. // @license MIT
  7. // @author mariczne
  8. // @match https://discord.com/*
  9. // @match https://www.facebook.com
  10. // @match https://app.slack.com/*
  11. // @icon https://github.githubassets.com/pinned-octocat.svg
  12. // @supportURL https://github.com/mariczne/stop-stealing-my-shortcuts/issues
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16. (function () {
  17. "use strict";
  18.  
  19. // List of shortcuts to prevent from hijacking
  20. const blockedShortcuts = [
  21. { key: "a", ctrl: false, shift: true, alt: false, meta: true }, // Cmd+Shift+A
  22. { key: "a", ctrl: true, shift: true, alt: false, meta: false }, // Ctrl+Shift+A
  23. // Add other shortcuts as needed
  24. ];
  25.  
  26. window.addEventListener(
  27. "keydown",
  28. function (event) {
  29. if (!event.isTrusted) return; // Ignore events dispatched by this script
  30.  
  31. for (const shortcut of blockedShortcuts) {
  32. if (
  33. event.key.toLowerCase() === shortcut.key.toLowerCase() &&
  34. event.ctrlKey === shortcut.ctrl &&
  35. event.shiftKey === shortcut.shift &&
  36. event.altKey === shortcut.alt &&
  37. event.metaKey === shortcut.meta
  38. ) {
  39. event.stopImmediatePropagation();
  40.  
  41. if (document.activeElement) {
  42. document.activeElement.blur(); // Remove focus from any element
  43. }
  44.  
  45. const newEvent = new KeyboardEvent("keydown", {
  46. key: event.key,
  47. code: event.code,
  48. ctrlKey: event.ctrlKey,
  49. shiftKey: event.shiftKey,
  50. altKey: event.altKey,
  51. metaKey: event.metaKey,
  52. bubbles: true,
  53. cancelable: true,
  54. });
  55.  
  56. window.setTimeout(() => {
  57. document.dispatchEvent(newEvent);
  58. }, 0);
  59.  
  60. return;
  61. }
  62. }
  63. },
  64. true
  65. );
  66. })();