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.2
  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://greasyfork.org/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(timeout);
  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. clearInterval(interval);
  39. }
  40. else
  41. {
  42. button.click();
  43. }
  44. }, 250);
  45. }
  46.  
  47. const observer = new MutationObserver((records) =>
  48. {
  49. for (const record of records)
  50. {
  51. for (const node of record.addedNodes)
  52. {
  53. if (node instanceof Element)
  54. {
  55. if (TOOLS_BUTTON_ATTRIBUTES.some(([name, value]) => (node.getAttribute(name) === value)))
  56. {
  57. handleToolsButton(node);
  58. }
  59. else
  60. {
  61. const button = node.querySelector(TOOLS_BUTTON_SELECTOR);
  62. if (button)
  63. {
  64. handleToolsButton(button);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. });
  71.  
  72. observer.observe(document.body, { subtree: true, childList: true });
  73. const timeout = setTimeout(() =>
  74. {
  75. console.debug("“Tools” button still not exist, aborting...");
  76. observer.disconnect();
  77. }, 120000);
  78.  
  79. const button = document.querySelector(TOOLS_BUTTON_SELECTOR);
  80. if (button)
  81. {
  82. handleToolsButton(button);
  83. }
  84. else
  85. {
  86. console.debug("“Tools” button not exist, attempting the MutationObserver method...");
  87. }