-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-chrome-search-tabbing-payload.js
44 lines (37 loc) · 1.26 KB
/
google-chrome-search-tabbing-payload.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
(function () {
var activeLinks = Array.prototype.filter.call(document.querySelectorAll('h3.r a'), function (el) {
return el;
});
if (activeLinks.length === 0) {
return;
}
var keyCodes = {
38: 'previous', // down
9: 'next', // tab
40: 'next' // up
};
var activeStyles = 'font-weight: bold;';
function addStylesToActiveLinks() {
document.activeElement.style.cssText += activeStyles;
}
function removeStylesFromActiveLinks() {
activeLinks.forEach(function (activeLink) {
activeLink.style.cssText = activeLink.style.cssText.replace(activeStyles, '');
});
}
function goToResult(direction) {
var indexOfActiveLink = activeLinks.indexOf(document.activeElement);
var previousIndex = indexOfActiveLink - 1 < 0 ? activeLinks.length - 1 : indexOfActiveLink - 1;
var nextIndex = indexOfActiveLink + 1 > activeLinks.length - 1 ? 0 : indexOfActiveLink + 1;
var indexToGoTo = direction === 'next' ? nextIndex : previousIndex;
removeStylesFromActiveLinks();
activeLinks[indexToGoTo].focus();
addStylesToActiveLinks();
}
window.addEventListener('keydown', function (e) {
if (keyCodes.hasOwnProperty(e.keyCode)) {
e.preventDefault();
goToResult(keyCodes[e.keyCode]);
}
});
})();