Auto enable grounding and context in Aistudio

Auto-enable "Grounding with Google Search" and "Browse the URL context" once on page load

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Auto enable grounding and context in Aistudio
// @namespace    Violentmonkey Scripts
// @version      1.1
// @description  Auto-enable "Grounding with Google Search" and "Browse the URL context" once on page load
// @author       Bui Quoc Dung
// @match        https://aistudio.google.com/*
// @grant        none
// @run-at       document-idle

// ==/UserScript==

(function () {
  'use strict';

  const selectors = [
    'button[aria-label="Grounding with Google Search"]',
    'button[aria-label="Browse the url context"]'
  ];

  function waitForElement(selector, timeout = 10000) {
    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();
        }
      }, interval);
    });
  }

  async function enableSwitch(selector) {
    try {
      const button = await waitForElement(selector);
      if (button.getAttribute('aria-checked') === 'false') {
        button.click();
      }
    } catch (err) {
      console.warn('Could not find:', selector);
    }
  }

  selectors.forEach(enableSwitch);
})();