forked from bunjik/aws-console-customize-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
93 lines (79 loc) · 2.26 KB
/
extension.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
/**
* AWS Console Customize Extension
* Created by fumiharu kinoshita
*/
const agent = window.navigator.userAgent.toLowerCase();
const SETTING_KEY = 'awsconsole';
var intervalId;
var retryLimit = 60
function getStorage() {
if (agent.indexOf("chrome") != -1) {
// chrome extension
return new Promise((resolve, reject) => {
chrome.storage.sync.get(SETTING_KEY, (ruleList) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(ruleList);
}
})
});
} else {
// other browser
return browser.storage.sync.get(SETTING_KEY);
}
}
function applyRule(rule) {
// header
$('header > nav').css('background-color', rule.color);
$('.globalNav-0366 > a').css('background-color', rule.color);
// footer
$('#console-nav-footer-inner').css('background-color', rule.color);
// label
if (rule.showLabel && rule.label != null && rule.label.length > 0) {
$('body').prepend('<span id="ruleLabel">' + rule.label + '</span>');
}
}
function findAccount() {
var name = $("span[data-testid='awsc-nav-account-menu-button'] span:first").text();
var acct = $("div[data-testid='account-detail-menu'] span:nth-child(2)").text();
if (name != '') {
//console.log(name, acct);
acct = acct.replaceAll('-', '');
clearInterval(intervalId);
var regions = location.search.match(/region=(.*?)(&|$)/);
var region = "";
if (regions != null && regions.length > 1) {
region = regions[1];
}
// load setting.
getStorage().then(ruleList => {
ruleList[SETTING_KEY].some(rule => {
var re = new RegExp(rule.user);
if (rule.enableRule && (re.test(name) || re.test(acct))) {
if (region == rule.region || "all-region" == rule.region) {
// apply rule.
applyRule(rule);
return true;
}
}
});
},
err => {
console.error(err);
});
}
if (--retryLimit <= 0) {
clearInterval(intervalId);
}
}
// main function
$(function() {
// show/hide label
$('#consoleNavHeader').hover(
() => $('#ruleLabel').css('visibility', 'hidden'),
() => $('#ruleLabel').css('visibility', '')
);
// waiting loading.
intervalId = setInterval(findAccount, 1000);
});