預設開啟 Google 的「工具」選單

在 Google 搜尋載入後自動打開「工具」選單。

目前為 2024-02-21 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name               Opens “Tools” Menu by Default on Google
// @name:zh-TW         預設開啟 Google 的「工具」選單
// @name:zh-CN         预设开启 Google 的“工具”选单
// @name:ja            Google のデフォルトで「ツール」メニューを開く
// @description        Opens the “Tools” menu on Google Search automatically when page loaded.
// @description:zh-TW  在 Google 搜尋載入後自動打開「工具」選單。
// @description:zh-CN  在 Google 搜索载入后自动打开“工具”选单。
// @description:ja     ページが読み込まれると、Google 検索の「ツール」メニューが自動的に開きます。
// @icon               https://wsrv.nl/?url=https://www.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png
// @author             Jason Kwok
// @namespace          https://jasonhk.dev/
// @version            1.1.4
// @license            MIT
// @match              https://www.google.com/search
// @match              https://www.google.com/search?*
// @run-at             document-end
// @grant              none
// @supportURL         https://greasyfork.org/scripts/460247/feedback
// ==/UserScript==

const TOOLS_BUTTON_ATTRIBUTES = [
    ["aria-controls", "hdtbMenus"],
    ["jsname", "I4bIT"],
    ["jscontroller", "LbcJwc"],
];

const TOOLS_BUTTON_SELECTOR = TOOLS_BUTTON_ATTRIBUTES.map(([name, value]) => `[${name}=${value}]`).join(",");

function handleToolsButton(button)
{
    console.debug("“Tools” button found, activating the “Tools” menu...");

    clearTimeout(observerTimeout);
    observer.disconnect();

    const interval = setInterval(() =>
    {
        if (button.getAttribute("aria-expanded") === "true")
        {
            console.debug("“Tools” menu activated, congration!");

            clearTimeout(activateTimeout);
            clearInterval(interval);
        }
        else
        {
            button.click();
        }
    }, 250);

    const activateTimeout = setTimeout(() =>
    {
        console.debug("“Tools” menu not opened, aborting...");
        clearInterval(interval);
    }, 120000);
}

const observer = new MutationObserver((records) =>
{
    for (const record of records)
    {
        for (const node of record.addedNodes)
        {
            if (node instanceof Element)
            {
                if (TOOLS_BUTTON_ATTRIBUTES.some(([name, value]) => (node.getAttribute(name) === value)))
                {
                    handleToolsButton(node);
                }
                else
                {
                    const button = node.querySelector(TOOLS_BUTTON_SELECTOR);
                    if (button)
                    {
                        handleToolsButton(button);
                    }
                }
            }
        }
    }
});

observer.observe(document.body, { subtree: true, childList: true });
const observerTimeout = setTimeout(() =>
{
    console.debug("“Tools” button still not exist, aborting...");
    observer.disconnect();
}, 120000);

const button = document.querySelector(TOOLS_BUTTON_SELECTOR);
if (button)
{
    handleToolsButton(button);
}
else
{
    console.debug("“Tools” button not exist, attempting the MutationObserver method...");
}