The author of this answer had exactly the same problem.
He wanted to filter out spam messages with a certain content within the body of the emails.
Therefore he decided to test his regexps on emails which were already in the trash folder.
And he read the exact same documentation as you did and ended up in not knowing how to pass the "folder" object corretly to the .messages.list(folder)
method.
The first "aha" experience was the following reading:
"WebExtension APIs are asynchronous, [... and] return a Promise object [...]".
Trying to read and deeply understand the following excellent explanation abot using Promise objects (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) helped to solve the authors problem with the following first [and mostly quick and dirty] code.
Supposed you just know the "name" of the account, you want to examine specific folders of, you can try the following code, which did it for the author of this answer:
function accountsList_successCallback( arrayOfMailAccount ) {
let accountId = false;
for (i = 0; i < arrayOfMailAccount.length; i++) {
if ( arrayOfMailAccount[i][ "name" ] == "firstname.lastname@email.com" ) {
accountId = arrayOfMailAccount[i][ "id" ];
break;
}
}
console.log('Last line of function accountsList_successCallback() before return of accountId: ' + accountId );
return accountId;
}
function mailAccount_successCallback( mailAccount ) {
console.log('Last line of function mailAccount_successCallback(), returning mailAccount\'s MailFolder array');
return mailAccount[ "folders" ];
}
function returnWantedMailFolder( Folders ) {
let mailFolder = false;
for (j = 0; j < Folders.length; j++) {
if ( Folders[j][ "type" ] == "trash" ) { // use "inbox" here instead of "trash"
mailFolder = Folders[j];
break;
}
}
console.log('Last line of function returnWantedMailFolder()');
return mailFolder;
}
function messagesList_successCallback( messageList ) {
console.log( "messageListId: " + messageList[ "id" ] );
console.log( "Number of Emails in this Page: " + messageList[ "messages" ].length );
/*
* This is where you can place your messages examining
* rotines...
*
* And don't forget to loop through the next pages.
* You just got the first of maybe several pages.
*
*/
console.log('Last line of function messagesList_successCallback()');
}
function accountsList_failureCallback(error) {
console.error( "Fehler (accountsList_failureCallback) : " + error);
}
function accountsGet_failureCallback(error) {
console.error( "Fehler (accountsGet_failureCallback) : " + error);
}
function mailAccount_failureCallback(error) {
console.error( "Fehler (mailAccount_failureCallback) : " + error);
}
function returnWantedMailFolder_failureCallback(error) {
console.error( "Fehler (returnWantedMailFolder_failureCallback) : " + error);
}
function messagesList_failureCallback(error) {
console.error( "Fehler (messagesList_failureCallback) : " + error);
}
browser.accounts.list()
.then( accountsList_successCallback,
accountsList_failureCallback) // after .list() is fulfilled...
// accountsList_successCallback is called, which
// in this example returns the accountId string...
.then( accountId => browser.accounts.get( accountId ),
accountsGet_failureCallback) // returns a MailAccount Promise, passed to ...
.then( mailAccount_successCallback,
mailAccount_failureCallback) // returns an array of MailFolder...
.then( arrayMailFolders => returnWantedMailFolder( arrayMailFolders ),
returnWantedMailFolder_failureCallback)
.then( mailFolder => browser.messages.list( mailFolder ), // <-- this was the problem, right?
messagesList_failureCallback) // returns a page Promise, passed to ...
.then( messagesList_successCallback,
messagesList_failureCallback)
;
This code can FOR SURE and will be further optimized. Some of the "failureCallback" routines will probably never been called. And with a single .catch( failureCallback )
at the end of the .then()
chain you can eliminate all other failureCallback functions.
This many failureCallback functions were necessary til the author of this answer checked, what is really going on within this "new" kind of thinking.
The author of this answer wishes you good luck. richard.