JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
blur on ESC, don't propogate bound key events
[userscripts.git] / numbered_links.user.js
index d29b24a..e442ca8 100644 (file)
@@ -325,31 +325,6 @@ function followLinks(follow) {
     }
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 // from your event handler you can: return stop_event(e)
 function stop_event(e) {
        // try {
@@ -359,32 +334,57 @@ function stop_event(e) {
        //      return false; // IE-compat
        // }
 }
-document.addEventListener(
-       'keydown',
-       function(e) {
-               // [de]activate on ^C
-               // deactivate on ESC
-               if(
-                       (e.ctrlKey && e.keyCode == 67)
-                       || (e.keyCode == 27 && active == 1)
-               ) {
-                       if(active) {
-                               got = '';
-                               removeAllHints();
-                       } else {
-                               followLinks(got);
-                       }
-                       active = 1 - active;
-                       return stop_event(e);
-               } else {
-                       if(active == 1 && !e.ctrlKey && !e.shiftKey && !e.altKey) {
-                               if(key_to_char[e.keyCode]) {
-                                       got += key_to_char[e.keyCode];
+
+function init() {
+       document.addEventListener(
+               'keydown',
+               function(e) {
+                       // [de]activate on ^C
+                       // deactivate on ESC
+                       if(
+                               (e.ctrlKey && e.keyCode == 67)
+                               || (e.keyCode == 27 && active == 1)
+                       ) {
+                               if(active) {
+                                       got = '';
+                                       removeAllHints();
+                               } else {
                                        followLinks(got);
-                                       return stop_event(e);
+                               }
+                               active = 1 - active;
+                               return stop_event(e);
+                       } else {
+                               if(active == 1 && !e.ctrlKey && !e.shiftKey && !e.altKey) {
+                                       if(key_to_char[e.keyCode]) {
+                                               got += key_to_char[e.keyCode];
+                                               followLinks(got);
+                                               return stop_event(e);
+                                       }
+                               } else {
+                                       // general keybinding, unrelated to numbered links
+                                       if (document.activeElement == document.body) { // FIXME get more specific
+                                               var c = key_to_char[e.keyCode];
+                                               switch (c) {
+                                                       case 'c':
+                                                               window.scrollBy(0, -200);
+                                                               stop_event(e);
+                                                       break;
+                                                       case 't':
+                                                               window.scrollBy(0, 200);
+                                                               stop_event(e);
+                                                       break;
+                                               }
+                                       } else if (e.keyCode == 27) {
+                                               // unfocus on ESC
+                                               document.activeElement.blur();
+                                               stop_event(e);
+                                       }
                                }
                        }
-               }
-       },
-       true);
+               },
+               true);
+}
+
+init();
+
 })();