Greasy Fork 还支持 简体中文。

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

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

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

  1. // ==UserScript==
  2. // @name Opens “Tools” Menu by Default on Google
  3. // @name:zh-TW 預設開啟 Google 的「工具」選單
  4. // @name:zh-CN 预设开启 Google 的“工具”选单
  5. // @name:ja Google のデフォルトで「ツール」メニューを開く
  6. // @description Opens the “Tools” menu on Google Search automatically when page loaded.
  7. // @description:zh-TW 在 Google 搜尋載入後自動打開「工具」選單。
  8. // @description:zh-CN 在 Google 搜索载入后自动打开“工具”选单。
  9. // @description:ja ページが読み込まれると、Google 検索の「ツール」メニューが自動的に開きます。
  10. // @icon https://wsrv.nl/?url=https://www.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png
  11. // @author Jason Kwok
  12. // @namespace https://jasonhk.dev/
  13. // @version 1.1.4
  14. // @license MIT
  15. // @match https://www.google.com/search
  16. // @match https://www.google.com/search?*
  17. // @run-at document-end
  18. // @grant none
  19. // @supportURL https://greasyfork.org/scripts/460247/feedback
  20. // ==/UserScript==
  21.  
  22. const TOOLS_BUTTON_ATTRIBUTES = [
  23. ["aria-controls", "hdtbMenus"],
  24. ["jsname", "I4bIT"],
  25. ["jscontroller", "LbcJwc"],
  26. ];
  27.  
  28. const TOOLS_BUTTON_SELECTOR = TOOLS_BUTTON_ATTRIBUTES.map(([name, value]) => `[${name}=${value}]`).join(",");
  29.  
  30. function handleToolsButton(button)
  31. {
  32. console.debug("“Tools” button found, activating the “Tools” menu...");
  33.  
  34. clearTimeout(observerTimeout);
  35. observer.disconnect();
  36.  
  37. const interval = setInterval(() =>
  38. {
  39. if (button.getAttribute("aria-expanded") === "true")
  40. {
  41. console.debug("“Tools” menu activated, congration!");
  42.  
  43. clearTimeout(activateTimeout);
  44. clearInterval(interval);
  45. }
  46. else
  47. {
  48. button.click();
  49. }
  50. }, 250);
  51.  
  52. const activateTimeout = setTimeout(() =>
  53. {
  54. console.debug("“Tools” menu not opened, aborting...");
  55. clearInterval(interval);
  56. }, 120000);
  57. }
  58.  
  59. const observer = new MutationObserver((records) =>
  60. {
  61. for (const record of records)
  62. {
  63. for (const node of record.addedNodes)
  64. {
  65. if (node instanceof Element)
  66. {
  67. if (TOOLS_BUTTON_ATTRIBUTES.some(([name, value]) => (node.getAttribute(name) === value)))
  68. {
  69. handleToolsButton(node);
  70. }
  71. else
  72. {
  73. const button = node.querySelector(TOOLS_BUTTON_SELECTOR);
  74. if (button)
  75. {
  76. handleToolsButton(button);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. });
  83.  
  84. observer.observe(document.body, { subtree: true, childList: true });
  85. const observerTimeout = setTimeout(() =>
  86. {
  87. console.debug("“Tools” button still not exist, aborting...");
  88. observer.disconnect();
  89. }, 120000);
  90.  
  91. const button = document.querySelector(TOOLS_BUTTON_SELECTOR);
  92. if (button)
  93. {
  94. handleToolsButton(button);
  95. }
  96. else
  97. {
  98. console.debug("“Tools” button not exist, attempting the MutationObserver method...");
  99. }