Possible to disable custom Javascript keybindings in Safari

javascriptkeyboardsafari

I have a couple of decades experience using Emacs, so the Emacs-like keybindings for text editing on Mac OS X are very nice. Unfortunately, some websites (ahem) choose to override these with Javascript. For example, one of them makes controlB insert the text **strong text**, rather than going back one character.

Is there any way to make Safari ignore Javascript keybindings that try to remap the standard OS X keybindings?

Best Answer

Here is Rob W's script from meta, updated to only block cntrl-* and fixed so that it works with NinjaKit which is a safari extension for running user scripts.

NinjaKit: https://github.com/os0x/NinjaKit

Script:

// ==UserScript==
// @name           Cya WMD shortcuts
// @namespace      Rob W
// @version        1.0
// @include          http://apple.stackexchange.com/*
// @include          http://stackoverflow.com/*
// @include          http://superuser.com/*
// @include          http://meta.superuser.com/*
// @include          http://serverfault.com/*
// @include          http://meta.serverfault.com/*
// @include          http://askubuntu.com/*
// @include          http://meta.askubuntu.com/*
// @include          http://*.stackexchange.com/*
// @include          http://answers.onstartups.com/*
// @include          http://meta.answers.onstartups.com/*
// @include          http://stackapps.com/*
// @run-at         document-end
// @grant          none
// ==/UserScript==
(function () {
    var p = document.getElementById('wmd-input');
    console.log("wmd-input:" + p);
    if (p) {
        p = p.parentNode;

        function ignore(e) {
            if (e.ctrlKey) {
                e.stopPropagation();
            }
        }
        p.addEventListener('keydown', ignore, true);
        p.addEventListener('keypress', ignore, true);
        p.addEventListener('keyup', ignore, true);
    }
})();