Skip to content

Instantly share code, notes, and snippets.

@ekk88d
Created August 16, 2023 12:08
Show Gist options
  • Save ekk88d/2b4244b5e1f6c6796e74bfc4fc933f60 to your computer and use it in GitHub Desktop.
Save ekk88d/2b4244b5e1f6c6796e74bfc4fc933f60 to your computer and use it in GitHub Desktop.
Google Search Tweaks - Viktor Qvarfordt (updated line 39)
// ==UserScript==
// @name Google search tweaks
// @description Shortcuts for selecting search results. Block ads and sponsored links.
// @author Viktor Qvarfordt
// @include /^https?:\/\/(www\.)?google.[^./]+\/search.*/
// @version 2016-08-21
// @grant none
// ==/UserScript==
// INSTALL: https://gist.github.com/ViktorQvarfordt/1d81cbf5a5bebbaaee90/raw/google-search-tweaks.user.js
(function() {
'use strict';
function addGlobalStyle(css) {
const head = document.getElementsByTagName('head')[0];
if (!head) { return; }
const style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
// Attempt to hide ads.
addGlobalStyle(`
iframe[name='google_ads_frame'],
a[href^='http://pagead2.googlesyndication.com'] {
display: none ! important;
}`);
// Remove ad search results
document.getElementById('tvcap').remove();
// Shortcuts for selecting search results
const gs = document.getElementsByClassName('g');
for (let i = 0; i < gs.length && i < 9; i++) {
gs[i].style.position = 'relative';
gs[i].insertAdjacentHTML('afterbegin', `<span style="position: absolute; left: -20px; top: 4px;">${i+1}</span>`);
document.addEventListener('keydown', e => {
if (e.keyCode === String(i+1).charCodeAt(0) || (e.keyCode === 13 && i === 0)) {
const link = gs[i].querySelector('.r a');
link.style['font-weight'] = 'bold';
window.location = link.href;
}
});
}
// No shortcuts from the search bar
window['lst-ib'].addEventListener('keydown', e => e.stopPropagation());
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment