Auto-select Single Google Account

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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