Skip to content

Instantly share code, notes, and snippets.

@nmc-eco
Created August 27, 2026 17:30
Show Gist options
  • Select an option

  • Save nmc-eco/cabf968e27ad6443f5dee5a6a2e5135f to your computer and use it in GitHub Desktop.

Select an option

Save nmc-eco/cabf968e27ad6443f5dee5a6a2e5135f to your computer and use it in GitHub Desktop.
Google Al/Web Toggle (Violent Monkey)
// ==UserScript==
// @name         Google All/Web Toggle (Ctrl/Cmd+I)
// @match        https://www.google.com/search*
// @grant        none
// @run-at       document-start
// ==/UserScript==
(() => {
  const STORAGE_KEY = 'google_web_mode';
  const TOGGLE_KEY = 'I';
  // Default is now Web mode (AI off). Only an explicit '0' means All mode.
  const isWebMode = () => localStorage.getItem(STORAGE_KEY) !== '0';
  const setWebMode = (enabled) =>
    localStorage.setItem(STORAGE_KEY, enabled ? '1' : '0');
  function targetUrl(webMode) {
    const url = new URL(location.href);
    if (webMode) {
      url.searchParams.set('udm', '14'); // Selects Web results
    } else {
      url.searchParams.delete('udm');    // Returns to normal All results
    }
    return url.toString();
  }
  // Apply the saved/default mode on load, before the page renders.
  // Only touches plain web search (no udm) and the Web tab (udm=14),
  // so Images/Videos/News/etc. are left alone.
  function enforceMode() {
    const url = new URL(location.href);
    const udm = url.searchParams.get('udm');
    if (isWebMode()) {
      if (udm === null) {
        url.searchParams.set('udm', '14');
        location.replace(url.toString());
        return true;
      }
    } else {
      if (udm === '14') {
        url.searchParams.delete('udm');
        location.replace(url.toString());
        return true;
      }
    }
    return false;
  }
  if (enforceMode()) return; // redirecting; stop here
  function showIndicator() {
    const make = () => {
      let el = document.getElementById('google-web-toggle-indicator');
      if (!el) {
        el = document.createElement('div');
        el.id = 'google-web-toggle-indicator';
        Object.assign(el.style, {
          position: 'fixed',
          right: '8px',
          bottom: '8px',
          zIndex: '2147483647',
          padding: '6px 8px',
          borderRadius: '6px',
          background: 'rgba(0,0,0,.7)',
          color: '#fff',
          font: '12px Arial, sans-serif',
          pointerEvents: 'none'
        });
        document.documentElement.appendChild(el);
      }
      el.textContent = isWebMode() ? 'Google: Web (AI off)' : 'Google: All (AI may appear)';
      clearTimeout(el._hideTimer);
      el.style.opacity = '1';
      el._hideTimer = setTimeout(() => { el.style.opacity = '0'; }, 1400);
    };
    if (document.documentElement) make();
    else document.addEventListener('DOMContentLoaded', make, { once: true });
  }
  window.addEventListener('keydown', (event) => {
    const isMac = /Mac|iPhone|iPad/.test(navigator.platform);
    const modifierPressed = isMac ? event.metaKey : event.ctrlKey;
    if (
      !modifierPressed ||
      event.altKey ||
      event.shiftKey ||
      event.key.toUpperCase() !== TOGGLE_KEY
    ) return;
    const active = document.activeElement;
    const typing =
      active &&
      (active.isContentEditable ||
       /^(INPUT|TEXTAREA|SELECT)$/.test(active.tagName));
    if (typing) return;
    event.preventDefault();
    const newWebMode = !isWebMode();
    setWebMode(newWebMode);
    location.assign(targetUrl(newWebMode));
  }, true);
  showIndicator();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment