-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
82 lines (65 loc) · 2.39 KB
/
script.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
// ==UserScript==
// @name AWS SSO Displayname
// @description Makes it clearer which AWS account you are currently logged into.
// @version 2.0.2
// @grant none
// @include https://console.aws.amazon.*
// @include https://*.console.aws.amazon.*
// ==/UserScript==
// Add your AWS account information below
// You can use the following command to populate an initial map without colors:
// $ aws organizations list-accounts | jq -r '.Accounts | map(.Id + ": [\"" + .Name + "\"],")[]'
const accounts = {
786908262739: ["root"],
264850291039: ["production", "red"],
274593078001: ["staging", "blue"],
128405919384: ["shared", "gray"],
998726572012: ["testing", "yellow"],
891749101823: ["personal"],
}
const NO_COLOR = null;
const POLL_INTERVAL = 100;
const extractRole = (displayName) => {
return /(\w+)\/\w+/.exec(displayName)[1];
};
const getAccountInfo = (accountId) => {
const [alias, color] = accounts[accountId] || [accountId, NO_COLOR];
return { alias, color };
};
const getTitleElement = () =>
document.querySelector("span[title^='AWSReservedSSO']");
const getDisplayNameElement = () =>
document.querySelector(
'[data-testid="awsc-nav-account-menu-button"] > span:nth-child(1)'
);
const getDropdownHeaderElement = () =>
document.querySelector(
'[data-testid="awsc-nav-account-menu-button"] [title]'
);
const isReady = () =>
getTitleElement() && getDisplayNameElement() && getDropdownHeaderElement();
const interval = setInterval(() => {
if (isReady()) {
clearInterval(interval);
onReady();
}
}, POLL_INTERVAL);
function onReady(elem) {
const accountId = getTitleElement()
.getAttribute("title")
.match(/AWSReservedSSO_.*\s(.*)/)[1]
.replace(/-/g, "");
const { alias, color } = getAccountInfo(accountId);
// Expected element format is "<span title="AWSReservedSSO_AdministratorAccess_f23ec324f447d77/jake @ onvp-production" class="_1Vtx1Z7gxtNZJP2MVzVCLO _31GHADTBDW3BW3qVhZRFPq">AdministratorAccess/jake</span>"
const displayName = getDisplayNameElement().textContent;
console.debug({ displayName });
// Expecting "AdministratorAccess/jake"
const role = extractRole(displayName);
console.debug({ role });
// Expecting "AdministratorAccess"
const dropdownHeader = getDropdownHeaderElement();
dropdownHeader.innerHTML = `${role}@${alias}`;
if (color !== NO_COLOR) {
dropdownHeader.style.backgroundColor = color;
}
}