Tampermonkey Google Drive sync fix [adopted]

Tampermonkey keeps asking us to select an account when we open the browser. But this script by Jack'lul will do it for us!

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Tampermonkey Google Drive sync fix [adopted]
// @namespace    https://github.com/Tampermonkey/tampermonkey/issues/904
// @version      0.1.3
// @description  Tampermonkey keeps asking us to select an account when we open the browser.  But this script by Jack'lul will do it for us!
// @author       jacklul <jacklul.github.io>
// @license      ISC
// @match        https://accounts.google.com/signin/oauth*tampermonkey*
// @match        https://accounts.google.com/o/oauth2/v2/auth*tampermonkey*
// @grant        none
// ==/UserScript==

// Now I am getting a new login screen without it saying specifically that it needs it for Tampermonkey :-/ (This must have been late 2020 or more likely early 2021)
// On 2021/05/11 Google sent me to this URL, but when I closed it, it actually went back to using the old one, so I guess this isn't necessary for now.
// @DO-NOT-YET-match        https://accounts.google.com/AccountChooser?oauth=1&continue=https%3A%2F%2Faccounts.google.com%2Fsignin%2Foauth%2Flegacy%2Fconsent%3Fauthuser*

(function() {
    'use strict';

    let tryClick = setInterval(function() {
        const el =
              // Original
              document.querySelector('div[data-authuser="0"] #profileIdentifier')
              // New
              || document.querySelector('div[data-authuser="0"]')
        ;

        if (el) {
            const isBusy = document.querySelector('div[role="presentation"][tabindex="-1"]');
            const hasBackButton = document.querySelector('div[role="button"]#next');

            if (isBusy) {
                console.log('Page is busy...');
            }

            // Close page if there was "unknown error"
            if (hasBackButton) {
                window.close();
            }

            // Try clicking until page gets busy
            if (!isBusy) {
                console.log('Clicking...');
                el.click();
            }
        }
    }, 500);
})();