【问题标题】:Thunderbird Webextensions Plug-In get a messageListThunderbird Webextensions 插件获取消息列表
【发布时间】:2020-05-12 22:20:35
【问题描述】:

我已经尝试了不同的方法来从 Thunderbird 的收件箱文件夹中获取邮件列表。

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

但是如何声明folder

MailFolder 在Thunderbird Docs 中有解释,但我如何获得

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.

?

我发现的另一种方法是使用query(queryInfo)。文档说明:

获取所有具有指定属性的消息,如果没有指定属性,则获取所有消息。

但是当我不传递任何参数时,我得到一个异常。

谁能给我一段代码,将我的收件箱文件夹分配给一个 messageList 对象?

【问题讨论】:

    标签: javascript thunderbird-webextensions


    【解决方案1】:

    此答案的作者遇到了完全相同的问题。 他想过滤掉邮件正文中包含特定内容的垃圾邮件。 因此,他决定在垃圾文件夹中的电子邮件上测试他的正则表达式。 他阅读了与您完全相同的文档,最终不知道如何将“文件夹”对象正确地传递给 .messages.list(folder) 方法。

    第一次“啊哈”的体验是以下阅读: “WebExtension API 是异步,[...并且]返回一个Promise对象[...]”。

    尝试阅读并深入理解以下使用 Promise 对象 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) 的出色解释有助于解决作者使用以下第一个 [而且大多是快速而肮脏的] 代码的问题。

    假设您只知道帐户的“名称”,想要检查特定文件夹,您可以尝试以下代码,该代码为该答案的作者完成:

    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)
    ;
    
    
    

    此代码可以肯定并且会进一步优化。一些“failureCallback”例程可能永远不会被调用。在 .then() 链的末尾使用单个 .catch( failureCallback ),您可以消除所有其他 failureCallback 函数。

    在这个答案的作者检查之前,需要这么多的 failureCallback 函数,在这种“新”思维中到底发生了什么。

    此答案的作者祝您好运。理查德。

    【讨论】:

    • 只是为了澄清 - 是这个答案的作者吗?这有点不清楚。如果你不是,你应该提到他们的名字。
    • 好吧,有点刺激。
    【解决方案2】:

    正如所承诺的,这里是优化的代码,它通过所有 MessageList“页面”对象递归地一个接一个地异步“循环”:

    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);
    }
    

    希望这会有所帮助。理查德

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 2012-07-07
      • 1970-01-01
      相关资源
      最近更新 更多