Opens “Tools” Menu by Default on Google

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

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

  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.1
  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_SELECTOR = "[aria-controls=hdtbMenus], [jsname=I4bIT]";
  19.  
  20. function handleToolsButton(button)
  21. {
  22. const interval = setInterval(() =>
  23. {
  24. if (button.getAttribute("aria-expanded") !== "true")
  25. {
  26. button.click();
  27. }
  28. else
  29. {
  30. clearInterval(interval);
  31. }
  32. }, 250);
  33. }
  34.  
  35. const observer = new MutationObserver((records) =>
  36. {
  37. for (const record of records)
  38. {
  39. for (const node of record.addedNodes)
  40. {
  41. if (node instanceof Element)
  42. {
  43. if ((node.getAttribute("aria-controls") === "hdtbMenus") || (node.getAttribute("jsname") === "I4bIT"))
  44. {
  45. observer.disconnect();
  46. handleToolsButton(node);
  47. }
  48. else
  49. {
  50. const button = node.querySelector(TOOLS_BUTTON_SELECTOR);
  51. if (button)
  52. {
  53. observer.disconnect();
  54. handleToolsButton(button);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. });
  61.  
  62. observer.observe(document.body, { subtree: true, childList: true });
  63.  
  64. const button = document.querySelector(TOOLS_BUTTON_SELECTOR);
  65. if (button) { handleToolsButton(button); }