Skip to content

Commit

Permalink
Merge pull request #6 from memeller/dev
Browse files Browse the repository at this point in the history
fix bug where event listener might not be added
  • Loading branch information
memeller authored Mar 26, 2024
2 parents 8556e3d + 82e6892 commit fa552d0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
66 changes: 54 additions & 12 deletions src/entry/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,38 @@ function onOptionsLoaded(options,onChanged=false) {
selectedFolders = getValueFromStorageObj(options,"selectedKeys",selectedFolders);
if(options.useFolderInfoEvent!==undefined)
{
useFolderInfoEvent = getValueFromStorageObj(options,"useFolderInfoEvent",useFolderInfoEvent);
if (useFolderInfoEvent) {
if(logConsole)
console.debug("MarkAsRead: Using folder info event for new mail tracking");
browser.folders.onFolderInfoChanged.addListener(folderInfoChanged);
browser.messages.onNewMailReceived.removeListener(messageReceivedListener);
} else {
if(logConsole)
console.debug("MarkAsRead: Using new mail received event for new mail tracking");
browser.messages.onNewMailReceived.addListener(messageReceivedListener);
browser.folders.onFolderInfoChanged.removeListener(folderInfoChanged);
}
let useFolderInfoEventNewValue = getValueFromStorageObj(options,"useFolderInfoEvent",useFolderInfoEvent);
if(useFolderInfoEvent!=useFolderInfoEventNewValue)
{
useFolderInfoEvent=useFolderInfoEventNewValue;
setEventHandlers();
}
}
if(isStartup)
{
isStartup=false;
setEventHandlers();
if(logConsole)
console.debug("MarkAsRead: Scanning folders at startup");
scanAndMarkAsRead(selectedFolders);
}
}
function setEventHandlers()
{
if (useFolderInfoEvent) {
if(logConsole)
console.debug("MarkAsRead: Using folder info event for new mail tracking");
browser.folders.onFolderInfoChanged.addListener(folderInfoChanged);
browser.messages.onNewMailReceived.removeListener(messageReceivedListener);
browser.messages.onMoved.removeListener(messageMovedListener);
} else {
if(logConsole)
console.debug("MarkAsRead: Using new mail received event for new mail tracking");
browser.messages.onNewMailReceived.addListener(messageReceivedListener);
browser.folders.onFolderInfoChanged.removeListener(folderInfoChanged);
browser.messages.onMoved.addListener(messageMovedListener);
}
}
function getValueFromStorageObj(storageObj,key,defaultValue)
{
if(storageObj[key]===undefined)
Expand All @@ -80,7 +91,38 @@ function folderInfoChanged(folder, folderInfo) {
console.debug(`MarkAsRead: Folder info change detected, checking if anything should be marked as read`);
markAsReadFolderData(folder, selectedFolders);
}
async function messageMovedListener(oldMessages, movedMessages) {
if(logConsole)
console.debug(`MarkAsRead: MessagemovedEvent`);
movedMessages.forEach(message => {
if(logConsole)
console.log(message);
//folder is optional in messageHeader
if(message.folder===undefined)
{
return;
}
else
{
let folder=message.folder;
let tempId = md5(folder.accountId + folder.path);
if(logConsole)
console.debug(`MarkAsRead: New message detected, tempId: ${tempId}`);
if (selectedFolders.includes(tempId)) {
if(logConsole)
console.debug(`MarkAsRead: Message belongs to folder that is marked`);
if(logConsole)
console.debug(`MarkAsRead: Marking message ${message.id} as read`);
browser.messages.update(message.id, { read: true });

}
}
});

}
async function messageReceivedListener(folder, messages) {
if(logConsole)
console.debug(`MarkAsRead: messageReceivedListener`);
let tempId = md5(folder.accountId + folder.path);
if(logConsole)
console.debug(`MarkAsRead: New message detected, tempId: ${tempId}`);
Expand Down
5 changes: 3 additions & 2 deletions src/js/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function scanAndMarkAsRead(selectedFolders) {

export function markAsReadFolderData(folderData, markAsReadIds) {
if(logConsole)
console.debug(`MarkAsRead: mark as read folder data`)
console.debug(`MarkAsRead: markAsReadFolderData`)
if (Array.isArray(folderData)) {
folderData.forEach((folder) => {
checkFolderAndMark(folder, markAsReadIds);
Expand All @@ -25,8 +25,9 @@ export function markAsReadFolderData(folderData, markAsReadIds) {
}
export function setDebug(isDebug)
{
console.debug("tools setDebug: "+isDebug);
logConsole=isDebug;
if(logConsole)
console.debug("MarkAsRead: tools setDebug: "+isDebug);
}
function checkFolderAndMark(folder, markAsReadIds) {
let id = md5(folder.accountId + folder.path);
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.development.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}
},
"description": "Automatically mark incoming messages as read if they are in one of selected folders. An attempt to revive & enhance 'Mark Junk Read' by Alex Cabal",
"version": "1.0.6",
"version": "1.0.8",
"background": {
"scripts": [
"/background.js"
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.production.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}
},
"description": "Automatically mark incoming messages as read if they are in one of selected folders. An attempt to revive & enhance 'Mark Junk Read' by Alex Cabal",
"version": "1.0.6",
"version": "1.0.8",
"background": {
"scripts": [
"/background.js"
Expand Down

0 comments on commit fa552d0

Please sign in to comment.