-
Notifications
You must be signed in to change notification settings - Fork 7
/
accounts.js
136 lines (121 loc) · 4.53 KB
/
accounts.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
let defAccount = 0;
window.onload = function () {
const accounts = document.getElementById("accounts_button");
accounts.onclick = function (event) {
openPage(event, "Accounts");
};
document.getElementById("rules_button").onclick = function (event) {
openPage(event, "Rules");
};
accounts.click();
};
function signIn(email) {
// A builder that returns a function to set the url to allow the user to log in to a signed out account
return (responses) => {
let response = responses[0],
url;
if (response.url.includes("google")) {
// Weak test for if Google will redirect to that url
url = new URL(response.url);
} else {
url = new URL("https://www.google.com/webhp");
}
let params = new URLSearchParams(url.search);
// This empties the authuser and moves it to the end to model the format that Google uses for this
params.delete("authuser");
params.set("authuser", "");
url.search = params.toString();
let newURL =
"https://accounts.google.com/AccountChooser?source=ogb&continue=" +
encodeURIComponent(url.toString()) +
"&Email=" +
email;
window.open(newURL);
window.close(); // Just in case
};
}
function populate(response) {
// Create the GUI from the strange nested Array structure that Google accounts responds with
const accounts = response[1].map((info) => ({
index: info[7],
name: info[2],
email: info[3],
profileUrl: info[4],
isLoggedIn: info.length >= 16, // If the account is signed in (as far as I know)
}));
SyncStorage.store({ accounts });
renderNumberOfAccounts(accounts);
renderAccounts(accounts, defAccount); // re-render accounts that arrived
}
function renderNumberOfAccounts(accounts) {
const accountButton = document.getElementById("accounts_button");
const numberOfAccounts = document.createElement("span");
numberOfAccounts.innerHTML = ` (${accounts.length})`;
accountButton.appendChild(numberOfAccounts);
}
function renderAccounts(accounts, defaultAccount) {
const accountsBody = document.getElementById("accounts_body");
accountsBody.innerHTML = ""; // to remove all children, if any
accounts.forEach((user) => {
const t = document.getElementById("cell_template").content;
const cellContent = document.importNode(t, true);
cellContent.querySelector(".cell-image").src = user.profileUrl;
let title;
if (user.isLoggedIn) {
title = `${user.index + 1}) ${user.name}`;
} else {
title = user.name;
}
cellContent.querySelector(".cell-title").textContent = title;
cellContent.querySelector(".cell-description").textContent = user.email;
if (!user.isLoggedIn) {
cellContent.querySelector(".cell-corner").textContent = "Signed out";
cellContent.querySelector(".cell-body").addEventListener("click", () => {
chrome.tabs.query(
{ active: true, currentWindow: true },
signIn(info[3])
);
});
} else {
if (user.index === defaultAccount) {
const checkedImage = document.createElement("img");
checkedImage.src = "images/checked.svg";
cellContent.querySelector(".cell-corner").appendChild(checkedImage);
}
cellContent
.querySelector(".cell-body")
.addEventListener("click", async () => {
SyncStorage.store({ defaultAccount: user.index }, function () {
redirectCurrectTab(user.index);
window.close();
});
});
}
accountsBody.appendChild(cellContent);
});
}
SyncStorage.get(["accounts", "defaultAccount"], (data) => {
defAccount = data.defaultAccount ?? 0;
if (data.accounts && data.accounts.length) {
// render accounts that are in the store, if any
renderAccounts(data.accounts, defAccount);
}
chrome.runtime.sendMessage("fetch_google_accounts", populate);
});
function openPage(evt, name) {
// Declare all variables
let i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to the button that opened the tab
document.getElementById(name).style.display = "block";
evt.currentTarget.className += " active";
}