-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.js
209 lines (173 loc) · 6.4 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { Options } from './modules/options.js';
import { IcIdentities } from '../modules/identities.js';
class IdentityChooser {
constructor() {
this.activeIdentityWindows = [];
this.icOptions = new Options();
}
async run() {
try {
await this.icOptions.setupDefaultOptions();
} catch (error) {
//
// Workaround. Several users report issues with Cardboox and
// Identity Chooser accessing the browser.local store
// (https://github.com/speedball2001/identitychooser-mx/issues/18:
//
// 20:30:33.873 TransactionInactiveError: A request was placed
// against a transaction which is currently not active, or which
// is finished. IndexedDB.jsm:101:46
//
// Assuming that this error is caused by a timing issue while
// accessing the store concurrently, we simply try to circumvent this by
// reloading ourselves
console.debug("Caught exception while reading settings. Reloading extension.", error);
browser.runtime.reload();
}
browser.tabs.onCreated.addListener(async (tab) => this.tabCreated(tab));
}
async tabCreated(tab) {
if(tab.type != 'messageCompose') {
return;
}
let composeDetails = await browser.compose.getComposeDetails(tab.id);
if(composeDetails.type == "new") {
var isEnabledComposeMessage =
await this.icOptions.isEnabledComposeMessage();
if(!isEnabledComposeMessage) {
return;
}
}
if(composeDetails.type == "reply") {
var isEnabledReplyMessage =
await this.icOptions.isEnabledReplyMessage();
if(!isEnabledReplyMessage) {
return;
}
}
if(composeDetails.type == "forward") {
var isEnabledForwardMessage =
await this.icOptions.isEnabledForwardMessage();
if(!isEnabledForwardMessage) {
return;
}
}
if(composeDetails.type == "draft") {
var isEnabledDraftMessage =
await this.icOptions.isEnabledDraftMessage();
if(!isEnabledDraftMessage) {
return;
}
}
let composeWindow = await browser.windows.get(tab.windowId);
const popUrl = browser.runtime.getURL("identitypopup/popup.html");
let identityWindow = await browser.windows.create({
url: popUrl,
type: "popup",
height: composeWindow.height - 200,
width: composeWindow.width - 200,
left: composeWindow.left + 100,
top: composeWindow.top +100,
allowScriptsToClose: true,
});
let idFocusListener = async (windowId) => this.focusChanged(windowId,
identityWindow.id);
let idRemovedListener = async (windowId) => this.windowRemoved(windowId,
identityWindow.id);
let activeIdentityWindow = {
identityWindowId: identityWindow.id,
composeWindowId: tab.windowId,
removedListener: idRemovedListener,
focusListener: idFocusListener,
};
this.activeIdentityWindows.push(activeIdentityWindow);
browser.windows.onFocusChanged.addListener(
activeIdentityWindow.focusListener);
browser.windows.onRemoved.addListener(
activeIdentityWindow.removedListener);
let chosenIdentity = await this.popupPrompt(identityWindow.id, null);
if(chosenIdentity == null || chosenIdentity == "cancel") {
browser.windows.remove(composeWindow.id);
} else if(chosenIdentity != null) {
browser.compose.setComposeDetails(tab.id, { identityId: chosenIdentity });
}
}
async windowRemoved(windowId, identityWindowId) {
let identityWindow = this.activeIdentityWindows.find(e => e.identityWindowId == identityWindowId);
let composeWindowId = identityWindow.composeWindowId;
if(windowId == identityWindowId) {
// identity window closed
browser.windows.onFocusChanged.removeListener(identityWindow.focusListener);
browser.windows.onRemoved.removeListener(identityWindow.removedListener);
let idx = this.activeIdentityWindows.findIndex(e => e.identityWindowId == identityWindowId);
this.activeIdentityWindows.splice(idx, 1);
}
if(windowId == composeWindowId) {
// composer window closed
browser.windows.remove(identityWindowId);
}
}
async focusChanged(windowId, identityWindowId) {
let identityWindow = this.activeIdentityWindows.find(e => e.identityWindowId == identityWindowId);
let composeWindowId = identityWindow.composeWindowId;
if(windowId == composeWindowId) {
browser.windows.update(identityWindowId, { focused: true });
}
}
async popupPrompt(popupId, defaultResponse) {
try {
await messenger.windows.get(popupId);
} catch (e) {
// Window does not exist, assume closed.
return defaultResponse;
}
return new Promise(resolve => {
let response = defaultResponse;
function windowRemoveListener(closedId) {
if (popupId == closedId) {
messenger.windows.onRemoved.removeListener(windowRemoveListener);
messenger.runtime.onMessage.removeListener(messageListener);
resolve(response);
}
}
function messageListener(request, sender, sendResponse) {
if (sender.tab.windowId != popupId || !request) {
return;
}
if (request.popupResponse) {
response = request.popupResponse;
}
}
messenger.runtime.onMessage.addListener(messageListener);
messenger.windows.onRemoved.addListener(windowRemoveListener);
});
}
}
browser.runtime.onInstalled.addListener(({ reason, previousVersion }) => {
if (reason == "update" /* && previousVersion?.startsWith("3.") */) {
browser.tabs.create({ url: "/onboarding/changes.html" });
} else if (reason == "install") {
browser.tabs.create({ url: "/onboarding/onboarding.html" });
}
});
async function waitForLoad() {
let onCreate = new Promise(function(resolve, reject) {
function listener() {
browser.windows.onCreated.removeListener(listener);
resolve(true);
}
browser.windows.onCreated.addListener(listener);
});
let windows = await browser.windows.getAll({windowTypes:["normal"]});
if (windows.length > 0) {
return false;
} else {
return onCreate;
}
}
// self-executing async "main" function
(async () => {
await waitForLoad();
var identityChooser = new IdentityChooser();
waitForLoad().then((isAppStartup) => identityChooser.run());
})()