-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.js
79 lines (70 loc) · 2.18 KB
/
context.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
// button to open new container
// shortcut to open new container
// function to check and close unopen container
// do it object oriented no ? or as clean and concise as possible
// right click to make link open in new container
/*
// create new container
function onCreated(context) {
console.log(`New identity's ID: ${context.cookieStoreId}.`);
}
function onError(e) {
console.error(e);
}
browser.contextualIdentities.create({
name: "my-thing",
color: "purple",
icon: "briefcase"
}).then(onCreated, onError);
*/
// from the example extension
function eventHandler(event) {
if (event.target.dataset.action === "create") {
browser.tabs.create({
url: "about:blank",
cookieStoreId: event.target.dataset.identity
});
}
if (event.target.dataset.action === "close-all") {
browser.tabs.query({
cookieStoreId: event.target.dataset.identity
}).then((tabs) => {
browser.tabs.remove(tabs.map((i) => i.id));
});
}
event.preventDefault();
}
function createOptions(node, identity) {
for (let option of ["Create", "Close All"]) {
let a = document.createElement("a");
a.href = "#";
a.innerText = option;
a.dataset.action = option.toLowerCase().replace(" ", "-");
a.dataset.identity = identity.cookieStoreId;
a.addEventListener("click", eventHandler);
node.appendChild(a);
}
}
const div = document.getElementById("identity-list");
if (browser.contextualIdentities === undefined) {
div.innerText = "browser.contextualIdentities not available. Check that the privacy.userContext.enabled pref is set to true, and reload the add-on.";
} else {
browser.contextualIdentities.query({})
.then((identities) => {
if (!identities.length) {
div.innerText = "No identities returned from the API.";
return;
}
for (let identity of identities) {
let row = document.createElement('div');
let span = document.createElement('span');
span.className = 'identity';
span.innerText = identity.name;
span.style = `color: ${identity.color}`;
console.log(identity);
row.appendChild(span);
createOptions(row, identity);
div.appendChild(row);
}
});
}