AI Everywhere

Highly customizable mini A.I. floating menu that can define words, answer questions, translate, and much more in a single click and with your custom prompts. Includes useful click to search on Google and copy selected text buttons, along with Rocker+Mouse Gestures and Units+Currency Converters, all features can be easily modified or disabled.

Install this script?
Author's suggested script

You may also like Precise Time Converter on Google.

Install this script
// ==UserScript==
// @name               AI Everywhere
// @namespace          OperaBrowserGestures
// @description        Highly customizable mini A.I. floating menu that can define words, answer questions, translate, and much more in a single click and with your custom prompts. Includes useful click to search on Google and copy selected text buttons, along with Rocker+Mouse Gestures and Units+Currency Converters, all features can be easily modified or disabled.
// @version            68
// @author             hacker09
// @include            *
// @exclude            https://accounts.google.com/v3/signin/*
// @icon               https://i.imgur.com/8iw8GOm.png
// @grant              GM_registerMenuCommand
// @grant              GM_getResourceText
// @grant              GM.xmlHttpRequest
// @grant              GM_deleteValue
// @grant              GM_openInTab
// @grant              window.close
// @run-at             document-end
// @grant              GM_setValue
// @grant              GM_getValue
// @connect            google.com
// @connect            generativelanguage.googleapis.com
// @resource           AICSS https://hacker09.glitch.me/AICSS.css
// @require            https://update.greasyfork.org/scripts/506699/1440902/marked.js
// ==/UserScript==

/* jshint esversion: 11 */

if (GM_getResourceText('AICSS') === '') {
  alert('Failed to load the .css file resource!\n\nPlease contact your network admin to have the https://glitch.me/ domain unblocked.\n\n');
  return; //Stop running
}

const BypassTT = window.trustedTypes?.createPolicy('BypassTT', { createHTML: HTML => HTML }); //Bypass trustedTypes

if (GM_getValue("APIKey") === undefined || GM_getValue("APIKey") === null || GM_getValue("APIKey") === '') { //Set up the API Key
  window.onload = function() {
    if (location.href === 'https://aistudio.google.com/app/apikey' && document.querySelector(".apikey-link") !== null) {
      setTimeout(function() {
        document.querySelectorAll(".apikey-link")[1].click(); //Click on the API Key
        setTimeout(function() {
          GM_setValue("APIKey", document.querySelector(".apikey-text").innerText); //Store the API Key
          (GM_getValue("APIKey") !== undefined && GM_getValue("APIKey") !== null && GM_getValue("APIKey") !== '') ? alert('API Key automatically added!') : alert('Failed to automatically add API Key!');
        }, 500);
      }, 500);
    }
  };
}

// Mouse Gestures _________________________________________________________________________________________________________________________________________________________
GM_registerMenuCommand("Enable/Disable Mouse Gestures", MouseGestures);
if (GM_getValue("MouseGestures") !== true && GM_getValue("MouseGestures") !== false) {
  GM_setValue("MouseGestures", true);
}

function MouseGestures() //Enable/disable MouseGestures
{
  if (GM_getValue("MouseGestures") === true) {
    GM_setValue("MouseGestures", false);
  }
  else {
    GM_setValue("MouseGestures", true);
    location.reload();
  }
}

if (GM_getValue("MouseGestures") === true) //If the MouseGestures is enabled
{
  const SENSITIVITY = 3;
  const TOLERANCE = 3;

  const funcs = { //Store the MouseGestures functions

    'L': function() { //Detect the Left movement
      window.history.back();
    },

    'R': function() { //Detect the Right movement
      window.history.forward();
    },

    'D': function() { //Detect the Down movement
      if (IsShiftNotPressed === true) { //If the shift key isn't being pressed
        GM_openInTab(link, {
          active: true,
          insert: true,
          setParent: true
        });
      }
    },

    'UD': function() { //Detect the Up+Down movement
      location.reload();
    },

    'DR': function(e) { //Detect the Down+Right movement
      top.close();
      e.preventDefault();
      e.stopPropagation();
    },

    'DU': function() { //Detect the Down+Up movement
      GM_openInTab(link, {
        active: false,
        insert: true,
        setParent: true
      });
    }

  };

  //Math codes to track the mouse movement gestures
  const s = 1 << ((7 - SENSITIVITY) << 1);
  const t1 = Math.tan(0.15708 * TOLERANCE),t2 = 1 / t1;

  let x, y, path;

  const tracer = function(e) { //Start the const tracer
    let cx = e.clientX, cy = e.clientY, deltaX = cx - x, deltaY = cy - y, distance = deltaX * deltaX + deltaY * deltaY;
    if (distance > s) {
      let slope = Math.abs(deltaY / deltaX), direction = '';
      if (slope > t1) {
        direction = deltaY > 0 ? 'D' : 'U';
      } else if (slope <= t2) {
        direction = deltaX > 0 ? 'R' : 'L';
      }
      if (path.charAt(path.length - 1) !== direction) {
        path += direction;
      }
      x = cx;
      y = cy;
    }
  };

  window.addEventListener('mousedown', function(e) {
    if (e.which === 3) {
      x = e.clientX;
      y = e.clientY;
      path = "";
      window.addEventListener('mousemove', tracer, false); //Detect the mouse position
    }
  }, false);

  var IsShiftNotPressed = true; //Hold the shift key status
  window.addEventListener("contextmenu", function(e) { //When the shift key is/isn't pressed
    if (e.shiftKey) {
      IsShiftNotPressed = false;
      open(link, '_blank', 'height=' + screen.height + ',width=' + screen.width);
    }
    if (LeftClicked === true) { //If the Left Click was released when the Rocker Mouse Gestures were enabled
      e.preventDefault();
      e.stopPropagation();
    }
    setTimeout(function() {
      IsShiftNotPressed = true;
    }, 500);
  }, false);

  window.addEventListener('contextmenu', function(e) { //When the right click BTN is released
    window.removeEventListener('mousemove', tracer, false); //Track the mouse movements
    if (path !== "") {
      e.preventDefault();
      if (funcs.hasOwnProperty(path)) {
        funcs[path]();
      }
    }
  }, false);

  var link;
  Array.from(document.querySelectorAll('a')).forEach(Element => Element.onmouseover = function() {
    link = this.href; //Store the hovered link to a variable
  });

  Array.from(document.querySelectorAll('a')).forEach(Element => Element.onmouseout = function() {
    const PreviousLink = link; //Save the hovered link to a variable
    setTimeout(function() {
      if (PreviousLink === link) //If the hovered link is still the same as the previously hovered Link
      {
        link = 'about:newtab'; //Make the script open a new browser tab when the mouse leaves any link that was hovered
      }
    }, 200);
  });
}

//Rocker Mouse Gesture Settings _________________________________________________________________________________________________________________________________________________________
GM_registerMenuCommand("Enable/Disable Rocker Mouse Gestures", RockerMouseGestures);
if (GM_getValue("RockerMouseGestures") !== true && GM_getValue("RockerMouseGestures") !== false) { //Set up the RockerMouseGestures
  GM_setValue("RockerMouseGestures", false);
}

function RockerMouseGestures() //Enable/disable RockerMouseGestures
{
  if (GM_getValue("RockerMouseGestures") === true) {
    GM_setValue("RockerMouseGestures", false);
  }
  else {
    GM_setValue("RockerMouseGestures", true);
    location.reload();
  }
}

