Last active
March 29, 2023 00:15
-
-
Save stretchkennedy/a22a2c1f7d2b7ef2e01839d82929aafd to your computer and use it in GitHub Desktop.
Tampermonkey script to scroll through Google results with J/K
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name J/K through Google results | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author Stretchkennedy | |
// @match https://www.google.com/search?* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=google.com | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
let index = null; | |
function setIndex(newIndex) { | |
index = index === null ? 0 : newIndex; // if nothing's selected, always select first | |
const searchLinks = [...document.getElementById('search').getElementsByTagName('a')]; | |
searchLinks.forEach(link => link.removeAttribute('style')); | |
const visibleLinks = searchLinks.filter(link => link.offsetWidth !== 0 || link.offsetHeight !== 0); | |
index = index % visibleLinks.length; // be overly paranoid; prevent integer overflow | |
const selectedLink = visibleLinks[index]; | |
selectedLink.setAttribute('style', 'background: yellow;'); | |
selectedLink.focus({ focusVisible: true }); | |
} | |
document.addEventListener('keydown', evt => { | |
if (evt.key === 'j') { | |
setIndex(index - 1); | |
} | |
else if (evt.key === 'k') { | |
setIndex(index + 1); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment