Thunderbird Webextensions Plug-In get a messageList

假装没事ソ 提交于 2021-02-20 05:03:51

问题


I already tried different things to get a list of mails from my inbox folder in Thunderbird.

let page = await browser.messages.list(folder);

But how to declare folder?

MailFolder is explained in Thunderbird Docs, but how do i get

accountId (string) The account this folder belongs to.
path (string) Path to this folder in the account. Although paths look predictable, never guess a folder’s path, as there are a number of reasons why it may not be what you think it is.
[name] (string) The human-friendly name of this folder.
[type] (string) The type of folder, for several common types.

?

Another way i found is to use query(queryInfo). Documentation explains:

Gets all messages that have the specified properties, or all messages if no properties are specified.

But when I pass no argument, i get an exception.

Can someone provide me a piece of code which just assigns my inbox folder to a messageList object?


回答1:


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.




回答2:


As promised, here the optimized code, which recursively "loops" asynchronusly one-after-the-other through all the MessageList "page" objects:

emailAccountName = "firstname.lastname@email.com";
wantedMailFolder = "inbox";
msgCnt = 0;

function accountsList_successCallback( arrayOfMailAccount ) {
    let accountId = false;
    for (let i = 0; i < arrayOfMailAccount.length; i++) {
    if ( arrayOfMailAccount[i].name == emailAccountName ) {
        accountId  = arrayOfMailAccount[i].id;
        break;
        }
    }
    console.log('Last line of accountsList_successCallback() before return of accountId: ' + accountId );
    return accountId;
}
function mailAccount_successCallback( mailAccount ) {
    let mailFolder = false;
    for (let i = 0; i < mailAccount.folders.length; i++) {
    if ( mailAccount.folders[i].type == wantedMailFolder ) {
        mailFolder = mailAccount.folders[i];
        break;
        }
    }
    console.log('Last line of mailAccount_successCallback(), returning mailAccount\'s MailFolder');
    return mailFolder;
}
function messagesList_successCallback( messageList ) {
    /***********************************************************
      (recursively) called to process the given "page" Promise
    ************************************************************/
    // console.log( "Number of Emails in this Page: " + messageList.messages.length );

    if ( messageList.messages.length ) {
    // iterating through messageList.messages
    for ( let i =0; i < messageList.messages.length; i++, msgCnt++) {
        /*
         * This is where you can place your message examining code.
         * For example a numbered list of all Subjects:
         *
         console.log( "Subject "
                      + ( "00000" + (msgCnt+1) ).slice(-5)
                      + ": "
                      + messageList.messages[ i ].subject );
         *
         *
         */
        }
    }
    if ( messageList.id ) {
        // continue with the NEXT "page" Promise
        browser.messages.continueList( messageList.id )
            .then( messagesList_successCallback, finalCatch );
    } else {
        // this was the LAST fulfilled "page" Promise,
        // we already acted upon
        console.log('Last return from messagesList_successCallback() with msgCnt == ' + msgCnt);
    }
}

browser.accounts.list()
    .then( accountsList_successCallback)  // after .accounts.list() Promise is fulfilled...
                                          // accountsList_successCallback is called, which
                                          // in this example returns the accountId string...
    .then( accountId => browser.accounts.get( accountId ))
    .then( mailAccount_successCallback)   // after .accounts.get() Promise is fulfilled...
                                          // mailAccount_successCallback is called, which
                                          // returns the wanted MailFolder...
    .then( mailFolder => browser.messages.list( mailFolder ))
    .then( messagesList_successCallback)  // after .messages.list() Promise is fulfilled...
                                          // messagesList_successCallback is called
                                          // with the FIRST "page" Promise, a MessageList
    .catch( finalCatch )
;

function finalCatch( error ) {
    console.error( "ERROR passed to finalCatch() : " + error);
}

Hope this helps. richard



来源:https://stackoverflow.com/questions/59932868/thunderbird-webextensions-plug-in-get-a-messagelist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!