if (GM_getValue("RockerMouseGestures") === true || GM_getValue("SearchHiLight") === true) //If the RockerMouseGestures or the SearchHiLight is enabled
{
  var LeftClicked, RightClicked;
  window.addEventListener("mousedown", function(e) { //Track which side of the mouse was the first one to be pressed
    switch (e.button) {
      case 0:
        LeftClicked = true;
        break;
      case 2:
        RightClicked = true;
        break;
    }
  }, false);

  window.addEventListener("mouseup", function(e) { //Track which side of the mouse was the last one to be released
    switch (e.button) {
      case 0:
        LeftClicked = false;
        break;
      case 2:
        RightClicked = false;
        break;
    }
    if (LeftClicked && RightClicked === false) { //If Left was Clicked and then Right Click was released
      history.back(); //Go Back
    }
    if (RightClicked && LeftClicked === false) { //If Right was Clicked and then Left Click was released
      history.forward(); //Go Forward
    }
  }, false);
}

//SearchHighLight + CurrenciesConverter + UnitsConverter _______________________________________________________________________________________________________________________________________
GM_registerMenuCommand("Enable/Disable SearchHiLight", SearchHiLight);
if (GM_getValue("SearchHiLight") !== true && GM_getValue("SearchHiLight") !== false) { //Set up the SearchHiLight
  GM_setValue("SearchHiLight", true);
}

if (GM_getValue("CurrenciesConverter") !== true && GM_getValue("CurrenciesConverter") !== false) {
  GM_setValue("CurrenciesConverter", true);
}

if (GM_getValue("UnitsConverter") !== true && GM_getValue("UnitsConverter") !== false) {
  GM_setValue("UnitsConverter", true);
}

function SearchHiLight() //Enable/disable the SearchHiLight and the Currency/Unit converters
{
  if (GM_getValue("SearchHiLight") === true) {
    GM_setValue("SearchHiLight", false);
    GM_setValue("CurrenciesConverter", false);
    GM_deleteValue('YourLocalCurrency');
    GM_setValue("UnitsConverter", false);
  }
  else {
    GM_setValue("SearchHiLight", true);

    if (confirm('If you want to enable the Currency Converter press OK.'))
    {
      GM_setValue("CurrenciesConverter", true);
    }
    else
    {
      GM_setValue("CurrenciesConverter", false);
    }

    if (confirm('If you want to enable the Units Converter press OK.'))
    {
      GM_setValue("UnitsConverter", true);
    }
    else
    {
      GM_setValue("UnitsConverter", false);
    }
    location.reload();
  }
}

