-
Notifications
You must be signed in to change notification settings - Fork 2
/
chromacs.js
102 lines (82 loc) · 2.56 KB
/
chromacs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// tell chrome to listen for a keypress
window.addEventListener( "keydown", key_press, true );
var body_bindings = {
// basic history navigation
"r": function () { location.reload() },
"B": function () { history.back() },
"F": function () { history.forward() },
// page navigation
"C-n": function () { scrollBy( 0, 30 ) },
"C-p": function () { scrollBy( 0, -30 ) },
"n": function () { scrollBy( 0, 30 ) },
"p": function () { scrollBy( 0, -30 ) },
"M-n": function () {
chrome.extension.connect().postMessage({
action: "next_tab"
});
},
"M-p": function () {
chrome.extension.connect().postMessage({
action: "previous_tab",
});
},
"C-x": {
"k": function () {
chrome.extension.connect().postMessage({
action: "close_tab",
});
},
"C-f": function () {
chrome.extension.connect().postMessage({
action: "open_url",
newtab: true,
url: "about:blank"
});
}
}
};
var text_area_bindings = { };
// this variable will contain either an object or a function which
// allow chromacs to determine what to do with the current keypress.
var current_binding;
// behaviour obviously affected by input type
function get_current_binding( target_type ) {
return ( target_type == "input"
|| target_type == "textarea" )
? text_area_bindings
: body_bindings;
}
function key_press( e ) {
var key = get_key( e ),
target_type = e.target.tagName.toLowerCase();
console.log( key );
if ( ! current_binding ) {
current_binding = get_current_binding( target_type );
}
// action we're going to take with the current binding
var command = current_binding[ key ];
switch ( typeof command ) {
case "function":
command();
// reset any chaing we have going on
current_binding = null;
e.preventDefault();
break;
case "object":
// set the current binding to that of the chain
current_binding = command;
e.preventDefault();
break;
default:
// abort
current_binding = null;
break;
}
}
function get_key( e ) {
var key = String.fromCharCode( e.keyCode ),
ctrl = e.ctrlKey ? "C-" : "",
meta = ( e.metaKey || e.altKey ) ? "M-" : "",
shift = e.shiftKey ? "S-" : "";
return ctrl + meta + ( shift ? key : key.toLowerCase() );
}