Auto-select Single Google Account

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

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

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