-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVimafarian.user.js
278 lines (257 loc) · 6.51 KB
/
Vimafarian.user.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// ==UserScript==
// @name Vimafarian
// @author Adrian Rollett
// @namespace ACR
// @description vimperator clone for Safari/GreaseKit
// @exclude http://gmail.com/*
// @include *
// ==/UserScript==
//
// CREDITS
// http://vimperator.org/trac/wiki/Vimperator (of course)
// http://www.openjs.com/scripts/events/keyboard_shortcuts/
// http://www.quirksmode.org/js/keys.html
// other sites too numerous to name...
//
// @TODOs
// the 'y' key sure would be nice, but seems technically impossible
// to implement correctly at the moment
// motion ranges are needed...
// @BUGS
// help HUD pops up at top of page even when page is scrolled down
// globals
// this is hacky, necessitated by the lack of a js function for going
// to the user's home page in safari
var homePage='http://reluctanthacker.rollett.org/software/vimafarian/';
var pressed;
var actionCount=1;
var debug=1;
var suspend=false;
function log(input) {
if (debug) {
window.console.log(input);
}
}
log(history);
// functions
function goToBottom() {
var sh=document.body.scrollHeight;
var oh=document.body.offsetHeight;
if (sh > oh) window.scrollTo(0,sh);
else window.scrollTo(0,oh);
return false;
}
function openNewTab() {
// Change "_blank" to something like "newWindow" to load all links in the same new window
var newWindow = window.open('', 'New Tab', 'location=1,resizable=1,scrollbars=1');
newWindow.focus();
return false;
}
function upOneDir(docHref) {
var lastSlash = docHref.lastIndexOf('/');
var lengthOfHref = docHref.length;
if (lengthOfHref == (lastSlash + 1)) {
// if we're already at the top level, quit!
if (charCount(docHref, '/') <= 3) {
return docHref;
}
docHref = docHref.substr(0, (lengthOfHref - 2));
var lastSlash = docHref.lastIndexOf('/');
}
return docHref.substr(0, (lastSlash + 1));
}
function upToTop(docHref) {
var endIndex = 0;
var counter = 0;
var myArray = docHref.toLowerCase().split('');
for (i=0; i < myArray.length; i++)
{
if (myArray[i] == '/')
{
counter++;
}
if (counter == 2) {
endIndex = i;
}
}
if (endIndex != 0) {
return docHref.substr(0, (endIndex + 1));
} else {
return docHref;
}
}
function charCount(myString, myChar) {
var counter = 0;
var myArray = myString.toLowerCase().split('');
for (i=0;i<myArray.length;i++)
{
if (myArray[i] == myChar)
{
counter++;
}
}
return counter;
}
document.addEventListener('keypress',
function(e){
var element;
if(e.target) element=e.target;
else if(e.srcElement) element=e.srcElement;
if(element.nodeType==3) element=element.parentNode;
if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
var keyNum;
if (e.which) {
keyNum = e.which;
// here's where we find out the keyNum for ctrl chars
// alert(keyNum);
}
if (e.keyCode) {
keyCode = e.keyCode;
}
var keyChar = String.fromCharCode(keyNum);
// ctrl-z functionality
if (keyNum == 26) {
if (suspend) {
suspend = false;
window.status = '';
} else {
suspend = true;
window.status = '-- Vimafarian suspended --';
}
}
if (!suspend) {
// action Counts
if (keyChar > 1 && keyChar < 10) {
actionCount = parseInt(keyChar);
}
// Multi-key functions
else if (keyChar == 'g') {
// emulates 'gg'
if (pressed == 'g') {
window.scrollTo(0,0);
pressed = '';
} else {
pressed = 'g';
}
}
else if (keyChar == 'h') {
// emulates 'gh'
if (pressed == 'g') {
// go to user's home page
window.location=homePage;
pressed = '';
}
else {
window.scrollBy(-50,0);
}
}
else if (keyChar == 'u') {
// emulates 'gu'
if (pressed == 'g') {
// go up one level
newHref = upOneDir(window.location.href);
if (newHref != window.location.href) {
window.location=newHref;
}
pressed = '';
}
}
else if (keyChar == 'U') {
// emulates 'gU'
if (pressed == 'g') {
// go up one level
newHref = upToTop(window.location.href);
if (newHref != window.location.href) {
window.location=newHref;
}
pressed = '';
}
}
// Movement functions
// scroll to bottom
else if (keyChar == 'G') {
goToBottom();
}
// scroll up
else if (keyChar == 'j') {
window.scrollBy(0,50);
}
// scroll down
else if (keyChar == 'k') {
window.scrollBy(0,-50);
}
else if (keyChar == 'l') {
window.scrollBy(50,0);
}
// scroll down one page
// ctrl-f
else if (keyNum == 6) {
window.scrollBy(0, window.innerHeight);
}
// scroll up one page
// ctrl-b
else if (keyNum == 2) {
window.scrollBy(0, -window.innerHeight);
}
// scroll down half page
// ctrl-d
else if (keyNum == 4) {
window.scrollBy(0, (window.innerHeight / 2));
}
// scroll up half page
// ctrl-u
else if (keyNum == 21) {
window.scrollBy(0, -(window.innerHeight / 2));
}
// Navigation functions
// go back one page
// ctrl-o
else if (keyNum == 15) {
if (actionCount > history.length) {
actionCount = history.length - 1;
}
history.go(-actionCount);
actionCount = 1;
}
// go forward one page
// ctrl-i
else if (keyNum == 9) {
currentHref = window.location.href;
if (actionCount == 1) {
history.go(1);
} else {
while (actionCount > 1) {
log(actionCount);
if (!history.go(actionCount)) {
actionCount = actionCount - 1;
} else {
break;
}
}
}
actionCount = 1;
}
// Browser functions
// stop loading current page
// ctrl-c
else if (keyNum == 3) {
window.stop();
}
// close window
else if (keyChar == 'd') {
window.open('', '_self', '');
top.window.close();
}
else if (keyChar == 'r') {
window.location.reload();
}
else if (keyChar == 't') {
openNewTab();
}
else if (keyChar == 'y') {
var y=window.location.href;
alert(y);
}
}
},
true);