Greasy Fork is available in English.

Google 検索窓を複製

検索窓をページ下部にも表示します。

質問やレビューの投稿はこちらへ、スクリプトの通報はこちらへどうぞ。
// ==UserScript==
// @name        Google 検索窓を複製
// @name:ja     Google 検索窓を複製
// @description Also shows the search box to the page bottom.
// @description:ja 検索窓をページ下部にも表示します。
// @namespace   http://userscripts.org/users/347021
// @version     3.0.2
// @include     https://www.google.*/*
// @include     https://www.google.*/?*
// @include     https://www.google.*/#*
// @include     https://www.google.*/webhp
// @include     https://www.google.*/webhp?*
// @include     https://www.google.*/webhp#*
// @include     https://www.google.*/search*
// @include     https://www.google.*/search?*
// @include     https://www.google.*/search#*
// @exclude     https://www.google.*/search?*tbm=isch*
// @require     https://greasyfork.org/scripts/17895/code/polyfill.js?version=189394
// @require     https://greasyfork.org/scripts/19616/code/utilities.js?version=230651
// @require     https://greasyfork.org/scripts/17896/code/start-script.js?version=112958
// @license     Mozilla Public License Version 2.0 (MPL 2.0); https://www.mozilla.org/MPL/2.0/
// @compatible  Edge 非推奨 / Deprecated
// @compatible  Firefox
// @compatible  Opera
// @compatible  Chrome
// @grant       dummy
// @run-at      document-start
// @icon        data:image/vnd.microsoft.icon;base64,AAABAAEAMDAAAAEAIADXCwAAFgAAAIlQTkcNChoKAAAADUlIRFIAAAAwAAAAMAgGAAAAVwL5hwAAC55JREFUaIHNmdmTXNV9xz/n3K336ekezWi0IJBkjATGTgyIxeV4icGG2MaqpFIJ5eg5D3lJ8cBD8pS/IDwklUpcocopkqoslB0bV1yUKQULEBisgNBImhlJI2mYtaen9773niUPt7vVEkLqESrjX9Wvunvm9jnf7/ntp+GjIpaWlg52Op1/jOP4tLVW209RjDFhHMcnWq3WX8/Ozu66Dt4r8vzzzwfVavUv4jhe6315eKHbqqOsea2EYXh2ZWXle4AcnPYQ/mBjY+NvisXis0IIYYy5IdnftAghEEJgjGlWKpW/mpyc/FsYYjI7O/uH2Wz2z38bwQNYazHGIKXMZbPZZ48ePXoAwAUolUqFUqn0jO/7OWMM1t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/KZbH5q+9RnCoWCUywWBwO9EAIp5W9cHcchCAIKhQLj4+M0Gg3OnTtf++8f//ilt3/1q18Ay26PQAO4/MMXX/xJrVnj8cef+Oqde/ZMTE/vxHWd3vDwcT58baxc5yfV/sctBq6Uyfl2Oh3m5uf14uXLy//5Xy+9/Prrr78CrALN/tISKAN7gQM7d+68/8knnzy0Y3py0vVS3u0KwFsTQRh2u3Nz8wsvv/yTt+v15kngDDDPEAFIakIB2AHcAUwDJSDD7fqV+tbEAE1gDVgGLvZeG4C5FphDArgIbANygM/Q9cunIBoIgU1gA6gBHXr3ef8PS5/7nA7Q79AAAAAASUVORK5CYII=
// @author      100の人
// @homepage    https://greasyfork.org/scripts/274
// ==/UserScript==

