Skip to content

Commit 844f418

Browse files
committed
Also save account information when selecting folders
Fixes: #1
1 parent 8eaf33c commit 844f418

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

background.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ async function onCommand(command) {
88
console.log("No folder setting found");
99
return;
1010
}
11-
const path = setting.folders[folderIndex].value;
12-
console.log(`Got saved folder value: ${path}`);
11+
const folderSetting = setting.folders[folderIndex].value;
12+
console.log(`Got saved folder value: ${folderSetting}`);
1313

1414
const tabs = await browser.tabs.query({
1515
active: true,
@@ -21,18 +21,26 @@ async function onCommand(command) {
2121
console.log("No message selected");
2222
return;
2323
}
24-
const folder = await findFolder(path);
24+
const folder = await findFolder(folderSetting);
2525
if (!folder) {
2626
console.log(`Folder ${setting.folder} not found`);
2727
return;
2828
}
2929
await browser.messages.move([message.id], folder);
30-
console.log("Success");
3130
}
3231

33-
async function findFolder(path) {
34-
const accounts = await browser.accounts.list();
35-
return findSubfolder(path, accounts.flatMap(account => account.folders));
32+
async function findFolder(folderSetting) {
33+
try {
34+
const setting = JSON.parse(folderSetting);
35+
if (setting.accountId) {
36+
const account = await browser.accounts.get(setting.accountId);
37+
return findSubfolder(setting.folderPath, account.folders);
38+
}
39+
} catch (ex) {
40+
// Saved with previous version which only contains the folder name
41+
const accounts = await browser.accounts.list();
42+
return findSubfolder(folderSetting, accounts.flatMap(account => account.folders));
43+
}
3644
}
3745

3846
function findSubfolder(path, list) {

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Move Mail Shortcuts",
33
"description": "Allows to assign keyboard shortcuts for moving mails to folders.",
4-
"version": "1",
4+
"version": "2",
55
"applications": {
66
"gecko": {
77
"id": "mailMoveShortcuts@extension",

options.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ function saveOptions(e) {
1111
}
1212

1313
function restoreOptions(idx, folderSelect) {
14-
function loadFolders(folderList, parent, prefix = "") {
14+
function loadFolders(account, folderList, parent, prefix = "") {
1515
if (!folderList)
1616
return;
1717
folderList.forEach(folder => {
1818
const f = document.createElement("option");
1919
f.appendChild(document.createTextNode(prefix + folder.name));
20-
var attr = document.createAttribute("value");
21-
attr.value = folder.path;
22-
f.setAttributeNode(attr);
23-
loadFolders(folder.subFolders, parent, prefix + "\xa0");
20+
const valueAttr = document.createAttribute("value");
21+
valueAttr.value = JSON.stringify({ folderPath: folder.path, accountId: account.id });
22+
f.setAttributeNode(valueAttr);
23+
loadFolders(account, folder.subFolders, parent, prefix + "\xa0");
2424
parent.appendChild(f);
2525
});
2626
}
@@ -30,7 +30,7 @@ function restoreOptions(idx, folderSelect) {
3030
var attr = document.createAttribute("label");
3131
attr.value = account.name;
3232
a.setAttributeNode(attr);
33-
loadFolders(account.folders, a);
33+
loadFolders(account, account.folders, a);
3434
folderSelect.appendChild(a);
3535
});
3636
}
@@ -39,7 +39,7 @@ function restoreOptions(idx, folderSelect) {
3939
browser.storage.sync.get("folders")
4040
.then(v => {
4141
if (v.folders[idx]) {
42-
console.log(v);
42+
console.log(v.folders[idx]);
4343
folderSelect.value = v.folders[idx].value;
4444
}
4545
} );

0 commit comments

Comments
 (0)