Auto-select Single Google Account

Auto-clicks the only account, or the sun-asterisk account when app_domain contains sun-asterisk

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Auto-select Single Google Account
// @namespace    https://accounts.google.com
// @version      1.1
// @description  Auto-clicks the only account, or the sun-asterisk account when app_domain contains sun-asterisk
// @match        https://accounts.google.com/o/oauth2/auth*
// @match        https://accounts.google.com/v3/signin/accountchooser*
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    const SUN_MARKER = 'sun-asterisk';

    function decodeURIComponentSafe(s) {
        try {
            return decodeURIComponent(s);
        } catch {
            return s;
        }
    }

    /** True when query param app_domain contains sun-asterisk (case-insensitive). */
    function appDomainHasSunAsterisk() {
        const params = new URLSearchParams(window.location.search);
        const raw = params.get('app_domain') || '';
        const decoded = decodeURIComponentSafe(raw);
        const needle = SUN_MARKER.toLowerCase();
        return (
            raw.toLowerCase().includes(needle) ||
            decoded.toLowerCase().includes(needle)
        );
    }

    function clickIfSingleAccount() {
        const accounts = document.querySelectorAll('div[data-identifier]');
        if (accounts.length === 1) {
            console.log('✅ One Google account detected, auto-selecting...');
            accounts[0].click();
            return;
        }
        if (accounts.length > 1 && appDomainHasSunAsterisk()) {
            const needle = SUN_MARKER.toLowerCase();
            const match = [...accounts].find((el) => {
                const id = (el.getAttribute('data-identifier') || '').toLowerCase();
                return id.includes(needle);
            });
            if (match) {
                console.log('✅ Multiple accounts; app_domain has sun-asterisk — selecting matching account.');
                match.click();
                return;
            }
            console.log('ℹ️ Multiple accounts and app_domain has sun-asterisk, but no account email contains sun-asterisk.');
            return;
        }
        console.log(`ℹ️ ${accounts.length} Google accounts detected — skipping auto-click.`);
    }

    // Handle dynamically loaded content
    const observer = new MutationObserver((_, obs) => {
        if (document.querySelectorAll('div[data-identifier]').length > 0) {
            clickIfSingleAccount();
            obs.disconnect();
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });

    // Fallback (in case MutationObserver misses it)
    setTimeout(clickIfSingleAccount, 1000);
})();