-
Notifications
You must be signed in to change notification settings - Fork 3
/
aws-pins.js
84 lines (72 loc) · 2.28 KB
/
aws-pins.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
// ------- Main Driver Begins ---------
let pinCount;
let pinDisplayType;
// Get values from local storage
// Default for number of pins = 7
// Default for display type = both label and icon
chrome.storage.sync.get({ "numOfPins": 7, "pinDisplayType": "both" }, function (options) {
pinCount = options.numOfPins;
pinDisplayType = options.pinDisplayType;
});
/*
* Check regularly until the required div appears on the page
*/
var checkExist = setInterval(function () {
if (document.querySelector('div[data-testid="favorites-container"]')) {
const pins = createPinsFromFavs();
addPinsToHeader(pins);
clearInterval(checkExist);
}
}, 100);
// ------- Main Driver Ends -----------
/*
* Given an array of pins, appends each of them to nav header
*/
function addPinsToHeader(pins) {
for (pin of pins) {
const header = document.getElementById('awsc-nav-header').firstChild;
header.append(pin);
}
}
/*
* Iterate over favorites list elements
* and create divs for the pins. Each div contains an anchor
* that navigates to respective AWS service.
* Number of pins to be added is decided based upon user's
* selection from Options page, so is the display type
*/
function createPinsFromFavs() {
const favs = getAllFavs()
let pins = []
for (li of favs) {
const favService = li.firstChild;
const icon = favService.getElementsByTagName('i')[0]
const label = favService.getElementsByTagName('span')[0]
let pin = document.createElement("div");
pin.className = "pin-style"
switch (pinDisplayType) {
case "onlyLabel" :
pin.classList.add("align-label-center")
favService.removeChild(icon)
break;
case "onlyIcon" :
favService.removeChild(label)
break;
}
pin.append(favService)
if (pins.length < pinCount) {
pins.push(pin)
} else {
break;
}
};
return pins;
}
/*
* Clones favorite services from the menu
* Returns an array of li elements
*/
function getAllFavs() {
const favContainer = document.querySelector('div[data-testid="favorites-container"]').firstChild.cloneNode(true);
return favContainer.getElementsByTagName('li');
}