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.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==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);
  })();
})();