Opens “Tools” Menu by Default on Google

Opens the “Tools” menu on Google Search automatically when page loaded.

当前为 2024-02-14 提交的版本,查看 最新版本

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址