-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
39 lines (33 loc) · 1.09 KB
/
popup.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
import { accounts } from './accounts.js';
const rootDiv = document.getElementById('root');
(function addAccountDetailsToDom() {
let content = '';
accounts.forEach((account) => {
content += `<button class="account-btn" data-authuser="${account.authuser}">${account.displayName}</button>`;
});
rootDiv.innerHTML = content;
accountSwitchFunctionality();
})();
async function getCurrentTabId() {
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab?.id;
}
function accountSwitchFunctionality() {
const accountButtons = document.querySelectorAll('.account-btn');
accountButtons.forEach((accountButton) => {
accountButton.addEventListener('click', accountButtonClickHandler);
});
async function accountButtonClickHandler(e) {
chrome.scripting.executeScript({
target: { tabId: await getCurrentTabId() },
func: switchAccount,
args: [e.target.dataset.authuser]
});
}
}
function switchAccount(authUser) {
let url = new URL(window.location.href);
url.search = `?authuser=${authUser}`;
window.location.href = url.href;
}