Skip to content

Instantly share code, notes, and snippets.

@varenc
Last active March 11, 2023 01:37
Show Gist options
  • Save varenc/a40b9109793eab7040568022fd2f079f to your computer and use it in GitHub Desktop.
Save varenc/a40b9109793eab7040568022fd2f079f to your computer and use it in GitHub Desktop.
Unblock browser keyboard shortcuts (in Chrome) from bad website javascript (like discussions.apple.com forums)
// Some bad sites, like the discussions.apple.com Apple Forums, block various browser keyboard shortcuts. Very rarely do you want this.
// They even block basic things like refresh, Cmd+R, or go back, Cmd+[
// This snippet just insert a new event handler that stops propagration but doesn't prevent the default.
// This prevents a site/Apple's event handlers from blocking your shortcuts. It also disables all other Keyboard shortcuts.
// ublock origin instructions:
// You can use ublock origin to automatically run this snippet on certain sites using a custom resource: https://github.com/gorhill/uBlock/wiki/Advanced-settings#userresourceslocation
// Steps
// - Create a custom JS resource file (see linked docs above) that includes this snippet. Here is mine for example: https://gist.github.com/varenc/7c0c17eea480a03d7c8844d81e608e1e#file-ublock_custom_scripts-js-L49
// - Once you have this JS available to use as a resource, just create a ublock filter rule to run it on certain sites.
// - For example, I have this automatically run on discussions.apple.com using this rule: `discussions.apple.com##+js(disable-keyboard.js)`
function __unblockEvents() {
// for each argument passed in, create a listener that does nothing
for (var i = 0; i < arguments.length; i++) {
document.addEventListener(arguments[i], function (e) {
e.stopPropagation();
console.log("event recieved and unblocked:", e);
}, true);
}
}; __unblockEvents('keydown', 'keyup', 'keypress');
// you can also turn this into a bookmarklet by using this:
// javascript:function __unblockEvents(){for(var i=0;i<arguments.length;i++){document.addEventListener(arguments[i],function(e){e.stopPropagation();console.log("event received and unblocked:",e);},true);}};__unblockEvents('keydown','keyup','keypress');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment