Auto Close Panel & Enable Grounding/Context in Google AI Studio

Automatically enables "Grounding with Google Search" and "Browse the URL context" and closes the Run settings panel after the aistudio page loads.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name        Auto Close Panel & Enable Grounding/Context in Google AI Studio
// @namespace   Violentmonkey Scripts
// @match       https://aistudio.google.com/*
// @run-at      document-start
// @version     1.0
// @author      Bui Quoc Dung
// @description Automatically enables "Grounding with Google Search" and "Browse the URL context" and closes the Run settings panel after the aistudio page loads.
// @license     MIT
// @grant       none
// ==/UserScript==

(function () {
  'use strict';

  const DELAY_MS_CLOSE_PANEL = 1000;
  const RETRY_TIMES_CLOSE_PANEL = 2;
  const SELECTOR_CLOSE_BUTTON = 'button[aria-label="Close run settings panel"]';
  let lastHandledUrlForPanel = null;
    const SELECTORS_ENABLE_BUTTONS = [
    'button[aria-label="Grounding with Google Search"]',
    'button[aria-label="Browse the url context"]'
  ];
  let lastHandledUrlForEnable = null;
  function closeSettingsPanelWithRetries() {
    if (lastHandledUrlForPanel === location.href) return;
    lastHandledUrlForPanel = location.href;

    for (let i = 0; i < RETRY_TIMES_CLOSE_PANEL; i++) {
      setTimeout(() => {
        try {
          const button = document.querySelector(SELECTOR_CLOSE_BUTTON);
          if (button) {
            button.click();
          }
        } catch (e) {
          console.error('Error trying to close settings panel:', e);
        }
      }, DELAY_MS_CLOSE_PANEL * (i + 1));
    }
  }

  function waitForElement(selector, timeout = 1000) {
    return new Promise((resolve, reject) => {
      const interval = 100;
      let elapsed = 0;
      const timer = setInterval(() => {
        const el = document.querySelector(selector);
        if (el) {
          clearInterval(timer);
          resolve(el);
        } else if ((elapsed += interval) >= timeout) {
          clearInterval(timer);
          reject(new Error(`Element not found for selector: ${selector}`));
        }
      }, interval);
    });
  }

  async function enableSwitches() {
      if (lastHandledUrlForEnable === location.href) return;
      lastHandledUrlForEnable = location.href;

    for (const selector of SELECTORS_ENABLE_BUTTONS) {
      try {
        const button = await waitForElement(selector, 2000);
        if (button.getAttribute('aria-checked') === 'false') {
          button.click();
        }
      } catch (err) {
      }
    }
  }

  function runAllTasks() {
      setTimeout(() => {
        enableSwitches();
        closeSettingsPanelWithRetries();
      }, 1000);
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', runAllTasks);
  } else {
    runAllTasks();
  }

  (function patchHistory() {
    const _push = history.pushState;
    const _replace = history.replaceState;

    history.pushState = function (...args) {
      const res = _push.apply(this, args);
      window.dispatchEvent(new Event('locationchange'));
      return res;
    };

    history.replaceState = function (...args) {
      const res = _replace.apply(this, args);
      window.dispatchEvent(new Event('locationchange'));
      return res;
    };

    window.addEventListener('popstate', () =>
      window.dispatchEvent(new Event('locationchange'))
    );
    window.addEventListener('locationchange', runAllTasks);
  })();
})();