Mock Old Search Bar

Slightly customizes the search bar on the tool bar to the old one’s like behavior.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Mock Old Search Bar
// @name:ja     旧検索バーもどき
// @description Slightly customizes the search bar on the tool bar to the old one’s like behavior.
// @description:ja ツールバーの検索バーの挙動を、旧検索バー風に微調整します。
// @namespace   https://greasyfork.org/users/137
// @version     1.3.0
// @include     main
// @license     MPL-2.0
// @contributionURL https://github.com/sponsors/esperecyan
// @compatible  Firefox userChromeJS用スクリプト です (※GreasemonkeyスクリプトでもuserChromeES用スクリプトでもありません) / This script is for userChromeJS (* neither Greasemonkey nor userChromeES)
// @incompatible Opera
// @incompatible Chrome
// @charset     UTF-8
// @author      100の人
// @homepageURL https://greasyfork.org/scripts/35379
// ==/UserScript==

new (class {
	constructor()
	{
		/**
		 * @access private
		 * @type {Ci.nsIDOMXULPopupElement}
		 */
		this.popup = document.getElementById('PopupSearchAutoComplete');

		/**
		 * @access private
		 * @type {Ci.nsIDOMXULPopupElement}
		 */
		this.buttons = this.popup.oneOffButtons.buttons;

		this.buttons.addEventListener('click', this);
		this.integrateCurrentEngine();
	}

	/**
	 * ワンクリック検索ボタンクリック時に検索を実行せず、既定の検索エンジンを切り替えてポップアップを閉じます。
	 * @param {MouseEvent} event - clickイベント。
	 */
	handleEvent(event)
	{
		if (event.button === 0 && event.originalTarget.engine) {
			event.stopPropagation();
			this.popup.closePopup();
			Services.search.setDefault(event.originalTarget.engine, Services.search.CHANGE_REASON_USER_SEARCHBAR);
		}
	}

	/**
	 * CustomCSSforFxのsearchbar_popup_engines_show_labels.css向けに、既定の検索エンジンを太字にします。
	 * @access private
	 * @type {Object.<Function>}
	 * @see {@link https://github.com/Aris-t2/CustomCSSforFx/blob/master/classic/css/generalui/searchbar_popup_engines_show_labels.css}
	 */
	setCurrentEngineButtonHighlightTrap()
	{
		new MutationObserver(function (mutations) {
			const name = Services.search.defaultEngine.name;
			for (const mutation of mutations) {
				const currentEngineButton
					= Array.from(mutation.addedNodes).find(node => node.getAttribute('tooltiptext') === name);
				if (currentEngineButton) {
					currentEngineButton.style.fontWeight = 'bold';
					break;
				}
			}
		}).observe(this.buttons, { childList: true });
	}

	/**
	 * @access private
	 * @type {Object.<Function>}
	 * 		【Firefox 60 ESR】CustomCSSforFxのsearchbar_popup_engines_show_labels.css向けに、既定の検索エンジンを太字にします。
	 */
	get currentEngineButtonHighlightTrap()
	{
		return {apply: (target, thisArg, argumentsList) => {
			const returnValue = Reflect.apply(target, thisArg, argumentsList);

			for (const button of this.buttons.querySelectorAll(
				'.searchbar-engine-one-off-item:not(.dummy):not(.search-setting-button-compact)[style*="font-weight"]'
			)) {
				button.style.fontWeight = '';
			}
			this.buttons.querySelector(`[tooltiptext="${CSS.escape(Services.search.defaultEngine.name)}"]`)
				.style.fontWeight = 'bold';

			return returnValue;
		}};
	}

	/**
	 * 既定の検索エンジンを、他の検索エンジンのボタンリストに統合します。
	 * @access private
	 */
	integrateCurrentEngine()
	{
		let searchPanelCurrentEngine = this.popup.getElementsByClassName('search-panel-current-engine')[0];
		if (searchPanelCurrentEngine) {
			this.setCurrentEngineButtonHighlightTrap();
		} else {
			// Firefox 60 ESR
			searchPanelCurrentEngine
				= document.getAnonymousElementByAttribute(this.popup, 'anonid', 'searchbar-engine');
			this.popup.oneOffButtons._rebuild
				= new Proxy(this.popup.oneOffButtons._rebuild, this.currentEngineButtonHighlightTrap);
			BrowserSearch.searchBar.selectEngine
				= new Proxy(BrowserSearch.searchBar.selectEngine, this.currentEngineButtonHighlightTrap);
		}
		searchPanelCurrentEngine.style.display = 'none';
		this.popup.oneOffButtons.header.style.display = 'none';
		this.popup.oneOffButtons.setAttribute('includecurrentengine', 'on');
	}
})();