-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbackground.js
130 lines (107 loc) · 3.14 KB
/
background.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
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
128
129
130
function onError(e) {
console.error(e);
}
function debug(message) {
console.debug(["[Easy Container Shortcuts] ", message]);
}
function onContainerCommand(command) {
if (command == 'ecs-new-tab-current-container') {
openTabInCurrentContainer();
return;
}
if (command === 'ecs-new-window-current-container') {
openWindowInCurrentContainer();
return;
}
const newTabMatches = command.match(/^ecs-new-tab-container-(\d)$/);
if (newTabMatches && newTabMatches.length == 2) {
openContainerTab(parseInt(newTabMatches[1]) - 1);
return;
}
const currentTabMatches = command.match(/^ecs-current-tab-container-(\d)$/);
if (currentTabMatches && currentTabMatches.length == 2) {
openTabInContainer(parseInt(currentTabMatches[1]) - 1);
return;
}
const newWindowMatches = command.match(/^ecs-new-window-container-(\d)$/);
if (newWindowMatches && newWindowMatches.length === 2) {
openContainerWindow(parseInt(newWindowMatches[1]) - 1);
return;
}
}
function getContexts() {
return browser.contextualIdentities.query({});
}
async function getContextFor(contextNumber) {
let contexts;
try {
contexts = await getContexts();
} catch (e) {
console.error(e);
return;
}
if (contextNumber > contexts.length - 1) {
return; // no container defined for this shortcut, do nothing
}
return contexts[contextNumber];
}
async function openTabInCurrentContainer() {
browser.tabs.query({currentWindow:true, active:true}).then(function(results) {
if (!results || results.length < 1) {
return; // do nothing
}
let currentTab = results[0];
browser.tabs.create({cookieStoreId: currentTab.cookieStoreId});
});
}
async function openWindowInCurrentContainer() {
browser.tabs.query({currentWindow:true, active:true}).then(function(results) {
if (!results || results.length < 1) {
return;
}
let currentTab = results[0];
browser.windows.create({cookieStoreId: currentTab.cookieStoreId});
});
}
async function openContainerTab(contextNumber) {
let context = await getContextFor(contextNumber);
if (!context) {
return;
}
return browser.tabs.create({
cookieStoreId: context.cookieStoreId
});
}
async function openContainerWindow(contextNumber) {
let context = await getContextFor(contextNumber);
if (!context) {
return;
}
return browser.windows.create({
cookieStoreId: context.cookieStoreId
});
}
async function openTabInContainer(contextNumber) {
let context = contextNumber !== -1 ? await getContextFor(contextNumber) : {};
if (!context) {
return;
}
browser.tabs.query({currentWindow:true, active:true, status:'complete'}).then(function(results) {
if (!results || results.length < 1) {
return; // do nothing
}
let currentTab = results[0];
if (currentTab.url.startsWith('about')) {
return;
}
browser.tabs.create({
cookieStoreId: context.cookieStoreId,
index: currentTab.index + 1,
url: currentTab.url,
pinned: currentTab.pinned
});
browser.tabs.remove(currentTab.id);
});
}
// [COMMANDS] register commands
browser.commands.onCommand.addListener(onContainerCommand);