Auto-select Single Google Account

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

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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);
})();