Auto-Expand Google Search Tools [pure javascript]

Show the Search Tools on Google search results instead of result-count and query-speed.

目前为 2024-12-01 提交的版本。查看 最新版本

// ==UserScript==
// @name        Auto-Expand Google Search Tools [pure javascript]
// @description Show the Search Tools on Google search results instead of result-count and query-speed.
// @namespace   gsAutoExp
// @license     MIT
// @version     1.5
// @match       https://www.google.com/*
// @icon        https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant       GM_addStyle
// ==/UserScript==

/* Inspired by GollyJer's Auto-Expand Google Search Tools v 1.4
   https://gf.qytechs.cn/en/scripts/13968-gollyjer-s-auto-expand-google-search-tools  */

/* globals $, waitForKeyElements */

// Hide the Search Tools button.
GM_addStyle('#hdtb-tls { display: none !important; }');

// Speed up visibility of the Seach Tools menu by removing the animation.
GM_addStyle('#hdtbMenus { transition: none !important; }');

// Show the Search Tools menu.
waitForKeyElements('#hdtb-tls', clickUntilItSticks);

function clickUntilItSticks(element) {
  var searchToolbar = document.getElementById('hdtbMenus'); // Use getElementById to select the element
  console.log('searchToolbar', searchToolbar);
  var sanityCount = 1;

  // Use setInterval to repeatedly check the condition
  var menusVisiblePoller = setInterval(function () {
    // Check whether the element is still invisible and increment sanityCount
    if (sanityCount < 20 && searchToolbar.offsetWidth === 0 && searchToolbar.offsetHeight === 0) {
      element.click(); // Trigger the click
      sanityCount++; // Increment the sanity counter
    } else {
      clearInterval(menusVisiblePoller); // Stop polling once the condition is met or the sanity limit is reached
    }
  }, 88);
}

/* Credit to https://github.com/CoeJoder/waitForKeyElements.js v1.3 */

function waitForKeyElements(selectorOrFunction, callback, waitOnce, interval, maxIntervals) {
    if (typeof waitOnce === "undefined") {
        waitOnce = true;
    }
    if (typeof interval === "undefined") {
        interval = 300;
    }
    if (typeof maxIntervals === "undefined") {
        maxIntervals = -1;
    }
    if (typeof waitForKeyElements.namespace === "undefined") {
        waitForKeyElements.namespace = Date.now().toString();
    }
    var targetNodes = (typeof selectorOrFunction === "function")
            ? selectorOrFunction()
            : document.querySelectorAll(selectorOrFunction);

    var targetsFound = targetNodes && targetNodes.length > 0;
    if (targetsFound) {
        targetNodes.forEach(function(targetNode) {
            var attrAlreadyFound = `data-userscript-${waitForKeyElements.namespace}-alreadyFound`;
            var alreadyFound = targetNode.getAttribute(attrAlreadyFound) || false;
            if (!alreadyFound) {
                var cancelFound = callback(targetNode);
                if (cancelFound) {
                    targetsFound = false;
                }
                else {
                    targetNode.setAttribute(attrAlreadyFound, true);
                }
            }
        });
    }

    if (maxIntervals !== 0 && !(targetsFound && waitOnce)) {
        maxIntervals -= 1;
        setTimeout(function() {
            waitForKeyElements(selectorOrFunction, callback, waitOnce, interval, maxIntervals);
        }, interval);
    }
}


QingJ © 2025

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