surf

My custom build of surf web browser.
git clone https://git.mtkn.jp/surf
Log | Files | Refs | README | LICENSE

easier_key.js (3305B)


      1 // ==UserScript==
      2 // @name vimkeybindings
      3 // @namespace renevier.fdn.fr
      4 // @author arno <arenevier@fdn.fr>
      5 // @licence GPL/LGPL/MPL
      6 // @description use vim keybingings (i, j, k, l, …) to navigate a web page.
      7 // ==/UserScript==
      8 
      9 /*
     10 * If you're a vim addict, and you always find yourself typing j or k in a web
     11 * page, then wondering why it just does not go up and down like any good
     12 * software, that user script is what you have been looking for.
     13 */
     14 
     15 function up() {
     16     if (window.scrollByLines)
     17         window.scrollByLines(-1); // gecko
     18     else
     19         window.scrollBy(0, -24); // webkit
     20 }
     21 
     22 function down() {
     23     if (window.scrollByLines)
     24         window.scrollByLines(1); // gecko
     25     else
     26         window.scrollBy(0, 24); // webkit
     27 }
     28 
     29 function up_half() {
     30 	window.scrollBy(0, -window.innerHeight / 2);
     31 }
     32 
     33 function down_half() {
     34 	window.scrollBy(0, window.innerHeight / 2);
     35 }
     36 
     37 function pageup() {
     38     if (window.scrollByPages)
     39         window.scrollByPages(-1); // gecko
     40     else
     41         window.scrollBy(0, 0 - _pageScroll()); // webkit
     42 }
     43 
     44 function pagedown() {
     45     if (window.scrollByPages)
     46         window.scrollByPages(1); // gecko
     47     else
     48         window.scrollBy(0, _pageScroll()); // webkit
     49 }
     50 
     51 function right() {
     52     window.scrollBy(15, 0);
     53 }
     54 
     55 function left() {
     56     window.scrollBy(-15, 0);
     57 }
     58 
     59 function home() {
     60     window.scroll(0, 0);
     61 }
     62 
     63 function bottom() {
     64     window.scroll(document.width, document.height);
     65 }
     66 
     67 function reload() {
     68 	location.reload();
     69 }
     70 
     71 function hard_reload() {
     72 	location.reload(true);
     73 }
     74 
     75 function forward() {
     76 	history.forward();
     77 }
     78 
     79 function back() {
     80 	history.back();
     81 }
     82 
     83 // If you don't like default key bindings, customize here.
     84 // if you want to use the combination 'Ctrl + b' (for example), use '^b'
     85 var bindings = {
     86 	// scroll
     87     'h' : left,
     88     'l' : right,
     89     'k' : up,
     90 	'^y': up,
     91     'j' : down,
     92 	'^e': down,
     93 	'^u': up_half,
     94 	'^d': down_half,
     95     'g' : home,
     96     'G' : bottom,
     97     '^b': pageup,
     98     '^f': pagedown,
     99 
    100 	// reload
    101 	'r' : reload,
    102 	'R' : hard_reload,
    103 	'L' : forward,
    104 	'H' : back,
    105 }
    106 
    107 function isEditable(element) {
    108 
    109     if (element.nodeName.toLowerCase() == "textarea")
    110         return true;
    111 
    112     // we don't get keypress events for text input, but I don't known
    113     // if it's a bug, so let's test that
    114     if (element.nodeName.toLowerCase() == "input" && element.type == "text")
    115         return true;
    116     if (element.nodeName.toLowerCase() == "input" && element.type == "search")
    117         return true;
    118 
    119     // element is editable
    120     if (document.designMode == "on" || element.contentEditable == "true") {
    121         return true;
    122     }
    123 
    124     return false;
    125 }
    126 
    127 function keypress(evt) {
    128     var target = evt.target;
    129 
    130     // if we're on a editable element, we probably don't want to catch
    131     // keypress, we just want to write the typed character.
    132     if (isEditable(target))
    133         return;
    134 
    135     var key = String.fromCharCode(evt.charCode);
    136     if (evt.ctrlKey) {
    137         key = '^' + key;
    138     }
    139 
    140     var fun = bindings[key];
    141     if (fun)
    142         fun();
    143 
    144 }
    145 
    146 function _pageScroll() {
    147     // Gecko algorithm
    148     // ----------------
    149     // The page increment is the size of the page, minus the smaller of
    150     // 10% of the size or 2 lines.
    151     return window.innerHeight - Math.min(window.innerHeight / 10, 24);
    152 }
    153 
    154 window.addEventListener("keypress", keypress, false);