Auto-Expand Google Search Tools [pure javascript]

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

Versión del día 1/12/2024. Echa un vistazo a la versión más reciente.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==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://greasyfork.org/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);
    }
}