-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.user.js
111 lines (94 loc) · 3.77 KB
/
script.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// ==UserScript==
// @name Gcloud auto select account
// @version 0.0.1
// @description Select my gcloud account automatically and approve access
// @author Vegar Sechmann Molvig
// @match https://accounts.google.com/o/oauth2/auth*
// @match https://accounts.google.com/signin/oauth/consent*
// @match https://accounts.google.com/signin/v2/challenge/pwd*
// ==/UserScript==
(function () {
'use strict';
const initialDelay = 500;
const retryInterval = 100;
function docReady(fn) {
// see if DOM is already available
if (document.readyState === "complete" || document.readyState === "interactive") {
// Ready, wait a bit and make the call
setTimeout(fn, initialDelay);
} else {
document.addEventListener("DOMContentLoaded", function () {
// When ready, wait a bit more before calling
setTimeout(fn, initialDelay);
});
}
}
function selectAccount() {
const getEmailFromUrl = () => {
//https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
const params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
})
return params.autoAccountSelect
}
const email = getEmailFromUrl()
if (!email) {
console.log("select: no 'autoAccountSelect' query parameter, stopping script")
return
}
const fn = () => {
console.log("select: checking for account selector")
let accountSelector = document.querySelector(`[data-identifier="${email}"]`);
if (!accountSelector) {
console.log("select: no account select found (yet)")
return setTimeout(this, retryInterval)
}
accountSelector.click()
}
return fn
}
function approve() {
const fn = () => {
console.log("approve: checking for approve access button")
let submitButton = document.querySelector('[id="submit_approve_access"]');
if (!submitButton) {
console.log("approve: no approve button (yet, retrying)")
return setTimeout(this, retryInterval)
}
submitButton.click()
}
return fn
}
function login() {
const fn = () => {
console.log("login: checking for next button")
const nextButton = document.querySelector('[id="passwordNext"]')
if (!nextButton) {
console.log("login: no next button (yet, retrying)")
return setTimeout(this, retryInterval)
}
console.log("login: checking for password input")
const passwordInput = document.querySelector('[name="password"]')
if (!passwordInput) {
console.log("login: no password input (yet, retrying)")
return setTimeout(this, retryInterval)
}
console.log("login: checking for password input value")
if (!passwordInput.value.length > 0) {
console.log("login: no password value (yet, retrying)")
return setTimeout(this, retryInterval)
}
nextButton.click()
}
return fn
}
[
{ pathStartsWith: "/o/oauth2/auth", handler: selectAccount },
{ pathStartsWith: "/signin/oauth/consent", handler: approve },
{ pathStartsWith: "/signin/v2/challenge/pwd", handler: login },
].forEach((e) => {
if (window.location.href.startsWith("https://accounts.google.com" + e.pathStartsWith)) {
docReady(e.handler())
}
})
})();