-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (108 loc) · 3.52 KB
/
Copy pathindex.js
File metadata and controls
127 lines (108 loc) · 3.52 KB
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
class containersHTTPS {
createIcon(container) {
const icon = document.createElement("div");
icon.classList.add("icon");
const iconUrl = container.iconUrl || "img/blank-tab.svg";
icon.style.mask = `url(${iconUrl}) top left / contain`;
icon.style.background = container.colorCode || "#000";
return icon;
}
async createToggle(container) {
const toggle = document.createElement("input");
toggle.type = "checkbox";
toggle.classList.add("toggle");
toggle.addEventListener("click", this);
toggle.addEventListener("submit", this);
toggle.addEventListener("change", this);
const state = await this.getState(container.cookieStoreId);
toggle.checked = state;
toggle.dataset.cookieStoreId = container.cookieStoreId;
toggle.dataset.action = "toggle";
return toggle;
}
createNewTab(container) {
const newTabButton = document.createElement("button");
newTabButton.classList.add("new-tab");
newTabButton.addEventListener("click", this);
newTabButton.addEventListener("submit", this);
newTabButton.dataset.cookieStoreId = container.cookieStoreId;
newTabButton.dataset.action = "new-tab";
return newTabButton;
}
async createRow(container) {
const li = document.createElement("li");
li.appendChild(this.createIcon(container));
li.appendChild(document.createTextNode(container.name));
li.appendChild(await this.createToggle(container));
li.appendChild(this.createNewTab(container));
return li;
}
stateKey(cookieStoreId) {
return `state-${cookieStoreId}`;
}
async getState(cookieStoreId) {
const stateKey = this.stateKey(cookieStoreId);
const states = await browser.storage.local.get(stateKey);
return states[stateKey];
}
async storeState(cookieStoreId, checked) {
console.log("storing state", cookieStoreId, checked);
const stateKey = this.stateKey(cookieStoreId);
return browser.storage.local.set({
[stateKey]: checked
});
}
handleEvent(event) {
const button = event.target;
const cookieStoreId = button.dataset.cookieStoreId;
switch (button.dataset.action) {
case "new-tab":
browser.tabs.create({
cookieStoreId
});
break;
case "toggle":
this.storeState(cookieStoreId, button.checked);
break;
}
}
/**
* Do some house keeping to clean up storage
*/
removeContainer(cookieStoreId) {
browser.storage.local.remove(this.stateKey(cookieStoreId));
}
constructor(cookieStoreId) {
this.urlElement = document.getElementById("search-field");
const rebuildEvent = () => {
this.rebuildMenu();
};
browser.contextualIdentities.onRemoved.addListener((container) => {
this.removeContainer(container.cookieStoreId);
rebuildEvent();
});
browser.contextualIdentities.onUpdated.addListener(rebuildEvent);
browser.contextualIdentities.onCreated.addListener(rebuildEvent);
this.rebuildMenu();
}
async rebuildMenu() {
const containers = await browser.contextualIdentities.query({});
const menu = document.getElementById("containers-menu");
while (menu.firstChild) {
menu.removeChild(menu.firstChild);
}
containers.unshift({
cookieStoreId: "firefox-default",
name: "Default"
});
const rowPromises = [];
containers.forEach((container) => {
rowPromises.push(this.createRow(container));
});
const rows = await Promise.all(rowPromises);
rows.forEach((row) => {
menu.appendChild(row);
});
}
};
new containersHTTPS();