// ==UserScript== // @name DDG-like Vim keybindings for Ecosia // @namespace https://gist.github.com/tycho01/f5d319a89d0c21ebcdae94f68b6bf604 // @version 0.01 // @description plants trees while navigating search results by keyboard! // @author tycho01 // @match https://www.ecosia.org/search?q=* // @grant none // @esversion 6 // @downloadURL none // ==/UserScript== (function() { 'use strict'; const get_res = i => document.getElementsByClassName('result')[i]; var hit = -1; // global function select_hit(next = true) { const el_ = get_res(hit); if(el_) el_.classList.remove("highlight"); hit = Math.max(0, next ? hit + 1 : hit - 1); const el = get_res(hit); el.classList.add("highlight"); el.getElementsByTagName("a")[0].focus(); var y = window.scrollY + el.getBoundingClientRect().top; window.scroll({ top: y, behavior: 'smooth' }); } document.addEventListener('keydown', function(event) { const code = event.keyCode; if (code == 13) { // enter get_res(hit).click(); } // if not in the search box if (code == 40 || code == 74) { // down / j event.preventDefault(); select_hit(true); } if (code == 38 || code == 75) { // up / k event.preventDefault(); select_hit(false); } }); // define css class var style = document.createElement('style'); style.type = 'text/css'; style.innerHTML = '.highlight { background-color: rgba(220, 220, 255, 0.1); }'; document.getElementsByTagName('head')[0].appendChild(style); })();