nicolaslegland / focus

// ==UserScript==
// @author	  Nicolas Le Gland
// @copyright   2018-2019, nicolaslegland (https://openuserjs.org/users/nicolaslegland)
// @description Focus keyboard on search form input.
// @downloadURL https://openuserjs.org/src/scripts/nicolaslegland/focus.user.js
// @grant	   none
// @homepageURL https://openuserjs.org/scripts/nicolaslegland/focus
// @icon		https://img.icons8.com/color/48/000000/search.png
// @include	 https://news.google.com/*
// @include	 https://news.ycombinator.com/*
// @include	 https://www.reddit.com/*
// @installURL  https://openuserjs.org/src/scripts/nicolaslegland/focus.user.js
// @license	 GPL-3.0-or-later
// @name		focus
// @run-at	  document-end
// @supportURL  https://openuserjs.org/scripts/nicolaslegland/focus/issues
// @updateURL   https://openuserjs.org/src/scripts/nicolaslegland/focus.user.js
// @version	 2
// ==/UserScript==

// Test domain
if ('news.google.com' === document.domain)
{
	// Focus on search field
	var form = document.getElementsByTagName('form')[0];
	form.getElementsByTagName('input')[1].focus();

	// Hide dropdown list
	var list = document.getElementById('nngdp12');
	list.style.display = 'none';
	window.setTimeout(function ()
	{
		// Restore search field style
		form.className = 'gb_ve gb_4e';

		// Clear dropdown list
		list.setAttribute('data-childcount', 0);
		list.style.display = '';
	}
	, 1000);
}
else if ('news.ycombinator.com' === document.domain)
{
   	// Focus on search field
	document.getElementsByName('q')[0].focus();

	// Scroll back to top
	window.scrollTo(0, 0);
}
else if ('www.reddit.com' === document.domain)
{
   	// Focus on search field
	document.getElementById('header-search-bar').focus();
}