Auto-select Single Google Account

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

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==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);
})();