Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can this extension be used to prevent websites from hijacking keys? #625

Open
realestninja opened this issue May 29, 2024 · 1 comment
Open

Comments

@realestninja
Copy link

Hi,

I'm actually looking for a way to outsmart websites from hijacking keyboard shortcuts.

Known offenders:

Both of them override the [alt + numerical key] which is a big pain in the ass.

My hope was that by using this browser extension, I could override what they have done but that doesn't work.

Would this be possible in theory by making changes to this extension?

@nalply
Copy link

nalply commented Jun 1, 2024

I have some websites that bind ArrowRight and ArrowLeft for video time line jumping and their programmers were so incompetent to accidentally also bind the Firefox shortcuts for history navigation Alt-Left and Alt-Right.

Please have a look at my TamperMonkey user script to keep the shortcuts. I dedicate this source code to the public domain or put it to Creative Commons CC0 "No Rights Reserved" whatever you prefer.

// ==UserScript==
// @name         KeepHistoryShortcuts
// @namespace    https://github.com/nalply
// @version      2024-06-01
// @description  Alt-Left and Alt-Right are shortcuts to navigate history. Keep them!
// @author       nalply
// @match        https://*/*
// @run-at       dpcument-start
// @grant        none
// ==/UserScript==

// Capture keydown events before anything else and stops propagation for Alt-ArrowLeft and Alt-ArrowRight.
// This needs run-at: document-start and { capture: true } as third parameter to addEventListener().

'use strict'

console.log("KHS: KeepHistoryShortcuts user script started")

document.addEventListener('keydown', forceDefault, { capture: true })

function forceDefault(ev) {
  if (ev.altKey && (ev.key == "ArrowRight" || ev.key == "ArrowLeft")) {
    console.log("KHS: keypress kept for browser", ev.key)
    ev.stopImmediatePropagation()
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants