forked from thomasst/GoogleShortcuts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·100 lines (90 loc) · 2.69 KB
/
script.js
File metadata and controls
executable file
·100 lines (90 loc) · 2.69 KB
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
// note: key library was modified to enable capture in addEventListener
(function() {
var shortcuts = {
openInNewTab : '⌘+return, ctrl+return',
open : 'return',
up : 'k, up',
down : 'j, down',
focusInSearchInput : '/, 9, backspace',
previousPage : 'left',
nextPage : 'right',
};
var googleHtmlIdentifiers = {
searchInput : '#lst-ib',
nextButton : '#pnnext',
prevButton : '#pnprev'
};
var idx = 0;
var select = function(focus) {
$('div.r h3').css('background-color', 'inherit');
var link = $('div.r h3:nth('+idx+')');
link.css('background-color', '#fcc');
if (focus) {link.focus(); }
return link;
};
var gotoURLFromLink = function(button, ev){
if(button != null) {
var url = button.attr('href');
if(typeof url !== 'undefined') {
location.href = url;
}
}
ev.stopPropagation();
ev.preventDefault();
};
key(shortcuts.openInNewTab, function(ev) {
var link = select().parent();
window.open(link.attr('href'));
ev.stopPropagation();
ev.preventDefault();
});
key(shortcuts.open, function(ev) {
var link = select().parent();
location.href = link.attr('href');
ev.stopPropagation();
ev.preventDefault();
});
var node = null;
$(function() {
document.getElementById('main').addEventListener("DOMSubtreeModified", function () {
var newNode = $('a h3:nth(0)');
if (!node || (node.attr('href') != newNode.attr('href'))) {
idx = 0;
select();
node = newNode;
}
});
});
key(shortcuts.down, function(ev) {
if (idx < $('div.r h3').length-1) {
idx++;
select(true);
}
ev.stopPropagation();
});
key(shortcuts.up, function(ev) {
if (idx > 0) {
idx--;
select(true);
}
ev.stopPropagation();
});
key(shortcuts.focusInSearchInput, function (ev) {
var searchInput = $(googleHtmlIdentifiers.searchInput);
var textInitial = searchInput.val();
searchInput
.val('')
.val(textInitial)
.focus();
ev.stopPropagation();
ev.preventDefault();
});
key(shortcuts.nextPage, function (ev) {
var button = $(googleHtmlIdentifiers.nextButton);
gotoURLFromLink(button, ev);
});
key(shortcuts.previousPage, function (ev) {
var button = $(googleHtmlIdentifiers.prevButton);
gotoURLFromLink(button, ev);
});
})();