if (GM_getValue("SearchHiLight") === true) //If the SearchHiLight is enabled
{
  var SelectedTextIsLink, FinalCurrency, SelectedText, SelectedTextSearch = '';
  const Links = new RegExp(/\.org|\.ly|\.net|\.co|\.tv|\.me|\.biz|\.club|\.site|\.br|\.gov|\.io|\.jp|\.edu|\.au|\.in|\.it|\.ca|\.mx|\.fr|\.tw|\.il|\.uk|\.zoom\.us|\youtu.be/i);

  window.addEventListener('load', function() { //Start the script after the page loads
    document.body.addEventListener('mouseup', function() { //When the user releases the mouse click after selecting something
      SelectedText = getSelection().toString(); //Store the selected text
      SelectedTextSearch = getSelection().toString().replaceAll('&', '%26'); //Store the selected text to be opened on Google
      const CurrencySymbols = new RegExp(/\$|R\$|HK\$|US\$|\$US|¥|€|Rp|kn|Kč|kr|zł|£|฿|₩/i);
      const Currencies = new RegExp(/^[ \t\xA0]*(?=.*?(\d+(?:.\d+)?))(?=(?:\1[ \t\xA0]*)?(Dólares|dolares|dólares|dollars|AUD|BGN|BRL|BCH|BTC|BYN|CAD|CHF|CNY|CZK|DKK|EUR|EGP|ETH|GBP|GEL|HKD|HRK|HUF|IDR|ILS|INR|JPY|LTC|KRW|MXN|MYR|NOK|NZD|PHP|PLN|RON|RM|RUB|SEK|SGD|THB|TRY|USD|UAH|ZAR|KZT|YTL|\$|R\$|HK\$|US\$|\$US|¥|€|Rp|kn|Kč|kr|zł|£|฿|₩))(?:\1[ \t\xA0]*\2|\2[ \t\xA0]*\1)[ \t\xA0]*$/i);

      function ShowConvertion(UnitORCurrency, Type, Result) {
        shadowRoot.querySelector("#SearchBTN span")?.remove(); //Return previous HTML
        shadowRoot.querySelector("#SearchBTN").innerHTML = (html => BypassTT?.createHTML(html) || html)('<span class="GreyBar">│ </span>' + shadowRoot.querySelector("#SearchBTN").innerHTML);

        if (UnitORCurrency === 'Currencies' && SelectedText.match(Currencies)[2].match(CurrencySymbols) !== null) { //If the selected currency contains a symbol
          shadowRoot.querySelector("#ShowCurrencyORUnits").innerHTML = (html => BypassTT?.createHTML(html) || html)(Type + ' 🠂 ' + Intl.NumberFormat(navigator.language, {
            style: 'currency',
            currency: GM_getValue("YourLocalCurrency")
          }).format(Result)); //Show the FinalCurrency
        }
        if (UnitORCurrency === 'Currencies' && SelectedText.match(Currencies)[2].match(CurrencySymbols) === null) { //If the selected currency contains no symbol
          shadowRoot.querySelector("#ShowCurrencyORUnits").innerHTML = (html => BypassTT?.createHTML(html) || html)(Intl.NumberFormat(navigator.language, {
            style: 'currency',
            currency: GM_getValue("YourLocalCurrency")
          }).format(Result)); //Show the FinalCurrency
        }

        UnitORCurrency === 'Units' ? shadowRoot.querySelector("#ShowCurrencyORUnits").innerHTML = (html => BypassTT?.createHTML(html) || html)(Result + ' ' + Type) : ''; //Show the converted unit results

        var htmlcode = shadowRoot.querySelector("#ShowCurrencyORUnits").innerHTML; //Save the converted unit/currency value
        setTimeout(() => { //Wait for Units to show up to get the right offsetWidth
          var offsetWidth = shadowRoot.querySelector("#ShowCurrencyORUnits").offsetWidth; //Store the current menu size
          shadowRoot.querySelector("#ShowCurrencyORUnits").onmouseover = function() { //When the mouse hovers the unit/currency
            shadowRoot.querySelector("#ShowCurrencyORUnits").innerHTML = (html => BypassTT?.createHTML(html) || html)(`Copy`);
            shadowRoot.querySelector("#ShowCurrencyORUnits").style.display = 'inline-flex';
            shadowRoot.querySelector("#ShowCurrencyORUnits").style.width = `${offsetWidth}px`; //Maintain the aspect ratio
          };
        }, 0);

        shadowRoot.querySelector("#ShowCurrencyORUnits").onmouseout = function() { //When the mouse leaves the unit/currency
          shadowRoot.querySelector("#ShowCurrencyORUnits").style.width = ''; //Return the original aspect ratio
          shadowRoot.querySelector("#ShowCurrencyORUnits").style.display = ''; //Return the original aspect ratio
          shadowRoot.querySelector("#ShowCurrencyORUnits").innerHTML = (html => BypassTT?.createHTML(html) || html)(htmlcode); //Return the previous html
        };

        shadowRoot.querySelector("#ShowCurrencyORUnits").onclick = function() { //When the unit/currency is clicked
          UnitORCurrency === 'Units' ? navigator.clipboard.writeText(`${Result} ${Type}`) : navigator.clipboard.writeText(Intl.NumberFormat(navigator.language, { style: 'currency', currency: GM_getValue("YourLocalCurrency") }).format(Result));
        };
      }

      //CurrenciesConverter _______________________________________________________________________________________________________________________________________
      if (GM_getValue("CurrenciesConverter") === true) { //If Currencies Converter is enabled
        shadowRoot.querySelector("#ShowCurrencyORUnits").innerText = ''; //Remove the previous Currency text

        if (SelectedText.match(Currencies) !== null) //If the selected text is a currency
        {
          if (GM_getValue("YourLocalCurrency") === undefined) {
            const UserInput = prompt('Write your local currency.\nThe script will always use your local currency to make exchange-rate conversions.\n\n*Currency input examples:\nBRL\nCAD\nUSD\netc...\n\n*Press OK');
            GM_setValue("YourLocalCurrency", UserInput);
          }

          (async () => { //Get the final converted currency value
            const currencyMap = { '$': 'USD', 'us$': 'USD', '$us': 'USD', 'r$': 'BRL', 'hk$': 'HKD', '¥': 'JPY', '€': 'EUR', 'rp': 'IDR', 'kn': 'HRK', 'kč': 'CZK', 'kr': 'DKK', 'zł': 'PLN', '£': 'GBP', '฿': 'THB', '₩': 'KRW' };
            const CurrencySymbol = currencyMap[SelectedText.match(CurrencySymbols)?.[0].toLowerCase()] || SelectedText.match(Currencies)[2]; //Store the currency symbol

            GM.xmlHttpRequest({ //Get the final converted currency value
              method: "GET",
              url: `https://www.google.com/search?q=${SelectedText.match(Currencies)[1]} ${CurrencySymbol} in ${GM_getValue("YourLocalCurrency")}`,
              onload: (response) => {
                const newDocument = new DOMParser().parseFromString(response.responseText, 'text/html'); //Parse the fetch response
                const FinalCurrency = parseFloat(newDocument.querySelector(".SwHCTb").innerText.split(' ')[0].replaceAll(',', '')); //Store the FinalCurrency and erase all commas
                ShowConvertion('Currencies', CurrencySymbol, FinalCurrency);
              }
            });
          })();
        }
      }

      //UnitsConverter _________________________________________________________________________________________________________________________________________________________________________
      if (GM_getValue("UnitsConverter") === true) { //If the Units Converter option is enabled
        shadowRoot.querySelector("#ShowCurrencyORUnits").innerText = ''; //Remove the previous Units text
        const Units = new RegExp(/^[ \t\xA0]*(-?\d+(?:[., ]\d+)?)(?:[ \t\xA0]*x[ \t\xA0]*(-?\d+(?:[., ]\d+)?))?[ \t\xA0]*(in|inch|inches|cm|cms|centimeters?|mt|mts|meters?|ft|kg|lbs?|pounds?|kilograms?|ounces?|g|ozs?|fl oz|fl oz (us)|fluid ounces?|kphs?|km\/h|kilometers per hours?|mphs?|meters per hours?|°?º?[CF]|km\/hs?|ml|milliliters?|l|liters?|litres?|gal|gallons?|yards?|yd|Millimeter|millimetre|kilometers?|mi|mm|miles?|km|ft|fl|feets?|grams?|kilowatts?|kws?|brake horsepower|mechanical horsepower|hps?|bhps?|miles per gallons?|mpgs?|liters per 100 kilometers?|l\/100km|liquid quarts?|lqs?|foot-?pounds?|ft-?lbs?|lb fts?|newton-?meters?|nm|\^\d+)[ \t\xA0]*(?:\(\w+\)[ \t\xA0]*)?$/i);

        if (SelectedText.match(Units) !== null) //If the selected text is an unit
        {
          const conversionMap = {};

          function addConversion(keys, unit, factor, convert) { //Helper function to add multiple keys with the same value
            keys.forEach(key => {
              conversionMap[key] = { unit, factor, convert };
            });
          }

          addConversion(['inch', 'inches', 'in', '"', '”'], 'cm', 2.54);
          addConversion(['centimeter', 'centimeters', 'cm', 'cms'], 'in', 1 / 2.54);
          addConversion(['meter', 'meters', 'mt', 'mts'], 'ft', 3.281);
          addConversion(['kilogram', 'kilograms', 'kg'], 'lb', 2.205);
          addConversion(['pound', 'pounds', 'lb', 'lbs'], 'kg', 1 / 2.205);
          addConversion(['ounce', 'ounces', 'oz', 'ozs'], 'g', 28.35);
          addConversion(['gram', 'grams', 'g'], 'oz', 1 / 28.35);
          addConversion(['kilometer', 'kilometers', 'km'], 'mi', 1 / 1.609);
          addConversion(['kph', 'kphs', 'km/h', 'km/hs', 'kilometers per hour', 'kilometers per hours'], 'mph', 0.621371);
          addConversion(['mph', 'mphs', 'meters per hour', 'meters per hours'], 'km/h', 1 / 1.000);
          addConversion(['mi', 'mile', 'miles'], 'km', 1.609);
          addConversion(['°c', '°f', 'ºc', 'ºf'], '°F', v => (v * 9 / 5) + 32);
          addConversion(['°f', 'ºf'], '°C', v => (v - 32) * 5 / 9);
          addConversion(['milliliter', 'milliliters', 'ml'], 'fl oz (US)', 1 / 29.574);
          addConversion(['fl oz (US)', 'fl oz', 'fl', 'fluid ounce', 'fluid ounces'], 'ml', 29.574);
          addConversion(['litre', 'liter', 'litres', 'liters', 'l'], 'gal (US)', 1 / 3.785);
          addConversion(['gal', 'gallon', 'gallons'], 'lt', 3.785);
          addConversion(['yard', 'yards', 'yd'], 'm', 1 / 1.094);
          addConversion(['millimetre', 'millimeters', 'millimetres', 'mm'], 'in', 1 / 25.4);
          addConversion(['feet', 'feets', 'ft'], 'mt', 0.3048);
          addConversion(['kilowatt', 'kilowatts', 'kw', 'kws'], 'mhp', 1.341);
          addConversion(['mhp', 'mhps', 'hp', 'hps', 'brake horsepower', 'mechanical horsepower'], 'kw', 1 / 1.341);
          addConversion(['mpg', 'mpgs', 'miles per gallon', 'miles per gallons'], 'l/100km', v => 235.215 / v);
          addConversion(['l/100km', 'liters per 100 kilometer', 'liters per 100 kilometers'], 'US mpg', v => 235.215 / v);
          addConversion(['lq', 'lqs', 'liquid quart', 'liquid quarts'], 'l', 1 / 1.057);
          addConversion(['foot-pound', 'foot-pounds', 'foot pound', 'foot pounds', 'ft-lbs', 'ft-lb', 'ft lbs', 'ft lb', 'lb ft', 'lb-ft'], 'Nm', 1.3558179483);
          addConversion(['nm', 'newton-meter', 'newton-meters', 'newton meter', 'newton meters'], 'ft lb', 1 / 1.3558179483);

          const SelectedUnitValue = SelectedText.match(Units)[1].replaceAll(',', '.');
          const SecondSelectedUnitValue = SelectedText.match(Units)[2]?.replaceAll(',', '.') || 0;
          const selectedUnitType = SelectedText.match(Units)[3].toLowerCase();

          const convertValue = (value, unitType) => {
            const { factor, convert } = conversionMap[unitType] || {};
            return convert ? convert(value) : value * factor;
          };

          var NewUnit = conversionMap[selectedUnitType]?.unit || selectedUnitType;
          var ConvertedUnit = SecondSelectedUnitValue != 0 ? `${convertValue(parseFloat(SelectedUnitValue), selectedUnitType).toFixed(2)} x ${convertValue(parseFloat(SecondSelectedUnitValue), selectedUnitType).toFixed(2)}` : convertValue(parseFloat(SelectedUnitValue), selectedUnitType).toFixed(2);
          ConvertedUnit = SelectedText.match(/\^(\d+\.?\d*)/) ? (NewUnit = 'power', Math.pow(parseFloat(SelectedUnitValue), parseFloat(SelectedText.match(/\^(\d+\.?\d*)/)[1]))) : ConvertedUnit;
          ShowConvertion('Units', NewUnit, ConvertedUnit);
        }
      }

      //Menu ___________________________________________________________________________________________________________________________________________________________________________
      if (shadowRoot.querySelector("#SearchBTN").innerText === 'Open') //If the Search BTN text is 'Open'
      {
        shadowRoot.querySelector("#highlight_menu > ul").style.paddingInlineStart = '19px'; //Increase the menu size
        shadowRoot.querySelector("#SearchBTN").innerText = 'Search'; //Display the BTN text as Search again
        shadowRoot.querySelector("#OpenAfter").remove(); //Remove the custom Open white hover overlay
        SelectedTextIsLink = false; //Make common words searchable again
      }

      if (SelectedText.match(Links) !== null) //If the selected text is a link
      {
        SelectedTextIsLink = true;
        shadowRoot.querySelector("#highlight_menu > ul").style.paddingInlineStart = '27px'; //Increase the menu size
        shadowRoot.querySelector("#SearchBTN").innerText = 'Open'; //Change the BTN text to Open
        shadowRoot.innerHTML += (html => BypassTT?.createHTML(html) || html)(` <style id="OpenAfter"> #SearchBTN::after { width: 225% !important; height: 221% !important; transform: translate(-48%, -71%) !important; } </style> `); //Add a custom Open white hover overlay
      }

      shadowRoot.querySelector("#SearchBTN").onmousedown = function() {
        var LinkfyOrSearch = 'https://www.google.com/search?q=';
        if (SelectedTextIsLink === true)
        {
          LinkfyOrSearch = 'https://'; //Make the non-HTTP and non-HTTPS links able to be opened
        }
        if (SelectedText.match(/http:|https:/) !== null) //If the selected text is a link that already has HTTP or HTTPS
        {
          LinkfyOrSearch = ''; //Remove the https:// that was previously added to this variable
        }

        GM_openInTab(LinkfyOrSearch + SelectedTextSearch, { //Open google and search for the selected text
          active: true,
          setParent: true,
          loadInBackground: true
        });
        getSelection().removeAllRanges(); //UnSelect the selected text after the search BTN is clicked so that if the user clicks on the past selected text the menu won't show up again.
        shadowRoot.querySelector("#highlight_menu").classList.remove('show'); //Hide the menu
      };

      const menu = shadowRoot.querySelector("#highlight_menu");
      if (document.getSelection().toString().trim() !== '') { //If text has been selected
        const p = document.getSelection().getRangeAt(0).getBoundingClientRect(); //Store the selected position

        menu.classList.add('show'); //Show the menu
        menu.offsetHeight; //Trigger reflow by forcing a style calculation
        menu.style.left = p.left + (p.width / 2) - (menu.offsetWidth / 2) + 'px';
        menu.style.top = p.top - menu.offsetHeight - 10 + 'px';
        menu.classList.add('highlight_menu_animate');

        return; //Keep the menu open
      }
      menu.classList.remove('show'); //Hide the menu
      shadowRoot.querySelector("#SearchBTN span")?.remove(); //Return previous HTML
    });
  });

  //AI Menu ___________________________________________________________________________________________________________________________________________________________________________
  var audio, Generating, isRecognizing = false;
  const HtmlMenu = document.createElement('div'); //Create a container div
  HtmlMenu.setAttribute('style', `width: 0px; height: 0px; display: block;`); //Hide the container div by default
  const shadowRoot = HtmlMenu.attachShadow({ mode: 'closed' });
  const BGColor = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'rgb(37, 36, 53)' : '#e7edf1'; //Change AI theme according to the browser theme
  const IMGsColor = BGColor === '#e7edf1' ? 'filter: invert(1)' : ''; //If on white mode invert black svg colors to white
  const TextColor = BGColor === '#e7edf1' ? 'black' : 'white'; //Depending on the browser theme change the AI menu text color
  const UniqueLangs = navigator.languages.filter((l, i, arr) => !arr.slice(0, i).some(e => e.split('-')[0].toLowerCase() === l.split('-')[0].toLowerCase()) ); //Filter unique languages
  const Lang = UniqueLangs.length > 1 ? `${UniqueLangs[0]} and into ${UniqueLangs[1]}` : UniqueLangs[0]; //Use 1 or 2 languages
  const GeminiSVG = '<svg viewBox="0 0 32 32" fill="none"> <path d="M14 28C14 26.0633 13.6267 24.2433 12.88 22.54C12.1567 20.8367 11.165 19.355 9.905 18.095C8.645 16.835 7.16333 15.8433 5.46 15.12C3.75667 14.3733 1.93667 14 0 14C1.93667 14 3.75667 13.6383 5.46 12.915C7.16333 12.1683 8.645 11.165 9.905 9.905C11.165 8.645 12.1567 7.16333 12.88 5.46C13.6267 3.75667 14 1.93667 14 0C14 1.93667 14.3617 3.75667 15.085 5.46C15.8317 7.16333 16.835 8.645 18.095 9.905C19.355 11.165 20.8367 12.1683 22.54 12.915C24.2433 13.6383 26.0633 14 28 14C26.0633 14 24.2433 14.3733 22.54 15.12C20.8367 15.8433 19.355 16.835 18.095 18.095C16.835 19.355 15.8317 20.8367 15.085 22.54C14.3617 24.2433 14 26.0633 14 28Z" fill="url(#paint)"></path></svg>';

  shadowRoot.innerHTML = (html => BypassTT?.createHTML(html) || html)(`<svg width=" 0" height=" 0">
  <defs>
    <radialGradient cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(2.77876 11.3795) rotate(18.6832) scale(29.8025 238.737)" id="paint">
      <stop offset="0.0671246" stop-color="#9168C0"></stop>
      <stop offset="0.342551" stop-color="#5684D1"></stop>
      <stop offset="0.672076" stop-color="#1BA1E3"></stop>
    </radialGradient>
  </defs>
</svg>
<style>
${GM_getResourceText('AICSS')}

.animated-border {
  background: border-box border-box ${BGColor};
}

#prompt {
  color: ${TextColor};
}

#AIBox.AnswerBox {
  background: ${BGColor};
}
</style>
<div id="highlight_menu">
  <div class="AI-BG-box">
    <button id="AIBTN">
      <div class="MenuGemini">${GeminiSVG}Explore more</div>
    </button>
    <button id="AIBTN" class="translate">
      <div class="MenuGemini">${GeminiSVG}Translate</div>
    </button>
  </div>
  <ul id="MenuList">
    <li class="popuptext"></li>
    <li id="ShowCurrencyORUnits"></li>
    <li class="popuptext" id="SearchBTN">Search</li>
    <li class="popuptext" id="CopyBTN">
      <span>│</span> Copy
    </li>
  </ul>
</div>
<div class="animated-border" id="AIBox">
  <div id="tabcontext">
    <p>Page Context</p>
    <p id="TabBox">Tab</p>
  </div>
  <button id="dictate">
    <svg id="dictateSvg" viewBox="0 0 700 700">
      <defs>
        <path id="commonPath1" d="M439.5,236c0-11.3-9.1-20.4-20.4-20.4s-20.4,9.1-20.4,20.4c0,70-64,126.9-142.7,126.9s-142.7-56.9-142.7-126.9c0-11.3-9.1-20.4-20.4-20.4s-20.4,9.1-20.4,20.4c0,86.2,71.5,157.4,163.1,166.7v57.5h-23.6c-11.3,0-20.4,9.1-20.4,20.4s9.1,20.4,20.4,20.4h88c11.3,0,20.4-9.1,20.4-20.4s-9.1-20.4-20.4-20.4h-23.6v-57.5C368,393.4,439.5,322.2,439.5,236Z" fill="#fff"></path>
        <path id="commonPath2" d="M256,323.5c51,0,92.3-41.3,92.3-92.3v-127.9C348.3,52.3,307,11,256,11s-92.3,41.3-92.3,92.3v127.9c0,51,41.3,92.3,92.3,92.3ZM203.7,103.3C203.7,74.5,227.2,51,256,51s52.3,23.5,52.3,52.3v127.9c0,28.8-23.5,52.3-52.3,52.3s-52.3-23.5-52.3-52.3v-127.9Z" fill="#fff"></path>
        <ellipse id="commonEllipse" rx="53" ry="59" transform="translate(255.581 226.12)" fill="#0f0"></ellipse>
      </defs>
      <g class="state1">
        <use href="#commonPath1"></use>
        <use href="#commonPath2"></use>
      </g>
      <g class="state2">
        <use href="#commonPath1"></use>
        <use href="#commonPath2"></use>
        <use href="#commonEllipse"></use>
        <rect width="106" height="68.751" transform="translate(202.581 167.12)" fill="#0f0"></rect>
      </g>
      <g class="state3">
        <use href="#commonPath1"></use>
        <use href="#commonPath2"></use>
        <use href="#commonEllipse"></use>
        <ellipse rx="53" ry="59.21" transform="translate(255.581 226.457)" fill="#0f0"></ellipse>
        <rect width="106" height="136.9" transform="translate(202.581 89.492)" fill="#0f0"></rect>
        <ellipse rx="35" ry="40.072" transform="matrix(1.513 0 0 1 255.557 89.492)" fill="#0f0"></ellipse>
      </g>
    </svg>
  </button>
  <button id="TopPause">
    <svg width="12" height="12" viewBox="0 0 12 12" fill="none">
      <rect x="0.499756" y="0.5" width="11" height="11" rx="1.5" fill="white" stroke="white" />
    </svg>
  </button>
  <div class="BoxGemini" id="gemini">${GeminiSVG}</div>
  <div id="context">PAGE CONTEXT</div>
  <input class="Prompt" id="prompt" placeholder="Enter your prompt to Gemini">
</div>
<div id="AIBox" class="AnswerBox">
  <div id="AIAnswer"></div>
</div>
<div id="CloseOverlay"></div>`); //Set the menu html

  shadowRoot.querySelector("#AIBTN:first-of-type").classList.add('show-button'); //Animate the Explore BTN
  shadowRoot.querySelector("#AIBTN.translate").classList.add('show-button'); //Animate the Translate BTN

  shadowRoot.querySelector('#CopyBTN').onmousedown = function() {
    navigator.clipboard.writeText(getSelection().toString());
  };

  function Generate(Prompt, button) { //Call the AI endpoint
    const context = !!shadowRoot.querySelector("#context.show") ? `(You're not allowed to say anything like "Based on the provided text")\n"${Prompt} mainly base yourself on the text below\n${document.body.innerText}` : Prompt; //Add the page context if context is enabled
    const IsQuestion = Prompt.includes('?') ? 'Answer:' : 'Help me further explore a term or topic from the text/word:';
    const AIFunction = button.match('translate') ? `(You're not allowed to say anything like "The text is already in ${UniqueLangs[0]}"\nNo translation is needed).\Translate into ${Lang} the following text/word inside quotes "${Prompt}".\nAlso give me a definition and usage examples.` : button.match('Prompt') ? context : `(PS*I'm unable to provide you with more context, so don't ask for it! Also, don't mention that I haven't provided context or anything similar to it!) ${IsQuestion} "${Prompt}"`; //AI prompts
    const msg = button.match('translate') ? `Translate this text: "${Prompt.length > 215 ? Prompt.trim().slice(0, 215) + '…' : Prompt.trim()}"` : button.match('Prompt') ? Prompt.length > 240 ? Prompt.trim().slice(0, 240) + '…' : Prompt.trim() : `${IsQuestion} "${Prompt.length > 180 ? Prompt.trim().slice(0, 180) + '…' : Prompt.trim()}"`; //User text

    function startGeneratingText() {
      Generating = setInterval(function() { //Start the interval to change the text
        if (shadowRoot.querySelector("#finalanswer p").innerText === '▋ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ') {
          shadowRoot.querySelector("#finalanswer p").innerText = 'ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ';
        } else { //Toggle between showing and hiding ▋
          shadowRoot.querySelector("#finalanswer p").innerText = '▋ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ';
        }
      }, 200);
    }

    const request = GM.xmlHttpRequest({ //Call the AI API
      method: "POST",
      url: `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GM_getValue("APIKey")}`,
      headers: {
        "Content-Type": "application/json"
      },
      data: JSON.stringify({
        contents: [{
          parts: [{
            text: `${AIFunction}` //Use our AI prompt
          }]
        }],
        safetySettings: [ //Allow all content
          {
            category: "HARM_CATEGORY_HARASSMENT",
            threshold: "BLOCK_NONE"
          },
          {
            category: "HARM_CATEGORY_HATE_SPEECH",
            threshold: "BLOCK_NONE"
          },
          {
            category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
            threshold: "BLOCK_NONE"
          },
          {
            category: "HARM_CATEGORY_DANGEROUS_CONTENT",
            threshold: "BLOCK_NONE"
          }
        ],
      }),
      onerror: function(err) {
        clearInterval(Generating); //Stop showing ▋
        shadowRoot.querySelector("#finalanswer").innerHTML = (html => BypassTT?.createHTML(html) || html)(`<br>Please copy and paste the error below:<br><a class="feedback" href="https://greasyfork.org/scripts/419825/feedback">Click here to report this bug</a><br><br> Prompt: ${Prompt}<br> Button: ${button}<br> Error: ${err}}<br><br><br>`); //Show an error message
      },
      onload: function(response) {
        clearInterval(Generating); //Stop showing ▋
        const AIResponse = JSON.parse(response.responseText).candidates?.[0]?.content?.parts?.[0]?.text;

        if (AIResponse !== undefined) {
          shadowRoot.querySelector("#finalanswer").innerHTML = (html => BypassTT?.createHTML(html) || html)(marked.parse(AIResponse) + '<br>'); //Show the parsed AI response
        } else {
          shadowRoot.querySelector("#finalanswer").innerHTML = (html => BypassTT?.createHTML(html) || html)(`<br>Please copy and paste the error below:<br><a class="feedback" href="https://greasyfork.org/scripts/419825/feedback">Click here to report this bug</a><br><br> Prompt: ${Prompt}<br> Button: ${button}<br> Error: ${response.responseText}<br><br><br>`); //Show an error message
        }

        audio = new SpeechSynthesisUtterance(AIResponse.replace(/[^a-zA-Z0-9\s%.,!?]/g, '')); //Play the AI response text, removing non-alphanumeric characters for better pronunciation

        shadowRoot.querySelector("#copyAnswer").onclick = function() {
          shadowRoot.querySelector("#copyAnswer").style.display = 'none';
          shadowRoot.querySelector("#AnswerCopied").style.display = 'inline-flex';
          navigator.clipboard.writeText(AIResponse.replace(/(\*\*|##)/g, '')); //Copy the AI response without duplicated symbols
          setTimeout(() => { //Return play BTN svg
            shadowRoot.querySelector("#copyAnswer").style.display = 'inline-flex';
            shadowRoot.querySelector("#AnswerCopied").style.display = 'none';
          }, 1000);
        };

        shadowRoot.querySelector("#dictate").classList.add('show'); //Show the dictate BTN
        shadowRoot.querySelector("#TopPause").classList.remove('show'); //Hide the TopPause BTN
        shadowRoot.querySelector("#AIMenu").classList.add('show'); //Show the AIMenu BTN
        shadowRoot.querySelector("#prompt").focus();
      },
      onabort: function(response) {
        clearInterval(Generating); //Stop showing ▋
        shadowRoot.querySelector("#finalanswer").innerText = 'ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤResponse has been interrupted.';
        shadowRoot.querySelector("#dictate").classList.add('show'); //Show the dictate BTN
        shadowRoot.querySelector("#copyAnswer").style.display = 'none'; //Hide the copy AI answer BTN
        shadowRoot.querySelector("#TopPause").classList.remove('show'); //Hide the TopPause BTN
        shadowRoot.querySelector("#AIMenu").classList.add('show'); //Show the AIMenu BTN
        shadowRoot.querySelector("#speak").style.display = 'none'; //Hide the speak BTN
      },
      onloadstart: function(response) {
        shadowRoot.querySelector("#dictate").classList.remove('show'); //Hide the dictate BTN
        shadowRoot.querySelector("#TopPause").classList.add('show'); //Show the TopPause BTN
        shadowRoot.querySelector("#AIAnswer").innerHTML = (html => BypassTT?.createHTML(html) || html)(`<div id="avatar">
  <svg width="32" height="32" viewBox="0 0 32 32">
    <rect width="32" height="32" rx="8" fill="#5021FF"></rect>
    <path fill-rule="evenodd" clip-rule="evenodd" d="M12.7375 12.5186C12.7375 10.7594 14.1636 9.33333 15.9228 9.33333C17.6819 9.33333 19.108 10.7594 19.108 12.5186C19.108 14.2778 17.6819 15.7039 15.9228 15.7039C14.1636 15.7039 12.7375 14.2778 12.7375 12.5186ZM15.9228 8C13.4272 8 11.4042 10.023 11.4042 12.5186C11.4042 15.0142 13.4272 17.0372 15.9228 17.0372C18.4183 17.0372 20.4414 15.0142 20.4414 12.5186C20.4414 10.023 18.4183 8 15.9228 8ZM11.5819 17.6255C11.8982 17.437 12.0018 17.0278 11.8133 16.7115C11.6248 16.3952 11.2156 16.2916 10.8993 16.4801C10.6831 16.6089 10.4663 16.8148 10.2746 17.0349C10.0746 17.2644 9.87008 17.546 9.68554 17.8601C9.32327 18.4767 9 19.2839 9 20.1144C9 20.8532 9.12214 21.4899 9.41978 22.0347C9.72071 22.5855 10.1679 22.9818 10.7122 23.2937C11.2082 23.5779 11.7335 23.8486 12.5469 24.0394C13.3432 24.2262 14.3899 24.3308 15.9368 24.3308C19.0007 24.3308 20.5881 23.9091 21.6046 22.9619C22.5374 22.0927 22.9531 21.1528 22.9506 20.1128C22.9489 19.4161 22.6359 18.6481 22.2905 18.0381C21.9435 17.4254 21.4844 16.8333 21.0628 16.5185C20.7678 16.2983 20.3501 16.3589 20.1298 16.6539C19.9095 16.949 19.9701 17.3667 20.2652 17.587C20.4764 17.7446 20.826 18.1578 21.1302 18.695C21.4359 19.2349 21.6164 19.7615 21.6173 20.116C21.6188 20.7365 21.3947 21.3351 20.6956 21.9864C20.0802 22.5599 18.9602 22.9975 15.9368 22.9975C14.4401 22.9975 13.5073 22.8952 12.8514 22.7414C12.2127 22.5915 11.8133 22.3879 11.375 22.1368C10.9852 21.9134 10.7439 21.6773 10.5899 21.3954C10.4326 21.1075 10.3333 20.7113 10.3333 20.1144C10.3333 19.609 10.5393 19.039 10.8351 18.5355C10.9796 18.2896 11.1364 18.0755 11.2798 17.9108C11.4315 17.7368 11.5404 17.6502 11.5819 17.6255Z" fill="white"></path>
  </svg>
</div>
<div class="AnswerContainer" style="color: ${TextColor};">
  <div id="msg">${msg}</div>
  <div id="LineEl"></div>
  <div class="BoxGemini" id="ContainerGemini">${GeminiSVG}</div>
  <div id="finalanswer"><p id="LoadBar">ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ</p></div>
  <div id="AIMenu">
    <button id="bottompause" class="MenuBTNs" style="display: none; ${IMGsColor};">
      <svg width="16" height="16" viewBox="0 -2 8 13" fill="none">
        <path fill-rule="evenodd" clip-rule="evenodd" d="M1.49879 0.5C0.671042 0.5 1.52588e-05 1.17103 1.52588e-05 1.99878V8.00122C1.52588e-05 8.82897 0.671042 9.5 1.49879 9.5C2.32655 9.5 2.99757 8.82897 2.99757 8.00122V1.99878C2.99757 1.17103 2.32655 0.5 1.49879 0.5ZM1.00002 1.99878C1.00002 1.72331 1.22333 1.5 1.49879 1.5C1.77426 1.5 1.99757 1.72331 1.99757 1.99878V8.00122C1.99757 8.27669 1.77426 8.5 1.49879 8.5C1.22333 8.5 1.00002 8.27669 1.00002 8.00122V1.99878ZM6.50575 0.5C5.678 0.5 5.00697 1.17103 5.00697 1.99878V8.00122C5.00697 8.82897 5.678 9.5 6.50575 9.5C7.33351 9.5 8.00453 8.82897 8.00453 8.00122V1.99878C8.00453 1.17103 7.33351 0.5 6.50575 0.5ZM6.00697 1.99878C6.00697 1.72331 6.23028 1.5 6.50575 1.5C6.78122 1.5 7.00453 1.72331 7.00453 1.99878V8.00122C7.00453 8.27669 6.78122 8.5 6.50575 8.5C6.23028 8.5 6.00697 8.27669 6.00697 8.00122V1.99878Z" fill="white"></path>
      </svg>
    </button>
    <button id="speak" class="MenuBTNs" style="display: inline-flex; ${IMGsColor};">
      <svg width="16" height="16" viewBox="0 0 16 16" fill="#fff">
        <path fill-rule="inherit" clip-rule="evenodd" d="M6.99585 3.24577C6.81771 3.36247 6.58157 3.56921 6.2112 3.89612L4.20926 5.66321L4.18027 5.68895C4.07192 5.78537 3.93427 5.90788 3.75948 5.97398C3.58469 6.04009 3.40043 6.03934 3.25538 6.03875L3.21662 6.03864H2.50001C2.25017 6.03864 2.11309 6.03971 2.018 6.05249L2.01435 6.05299L2.01385 6.05663C2.00107 6.15173 2.00001 6.28881 2.00001 6.53864V9.53864C2.00001 9.78848 2.00107 9.92556 2.01385 10.0207L2.01435 10.0243L2.01799 10.0248C2.11309 10.0376 2.25017 10.0386 2.50001 10.0386H3.2338L3.27297 10.0385C3.41953 10.0379 3.60574 10.0372 3.78206 10.1046C3.95838 10.172 4.09659 10.2968 4.20536 10.395L4.23448 10.4212L6.20852 12.189C6.57949 12.5212 6.81642 12.7317 6.99537 12.8508L7.00796 12.8591L7.01018 12.8442C7.04081 12.6314 7.04208 12.3145 7.04208 11.8166V4.27098C7.04208 3.77697 7.04081 3.46312 7.01041 3.25234L7.00823 3.23776L6.99585 3.24577ZM7.1278 3.17543C7.12773 3.17558 7.1259 3.17617 7.12253 3.17674C7.12619 3.17556 7.12788 3.17528 7.1278 3.17543ZM6.97834 3.11168C6.97653 3.10878 6.97576 3.10702 6.97583 3.10686C6.97589 3.10671 6.9768 3.10815 6.97834 3.11168ZM6.97515 12.9916C6.97508 12.9915 6.97586 12.9897 6.97769 12.9867C6.97613 12.9903 6.97522 12.9918 6.97515 12.9916ZM7.12322 12.9217C7.12662 12.9223 7.12847 12.9229 7.12854 12.9231C7.12862 12.9232 7.12692 12.9229 7.12322 12.9217ZM6.44789 2.40927C6.69128 2.24984 7.05663 2.07222 7.45332 2.25119C7.85002 2.43016 7.95863 2.82161 8.00017 3.10959C8.04215 3.40069 8.04212 3.78842 8.04209 4.23246L8.04208 4.27098V11.8166L8.04209 11.8551C8.04212 12.3031 8.04215 12.6937 7.99998 12.9867C7.95838 13.2757 7.84954 13.67 7.45005 13.8485C7.05057 14.027 6.68423 13.845 6.44119 13.6832C6.19482 13.5192 5.90383 13.2586 5.57013 12.9597L5.57012 12.9597L5.5414 12.934L3.56736 11.1662C3.49189 11.0986 3.45417 11.0652 3.42542 11.0429L3.4238 11.0417L3.42176 11.0415C3.38548 11.0389 3.3351 11.0386 3.2338 11.0386H2.50001L2.47281 11.0386C2.26077 11.0387 2.05471 11.0387 1.88475 11.0159C1.69315 10.9901 1.47451 10.9274 1.2929 10.7458C1.11129 10.5641 1.04853 10.3455 1.02277 10.1539C0.999921 9.98395 0.999962 9.77788 1 9.56585L1.00001 9.53864V6.53864L1 6.51144C0.999962 6.29941 0.999921 6.09334 1.02277 5.92338C1.04853 5.73178 1.11129 5.51315 1.2929 5.33154C1.47451 5.14993 1.69315 5.08717 1.88475 5.06141C2.0547 5.03856 2.26076 5.0386 2.4728 5.03864H2.4728L2.50001 5.03864H3.21662C3.31686 5.03864 3.36669 5.03836 3.40258 5.03584L3.40461 5.03569L3.40622 5.03446C3.4348 5.0126 3.47234 4.97984 3.5475 4.9135L5.54944 3.14641L5.57832 3.12091L5.57834 3.1209L5.57835 3.12089C5.91122 2.82703 6.20187 2.57043 6.44789 2.40927ZM10.1345 5.33693C10.2257 5.07628 10.5109 4.93892 10.7716 5.03012C11.5468 5.30139 12.1156 5.71974 12.4845 6.27343C12.851 6.82368 12.9896 7.46152 12.9888 8.12059C12.9881 8.77784 12.8479 9.36588 12.4685 9.8652C12.095 10.3568 11.5331 10.7045 10.8038 10.9736C10.5448 11.0692 10.2573 10.9367 10.1617 10.6777C10.0661 10.4186 10.1986 10.1311 10.4577 10.0355C11.0913 9.80166 11.4579 9.54244 11.6723 9.2602C11.8809 8.98566 11.9883 8.63292 11.9888 8.11953C11.9894 7.59876 11.8804 7.1703 11.6522 6.82786C11.4264 6.48888 11.0533 6.18815 10.4413 5.97401C10.1807 5.88281 10.0433 5.59758 10.1345 5.33693ZM10.5874 3.04341C10.3154 2.99552 10.0561 3.17716 10.0082 3.44912C9.96033 3.72108 10.142 3.98036 10.4139 4.02825C11.5592 4.22994 12.4289 4.71111 13.0117 5.38193C13.5932 6.05126 13.9207 6.94547 13.9201 8.03323C13.9195 9.12625 13.5988 9.99297 13.0242 10.6448C12.4457 11.3008 11.573 11.7808 10.3984 12.026C10.1281 12.0825 9.95475 12.3474 10.0112 12.6177C10.0676 12.888 10.3325 13.0614 10.6028 13.0049C11.9332 12.7271 13.0195 12.1622 13.7743 11.3061C14.5328 10.4457 14.9194 9.33294 14.9201 8.03375C14.9208 6.7372 14.5263 5.60054 13.7666 4.72609C13.0082 3.85314 11.9174 3.27763 10.5874 3.04341Z"></path>
      </svg>
    </button>
    <button id="AnswerCopied" class="MenuBTNs" style="display: none; ${IMGsColor};">
      <svg width="16" height="16" viewBox="0 0 10 8" fill="none">
        <path d="M1.02063 3.68066L3.67635 6.65194L8.97935 1.34802" stroke="white" stroke-linecap="round" />
      </svg>
    </button>
    <button id="copyAnswer" class="MenuBTNs" style="display: inline-flex; ${IMGsColor};">
      <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
        <path fill-rule="evenodd" clip-rule="evenodd" d="M4.22727 4.5H3.5C2.39543 4.5 1.5 5.39543 1.5 6.5V12.5C1.5 13.6046 2.39543 14.5 3.5 14.5H9.5C10.6046 14.5 11.5 13.6046 11.5 12.5V11.7727H10.5V12.5C10.5 13.0523 10.0523 13.5 9.5 13.5H3.5C2.94772 13.5 2.5 13.0523 2.5 12.5V6.5C2.5 5.94772 2.94772 5.5 3.5 5.5H4.22727V4.5Z" fill="white"></path>
        <rect x="5" y="2" width="9" height="9" rx="1.5" stroke="white"></rect>
      </svg>
    </button>
    <button id="NewAnswer" class="MenuBTNs" style="display: inline-flex; ${IMGsColor};">
      <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
        <path d="M3.64362 8.00003C3.64362 5.70974 5.50027 3.85309 7.79056 3.85309C9.06546 3.85309 10.2057 4.42786 10.9671 5.33402C11.0399 5.42069 11.0743 5.56527 10.9942 5.64533L10.5291 6.11045C10.2141 6.42543 10.4372 6.964 10.8826 6.964H12.75C13.0261 6.964 13.25 6.74015 13.25 6.464V4.59664C13.25 4.15119 12.7114 3.92811 12.3964 4.24309L11.8532 4.78632C11.7983 4.84128 11.7013 4.81866 11.6513 4.75915C10.7273 3.65956 9.34048 2.95947 7.79056 2.95947C5.00674 2.95947 2.75 5.21621 2.75 8.00003C2.75 10.7839 5.00674 13.0406 7.79056 13.0406C9.95003 13.0406 11.7913 11.6828 12.5092 9.77593C12.5962 9.54499 12.4795 9.28729 12.2485 9.20034C12.0176 9.11338 11.7599 9.2301 11.6729 9.46104C11.0818 11.0312 9.5659 12.147 7.79056 12.147C5.50027 12.147 3.64362 10.2903 3.64362 8.00003Z" fill="white"></path>
      </svg>
    </button>
  </div>`); //Create the AI menu HTML

        var transcript = ""; //Add words
        startGeneratingText();
        shadowRoot.querySelector("#CloseOverlay").classList.add('show'); //Show a black overlay
        var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
        var recognition = new SpeechRecognition();
        recognition.interimResults = true; //Show partial results
        recognition.continuous = true; //Keep listening until stopped
        shadowRoot.querySelector("#highlight_menu").classList.remove('show'); //Hide the mini menu on the page
        shadowRoot.querySelectorAll("#AIBox, .animated-border, #AIBox.AnswerBox").forEach(el => el.classList.add('show')); //Show the AI input and box
        getSelection().removeAllRanges(); //UnSelect the selected text so that if the user clicks on a previously selected text the menu won't show up again

        shadowRoot.querySelector("#CloseOverlay").onclick = function() {
          shadowRoot.querySelectorAll("#AIBox, .animated-border, #AIBox.AnswerBox").forEach(el => el.classList.remove('show')); //Hide the AI input and box
          this.classList.remove('show');
          recognition.stop(); //Stop recognizing audio
          speechSynthesis.cancel(); //Stop speaking
          request.abort(); //Abort any ongoing request
          if (shadowRoot.querySelector("#gemini").style.display === 'none') { //If the Gemini BTN is hidden
            shadowRoot.querySelector("#AddContext").remove(); //Return original prompt input styles
            shadowRoot.querySelector("#context").classList.remove('show'); //Hide the context view
            shadowRoot.querySelector("#prompt").placeholder = 'Enter your prompt to Gemini'; //Return default placeholder
          }
        };

        shadowRoot.querySelector("#TopPause").onclick = function() {
          shadowRoot.querySelector("#dictate").classList.add('show'); //Show the dictate BTN
          shadowRoot.querySelector("#TopPause").classList.remove('show'); //Hide the TopPause BTN
          request.abort(); //Abort the request
        };

        recognition.onend = function() {
          shadowRoot.querySelectorAll('.state1, .state2, .state3').forEach((state, index) => { //ForEach SVG animation state
            index.toString().match(/1|2/) ? state.style.display = 'none' : ''; //Show only the 1 state

            state.classList.remove('animate'+index); //Stop the voice recording animation
          });
          isRecognizing = false;
          transcript !== '' ? Generate(transcript, shadowRoot.querySelector("#prompt").className) : shadowRoot.querySelector("#finalanswer").innerHTML = (html => BypassTT?.createHTML(html) || html)(`<br>No audio detected. Please try again or check your mic settings.ㅤㅤㅤㅤㅤㅤㅤㅤㅤ<br><br>`); //Call the AI API if audio has been detected or show an error message
        }; //Finish the recognition end event listener

        recognition.onresult = function(event) { //Handle voice recognition results
          transcript = ""; //Clear the transcript at the start of the event
          for (var i = 0; i < event.results.length; i++) { //For all transcript results
            transcript += event.results[i][0].transcript + ' '; //Concatenate all intermediate transcripts
          }
          shadowRoot.querySelector("#msg").innerText = transcript.length > 240 ? transcript.slice(0, 240) + '…' : transcript; //Display recognized words
        };

        shadowRoot.querySelector("#dictate").onclick = function() {
          if (isRecognizing) {
            recognition.stop();
          } else {
            isRecognizing = true;
            recognition.start();
            shadowRoot.querySelectorAll('.state1, .state2, .state3').forEach((state, index) => { //ForEach SVG animation state
              state.style.display = 'unset'; //Show all states
              state.classList.add('animate'+index); //Start the voice recording animation
            });
          }
        };

        var desiredVoice = null;
        speechSynthesis.onvoiceschanged = () => desiredVoice = speechSynthesis.getVoices().find(v => v.name === "Microsoft Zira - English (United States)"); //Get and store the desired voice
        speechSynthesis.onvoiceschanged(); //Handle cases where the event doesn't fire

        function speakText(text) {
          audio.voice = desiredVoice; //Use the desiredVoice
          speechSynthesis.speak(audio); //Speak the text
        }

        shadowRoot.querySelectorAll("#speak, #bottompause").forEach(function(el) {
          el.onclick = function() { //When speak or the bottom pause BTNs are clicked
            if (speechSynthesis.speaking) {
              speechSynthesis.cancel();
              shadowRoot.querySelector("#speak").style.display = 'inline-flex'; //Show the play BTN
              shadowRoot.querySelector("#bottompause").classList.remove('show'); //Hide the pause BTN
            }
            else
            {
              shadowRoot.querySelector("#speak").style.display = 'none'; //Hide the play BTN
              shadowRoot.querySelector("#bottompause").classList.add('show'); //Show the pause BTN

              speakText(audio); //Speak the AI reponse text

              audio.onend = (event) => {
                shadowRoot.querySelector("#speak").style.display = 'inline-flex'; //Show the play BTN
                shadowRoot.querySelector("#bottompause").classList.remove('show'); //Hide the pause BTN
              };
            }
          };
        });

        shadowRoot.querySelector("#NewAnswer").onclick = function() {
          shadowRoot.querySelector("#dictate").classList.remove('show'); //Hide the dictate BTN
          shadowRoot.querySelector("#TopPause").classList.add('show'); //Show the top pause BTN
          Generate(Prompt, button); //Call the AI API
        };
      } //Finish the onloadstart event listener
    });//Finish the GM.xmlHttpRequest function
  } //Finish the Generate function

  shadowRoot.querySelector("#prompt").addEventListener("keydown", (event) => {
    if (event.key === "Enter") {
      Generate(shadowRoot.querySelector("#prompt").value, shadowRoot.querySelector("#prompt").className); //Call the AI API
      shadowRoot.querySelector("#prompt").value = ''; //Erase the prompt text
    }
    if (event.key === "Tab") {
      if (shadowRoot.querySelector("#prompt").placeholder.match('using')) { //If the input bar contains the word "using"
        shadowRoot.querySelector("#AddContext").remove(); //Return original prompt input styles
        shadowRoot.querySelector("#context").classList.remove('show'); //Hide the context view
        shadowRoot.querySelector("#prompt").placeholder = 'Enter your prompt to Gemini'; //Return default placeholder
      }
      else
      {
        shadowRoot.querySelector("#context").classList.add('show'); //Show the context view
        shadowRoot.querySelector("#prompt").placeholder = `Gemini is using ${location.host.replace('www.','')} for context...`; //Change placeholder
        shadowRoot.querySelector("#highlight_menu").insertAdjacentHTML('beforebegin', ` <style id="AddContext"> #gemini { display: none; /* Hide the Gemini button */ } #prompt { left: 12%; /* Push the input bar to the left */ width: 75%; /* Increase the input bar width */ } #tabcontext { display: none; /* Hide the "page context tab" */ } .animated-border { --color-OrangeORLilac: #FF8051; /* Change the border effect color to orange */ } </style> `); //Show the context bar
      }
    }
    setTimeout(() => { //Wait for the code above to execute
      shadowRoot.querySelector("#prompt").focus(); //Refocus on the input bar
    }, 0);
  });

  if (document.body.textContent !== '' || document.body.innerText !== '') //If the body has any text
  {
    document.body.appendChild(HtmlMenu); //Add the script menu div container
  }

  shadowRoot.querySelectorAll("#AIBTN").forEach(function(button) {
    button.onmousedown = function(event,i) { //When the Explore or the Translate BTNs are clicked
      if (GM_getValue("APIKey") === undefined || GM_getValue("APIKey") === null || GM_getValue("APIKey") === '') { //Set up the API Key if it isn't already set
        GM_setValue("APIKey", prompt('Enter your API key\n*Press OK\n\nYou can get a free API key at https://aistudio.google.com/app/apikey'));
      }
      if (GM_getValue("APIKey") !== null && GM_getValue("APIKey") !== '') {
        Generate(SelectedText, this.className); //Call the AI API
      }
    };
  });

  //Allow the script in iframes__________________________________________________________________________________________________________________________________________________________________
  setTimeout(function() {
    const AllIframes = document.querySelectorAll("iframe");
    for (var i = AllIframes.length; i--;) {
      if (AllIframes[i].allow.match('clipboard-write;') === null && AllIframes[i].src.match(Links) !== null && AllIframes[i].src.match(/recaptcha|(rt|hrms.*.*.edu)|challenges.cloudflare|youtube|dailymotion|vimeo|streamtape|mcloud|vidstream|dood.wf|mp4upload|googlevideo|kaltura|crunchyroll|animesup|embtaku.pro|aniwave.se\/ajax|google.com\/recaptcha\/|blank.html|\.mp4/) === null) //If the iframe doesn't have the clipboard-write attribute yet, it has a link and it isn't a video
      {
        AllIframes[i].allow = AllIframes[i].allow + 'clipboard-write;'; //Add the permission to copy the iframe text
        AllIframes[i].src = AllIframes[i].src; //Reload the iframe to apply the new permissions
      }
    }
  }, 4000);

  window.addEventListener('scroll', async function() {
    shadowRoot.querySelector("#highlight_menu").classList.remove('show'); //Hide the menu
    if (LeftClicked === false && SelectedText !== '') { //If the Left Click isn't being held, and if something is currently selected
      getSelection().removeAllRanges(); //UnSelect the selected text when scrolling the page down so that if the user clicks on the past selected text the menu won't show up again
    }
  });
}