Skip to content

Instantly share code, notes, and snippets.

@stretchkennedy
Last active March 29, 2023 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stretchkennedy/a22a2c1f7d2b7ef2e01839d82929aafd to your computer and use it in GitHub Desktop.
Save stretchkennedy/a22a2c1f7d2b7ef2e01839d82929aafd to your computer and use it in GitHub Desktop.
Tampermonkey script to scroll through Google results with J/K
// ==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