[dev] [surf]easier keyboard shortcuts

From: Thuban <thuban_AT_singularity.fr>
Date: Wed, 7 Nov 2012 10:02:06 +0100

hi all,
Instead of always using Ctrl + any other key to navigate with surf, here
is a simple replacement using vim usual shortcuts.
It is javascript, and simply typing h/j/k/l to move around is enough.
Pressing g or G bring you to the top or the bottom of the page.

Just add this code in ~/.surf.script.js
---
// ==UserScript==
// _AT_name vimkeybindings
// _AT_namespace renevier.fdn.fr
// _AT_author arno <arenevier_AT_fdn.fr>
// _AT_licence GPL/LGPL/MPL
// _AT_description use vim keybingings (i, j, k, l, …) to navigate a web page.
// ==/UserScript==
/*
 * If you're a vim addict, and you always find yourself typing j or k in a web
 * page, then wondering why it just does not go up and down like any good
 * software, that user script is what you have been looking for.
 */
function up() {
    if (window.scrollByLines)
        window.scrollByLines(-1); // gecko
    else
        window.scrollBy(0, -12); // webkit
}
function down() {
    if (window.scrollByLines)
        window.scrollByLines(1); // gecko
    else
        window.scrollBy(0, 12); // webkit
}
function pageup() {
    if (window.scrollByPages)
        window.scrollByPages(-1); // gecko
    else
        window.scrollBy(0, 0 - _pageScroll()); // webkit
}
function pagedown() {
    if (window.scrollByPages)
        window.scrollByPages(1); // gecko
    else
        window.scrollBy(0, _pageScroll()); // webkit
}
function right() {
    window.scrollBy(15, 0);
}
function left() {
    window.scrollBy(-15, 0);
}
function home() {
    window.scroll(0, 0);
}
function bottom() {
    window.scroll(document.width, document.height);
}
// If you don't like default key bindings, customize here. 
// if you want to use the combination 'Ctrl + b' (for example), use '^b'
var bindings = {
    'h' : left, 
    'l' : right,
    'k' : up,
    'j' : down,
    'g' : home,
    'G' : bottom,
    //'^b': pageup,
    //'^f': pagedown,
}
function isEditable(element) {
    
    if (element.nodeName.toLowerCase() == "textarea")
        return true;
    // we don't get keypress events for text input, but I don't known
    // if it's a bug, so let's test that
    if (element.nodeName.toLowerCase() == "input" && element.type == "text")
        return true;
    // element is editable
    if (document.designMode == "on" || element.contentEditable == "true") {
        return true;
    }
    
    return false;
}
function keypress(evt) {
    var target = evt.target;
            
    // if we're on a editable element, we probably don't want to catch
    // keypress, we just want to write the typed character.
    if (isEditable(target))
        return;
    var key = String.fromCharCode(evt.charCode);
    if (evt.ctrlKey) {
        key = '^' + key;
    }
    var fun = bindings[key];
    if (fun)
        fun();
}
function _pageScroll() {
    // Gecko algorithm
    // ----------------
    // The page increment is the size of the page, minus the smaller of
    // 10% of the size or 2 lines.  
    return window.innerHeight - Math.min(window.innerHeight / 10, 24);
}
window.addEventListener("keypress", keypress, false);
-- 
 ,--.   Xavier Cartron                           ,   /(
: /` )  M2 MEFPC                        o     **}=\\,\(,,  
| `-'   Debian user                    <0--        (___(_\\
 \_     jabber : thuban_AT_jabber.fr      / >        ,)   ,/ ``==>
Received on Wed Nov 07 2012 - 10:02:06 CET

This archive was generated by hypermail 2.3.0 : Wed Nov 07 2012 - 10:12:04 CET