Skip to content

Instantly share code, notes, and snippets.

@ViktorQvarfordt
Last active March 9, 2024 14:57
Show Gist options
  • Save ViktorQvarfordt/1d81cbf5a5bebbaaee90 to your computer and use it in GitHub Desktop.
Save ViktorQvarfordt/1d81cbf5a5bebbaaee90 to your computer and use it in GitHub Desktop.
Google search tweaks. User script. Shortcuts for selecting search results. Block ads and sponsored links.
// ==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].innerHTML = `<span style="position: absolute; left: -20px; top: 4px;">${i+1}</span>${gs[i].innerHTML}`;
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());
}());
@ooker777
Copy link

ooker777 commented Feb 25, 2017

This script conflicts with the Google Hit Hider script. The author of it suggests changing the line 39 from

gs[i].innerHTML = `<span style="position: absolute; left: -20px; top: 4px;">${i+1}</span>${gs[i].innerHTML}`;

to

gs[i].insertAdjacentHTML('afterbegin', `<span style="position: absolute; left: -20px; top: 4px;">${i+1}</span>`);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment