Skip to content

Instantly share code, notes, and snippets.

@eevee
Last active August 29, 2015 14:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eevee/1a2107bf31c6ab5872fd to your computer and use it in GitHub Desktop.
Save eevee/1a2107bf31c6ab5872fd to your computer and use it in GitHub Desktop.
userscript that blocks really really annoying js quirks. web devs should really do this crap themselves...
// ==UserScript==
// @name disable key events
// @namespace eev.ee
// @description Prevents JS on the page from receiving keyup, keydown, or keypress. Fixes, e.g., Twitter's disabling of shortcuts until the page has loaded.
// @include https://twitter.com/*
// @version 1
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
"use strict";
var blocker = function(event) {
event.stopImmediatePropagation();
};
['keyup', 'keydown', 'keypress'].map(function(event_type) {
// Setting useCapture to true ensures the document receives the event
// first (during the capture phase, when handlers are run from the
// outside in). Most event handlers use bubbling, which runs
// inside-out. Even if the page did this same thing, this script runs
// before there's any DOM at all, so we're guaranteed (I hope) to be
// the first listener, and stopImmediatePropagation will block any
// other listeners even for the same event on the same element.
document.addEventListener(event_type, blocker, true);
});
})();
// ==UserScript==
// @name never navigate to pound nowhere
// @namespace veekun.com
// @description Prevent jumping to the top of the page when clicking a poorly-made JS link before the JS has loaded.
// @include *
// @version 1
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
"use strict";
// Capture (third argument true) means this will always run first before
// any other event handler gets a chance to run (and possibly
// stopPropagation() without preventDefault()). So links to # will never
// ever work. I think I can live with that.
document.documentElement.addEventListener('click', function(event) {
var el = event.target;
// Gotta run up the tree to look for a link, since we might've clicked
// a child of one
while (el) {
if (el.tagName.toLowerCase() === 'a') {
break;
}
el = el.parentNode;
}
// If it's a link to #, don't follow it.
if (el && el.getAttribute('href') === '#') {
event.preventDefault();
}
}, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment