forked from eykamp/Folder-Account
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
executable file
·88 lines (73 loc) · 2.15 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
import * as folderAccount from "./folderAccount.mjs";
await folderAccount.checkForMigration();
let lastFocusedWindow = messenger.windows.WINDOW_ID_NONE;
messenger.windows.getCurrent().then((w) => (lastFocusedWindow = w.id));
messenger.windows.onFocusChanged.addListener(async (windowId) => {
if (windowId < 0) {
return;
}
let focusedWindow = await messenger.windows.get(windowId);
if (focusedWindow?.type == "messageCompose") {
return;
}
lastFocusedWindow = windowId;
});
messenger.folders.onRenamed.addListener(
async (originalFolder, renamedFolder) => {
await folderAccount.updateSettings(originalFolder, renamedFolder);
}
);
const menuId = "folderAccount";
messenger.menus.create({
contexts: ["folder_pane"],
title: "Folder Account",
id: menuId,
});
messenger.menus.onShown.addListener((info, tab) => {
if (!info.contexts.includes("folder_pane")) {
return;
}
messenger.menus.update(menuId, {
visible: Boolean(info.selectedFolder),
});
messenger.menus.refresh();
});
messenger.menus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId != menuId) {
return;
}
const folder = info.selectedFolder;
const params = new URLSearchParams({
id: folder.id,
});
const { windowSize } = await browser.storage.local.get({
windowSize: { height: 400, width: 600 },
});
messenger.windows.create({
type: "popup",
url: `folderSettings.html?${params}`,
allowScriptsToClose: true,
height: windowSize.height,
width: windowSize.width,
});
});
messenger.windows.onCreated.addListener(async (window) => {
if (window.type != "messageCompose") {
return;
}
let [currentTab] = await messenger.tabs.query({ windowId: window.id });
let [lastFocusedMailTab] = await messenger.tabs.query({
active: true,
mailTab: true,
windowId: lastFocusedWindow,
});
if (!currentTab || !lastFocusedMailTab) {
return;
}
const details = await messenger.compose.getComposeDetails(currentTab.id);
const customDetails = await folderAccount.getCustomComposeDetails(
details,
lastFocusedMailTab.id
);
await messenger.compose.setComposeDetails(currentTab.id, customDetails);
});