(function () {
'use strict';

class GoogleBottomSearchBox
{
	/**
	 * messageイベントで使用する識別子。
	 * @constant {string}
	 */
	static get ID() {return 'google-bottom-search-box-137';}

	constructor()
	{
		startScript(
			() => {
				if (document.querySelector('#csi + script, #csi + a')) {
					// インスタント検索が有効
					this.main();
				} else if (location.pathname === '/search') {
					// インスタント検索が無効
					this.mainWithoutInstant();
				}
			},
			parent => parent.id === 'viewport' || /* インスタンス検索が無効 */ parent === document.body,
			target => target.id === 'main',
			() => document.getElementById('main')
		);
	}

	/**
	 * @param {Event} event
	 */
	handleEvent(event)
	{
		switch (event.type) {
			case 'focus':
				event.target.closest('#sfdiv').classList.add('sbfcn');
				break;
			case 'blur':
				event.target.closest('#sfdiv').classList.remove('sbfcn');
				break;
			case 'mouseup':
				if (event.target.name !== 'q') {
					event.target.closest('form').q.focus();
				}
				break;
			case 'hashchange':
			case 'message':
				if (event.type === 'hashchange'
					|| event.origin === location.origin && typeof event.data === 'object' && event.data !== null
						&& event.data.id === GoogleBottomSearchBox.ID) {
					if (this.getTbm(location) === this.getTbm(new URL(event.oldURL || event.data.oldURL))) {
						document.querySelector('#foot ~ form [name="q"]').value
							= new URLSearchParams(location.hash.replace('#', '')).get('q') || '';
					}
				}
				break;
		}
	}

	/**
	 * @access protected
	 */
	main()
	{
		this.observeFooterInserting(() => {
			if (!document.querySelector('#foot ~ form [name="q"]')) {
				this.cloneForm();
				this.synchronizeSearchWord();
				this.waitSearchControl().then(() => this.setEventListeners());
			}
		});
	}

	/**
	 * URLのtbmパラメータを取得します。
	 * @access protected
	 * @param {(Location|URL)} url
	 * @returns {string}
	 */
	getTbm(url)
	{
		return new URLSearchParams(url.hash.replace('#', '')).get('tbm')
			|| new URLSearchParams(url.search).get('tbm') || '';
	}

	/**
	 * ページにフッタが挿入されるのを監視します。
	 * @access protected
	 * @param {Function} callback - フッタが挿入されるたびに呼び出されるコールバック関数。
	 * @returns {Promise.<void>}
	 */
	observeFooterInserting(callback)
	{
		new MutationObserver(function (mutations, observer) {
			mutations: for (const mutation of mutations) {
				for (const node of mutation.addedNodes) {
					if (node.id === 'cnt' || node.id === 'foot') {
						callback();
						break mutations;
					}
				}
			}
		}).observe(document.getElementById('main'), {childList: true, subtree: true});
	}

	/**
	 * フォームを複製して挿入します。
 	 * @access protected
	 */
	cloneForm()
	{
		document.head.insertAdjacentHTML('beforeend', `<style>
			.hp #foot ~ form {
				/* トップページから完全に切り替わるまではフォームを表示しない */
				display: none;
			}
			#foot ~ form {
				margin-bottom: 1em;
			}
			:not(.mw) > * > #foot ~ form .tsf-p {
				padding-left: 8px;
			}
			#foot ~ form #sfdiv:hover {
				box-shadow: 0 3px 8px 0 rgba(0,0,0,0.2), 0 0 0 1px rgba(0,0,0,0.08);
			}
		</style>`);

		const bottomForm = document.getElementById('tsf').cloneNode(true);
		bottomForm.querySelector('#logocont').remove();
		document.getElementById('foot').after(bottomForm);
	}

	/**
	 * テキスト入力欄が置換されるのを待機し、実行時点で置換されて居れなければ、複製して置換します。
 	 * @access protected
 	 * @returns {Promise.<void>}
	 */
	waitSearchControl()
	{
		return new Promise(function (resolve) {
			if (document.getElementsByClassName('sbib_a')[0]) {
				resolve();
			} else {
				const ancestors = document.getElementsByClassName('lst-c');
				new MutationObserver((mutations, observer) => {
					observer.disconnect();
					const clone = ancestors[0].cloneNode(true);
					clone.getElementsByTagName('input')[0].removeAttribute('autocomplete');
					ancestors[1].replaceWith(clone);
					resolve();
				}).observe(ancestors[0], {childList: true});
			}
		});
	}

	/**
	 * フォーカス時とクリック時のイベントリスナーを設定します。
 	 * @access protected
	 */
	setEventListeners()
	{
		const form = document.querySelector('#foot ~ form');
		// 検索窓にフォーカスが移った時
		const q = form.q;
		q.addEventListener('focus', this);
		q.addEventListener('blur', this);
		// 検索窓をクリックしたとき
		form.getElementsByClassName('sbib_a')[0].addEventListener('mouseup', this);
	}

	/**
	 * 疑似ページ移動時、複製した検索窓に検索語句を反映します。
 	 * @access protected
	 */
	synchronizeSearchWord()
	{
		if (!this.alreadyObserved) {
			this.alreadyObserved = true;
			GreasemonkeyUtils.executeOnUnsafeContext(function (id) {
				History.prototype.pushState = new Proxy(History.prototype.pushState, {
					apply(target, thisArg, argumentsList)
					{
						const oldURL = location.href;
						const returnValue = Reflect.apply(target, thisArg, argumentsList);
						window.postMessage({id, oldURL}, location.origin);
						return returnValue;
					},
				});
			}, [GoogleBottomSearchBox.ID]);
			window.addEventListener('message', this);
			window.addEventListener('hashchange', this);
		}
	}

	/**
	 * インスタント検索無効時の処理。
 	 * @access protected
	 */
	mainWithoutInstant()
	{
		// body要素挿入時に実行し、Google検索のバージョンを判別する
		let textBoxId, inputNodeId, inputParentNodesClassName, textBoxBorderClass, classOnfocuse, previousSiblingId;

		let isTargetParent, isTarget, functionsForFirefox;
		if (document.body.id) {
			if (document.body.getAttribute('marginheight')) {
				// User-AgentがFirefox
				textBoxId = 'tsf';
				inputNodeId = 'lst-ib';
				inputParentNodesClassName = 'lst-d';
				textBoxBorderClass = 'lst-td';
				classOnfocuse = ['lst-d-f'];
			} else {
				// Google Chrome版 (UAがOpera、Google Chrome、IE8以降)
				textBoxId = 'gbqf';
				inputNodeId = 'gbqfq';
				inputParentNodesClassName = 'gbqfqwc';
				textBoxBorderClass = 'gbqfqw';
				classOnfocuse = ['gbqfqwf', 'gsfe_b'];
			}
			previousSiblingId = 'xjs';

			isTargetParent = parent => parent.id === 'foot';
			isTarget = target => target.id === 'xjs';
			functionsForFirefox = {
				isTargetParent: parent => parent.classList.contains('mw'),
				isTarget(target)
				{
					const firstElementChild = target.firstElementChild;
					return firstElementChild && firstElementChild.id === 'foot';
				},
			};
		} else {
			// IE7版 (UAがIE7以下、またはJavaScriptが無効)
			textBoxId = 'tsf';
			previousSiblingId = 'nav';
			isTargetParent = parent => parent.id === 'foot';
			isTarget = target => target.id === 'nav';
			functionsForFirefox = {
				isTargetParent: parent => parent.localName === 'tbody' && parent.parentNode.id === 'mn',
				isTarget(target)
				{
					const cells = target.cells;
					return cells && cells[0] && cells[0].id === 'leftnav';
				},
			};
		}

		startScript(
			function () {
				// スタイルシートの設定
				document.head.insertAdjacentHTML('beforeend', `<style>
					#foot form {
						margin-top: 13px;
					}

					#foot > form {
						margin-bottom: 1em;
					}

					/*------------------------------------
						Firefox版
					*/
					#foot .nojsv {
						display: none;
					}
					#foot .tsf-p {
						width: 631px;
						padding-left: 8px;
					}
					#nav {
						margin-bottom: initial !important;
					}
				</style>`);

				// 検索ボックスを取得
				const original = document.getElementById(textBoxId);
				if (!original) {
					return;
				}

				// 複製
				const bottomForm = original.cloneNode(true);

				// 移動先を取得
				const previousSibling = document.getElementById(previousSiblingId);

				// 挿入
				previousSibling.parentNode.insertBefore(bottomForm, previousSibling.nextSibling);

				let textBoxBorder, textBoxBorderClassList, inputParentNodes, submitButtonClassList;

				// ページ描画後のスクリプトによる書き換えを待機
				if (inputParentNodesClassName) {
					inputParentNodes = document.getElementsByClassName(inputParentNodesClassName);
					startScript(
						function () {
							// 後から挿入された検索窓を複製
							const table = inputParentNodes[0].firstElementChild.cloneNode(true);
							// オートコンプリートを有効に
							table.getElementsByTagName('input')[0].removeAttribute('autocomplete');
							// 下の検索窓を置き換え
							inputParentNodes[1].replaceChild(table, inputParentNodes[1].firstElementChild);
						},
						parent => parent.id === 'gs_lc0',
						target => target.id === inputNodeId,
						() => document.querySelector('#' + inputNodeId + '[style]')
					);
				}

				// 検索窓にフォーカスが移った時
				if (textBoxBorderClass) {
					textBoxBorder = bottomForm.getElementsByClassName(textBoxBorderClass)[0];
					textBoxBorderClassList = textBoxBorder.classList;
					textBoxBorder.addEventListener('focus', function () {
						textBoxBorderClassList.add(...classOnfocuse);
					}, true);

					textBoxBorder.addEventListener('blur', function () {
						textBoxBorderClassList.remove(...classOnfocuse);
					}, true);

					// 検索窓をクリックしたとき
					textBoxBorder.addEventListener('click', function (event) {
						if (event.target.localName !== 'input') {
							bottomForm.elements.namedItem('q').focus();
						}
					});
				}

				// 検索窓にマウスが載ったとき
				const submitButton = bottomForm.getElementsByClassName('gbqfb')[0];
				if (submitButton) {
					submitButtonClassList = submitButton.classList;
					bottomForm.addEventListener('mouseover', function (event) {
						if (textBoxBorder.contains(event.target)) {
							// 検索窓
							textBoxBorderClassList.add('gbqfqw-hvr', 'gsfe_a');
						} else if (submitButton.contains(event.target)) {
							// 検索ボタン
							submitButtonClassList.add('gbqfb-hvr');
						}
					});

					bottomForm.addEventListener('mouseout', function (event) {
						if (!textBoxBorder.contains(event.relatedTarget)) {
							// 検索窓
							textBoxBorderClassList.remove('gbqfqw-hvr', 'gsfe_a');
						}
						if (!submitButton.contains(event.relatedTarget)) {
							// 検索ボタン
							submitButtonClassList.remove('gbqfb-hvr');
						}
					});
				}
			},
			isTargetParent,
			isTarget,
			() => document.getElementById(previousSiblingId),
			functionsForFirefox
		);
	}
}

new GoogleBottomSearchBox();

})();