Auto Close Google AI Studio Settings Panel

Close the Run settings panel after page aistudio load

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Auto Close Google AI Studio Settings Panel
// @namespace   Violentmonkey Scripts
// @match       https://aistudio.google.com/*
// @run-at      document-start
// @version     1.2
// @author      Bui Quoc Dung
// @description Close the Run settings panel after page aistudio load
// @license     MIT
// ==/UserScript==

(function () {
  'use strict';

  const DELAY_MS = 1000;
  const RETRY_TIMES = 2;
  const SELECTOR = 'button[aria-label="Close run settings panel"]';
  let lastHandledUrl = null;

  function closeSettingsPanelWithRetries() {
    if (lastHandledUrl === location.href) return;
    lastHandledUrl = location.href;

    for (let i = 0; i < RETRY_TIMES; i++) {
      setTimeout(() => {
        try {
          const button = document.querySelector(SELECTOR);
          if (button) {
            button.click();
          }
        } catch (e) {
        }
      }, DELAY_MS * (i + 1));
    }
  }

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

  (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', closeSettingsPanelWithRetries);
  })();
})();