从邮件正文中检索URL的最佳方法

时间:2016-05-25 13:36:52

标签: javascript office365 outlook-addin office-addins office-js

以下一行是在Mac客户端上使加载项无响应。如果我们删除此行并直接执行函数someFunction,则加载项在mac客户端上工作正常。

  

Office.context.mailbox.item.body.getAsync(" html",someFunction);

我们使用body.getAsync()因为我们需要通过处理html和那些包含特定ID的网址来提取邮件正文中的所有网址。

尝试使用以下内容但未提供预期的URL。

  

var links = Office.context.mailbox.item.getEntities()。urls;

我也在尝试以下

Office.initialize = function (reason) {
    $(document).ready(function () {
        app.initialize();
        Office.context.mailbox.item.body.getAsync("html", processHtmlBody);
    });
};

function processHtmlBody(asyncResult) {
    var htmlParser = new DOMParser().parseFromString(asyncResult.value, "text/html");
    var links = htmlParser.getElementsByTagName("a");       
}

是否有更好的替代方法从邮件正文中获取网址。

3 个答案:

答案 0 :(得分:1)

请注意,getAsync是1.3邮箱要求集的一部分,Outlook for Mac目前不支持:

https://dev.outlook.com/reference/add-ins/tutorial-api-requirement-sets.html

否则使用实体是您唯一的选择,但getElementsByTagName可能效果最佳(如果您有权访问电子邮件正文)。

答案 1 :(得分:0)

我们尝试了使用makeEwsRequestAsync()getEntities().urls的备选方案/解决方法,以便从Mac客户端上的邮件正文中检索网址。但两者都没有成功:

  • makeEwsRequestAsync():"没有数据错误"。
  • getEntities().urls:未提供预期的结果/网址

因此,对于Mac客户端,我们最终更改了逻辑并添加了条件逻辑,其中不包含任何代码来检索URL。

  

if(Office.context.requirements.isSetSupported(" mailbox",1.3))
  {
       //条件代码

     

}

答案 2 :(得分:0)

我遇到了同样的问题,我以类似的方式解决了这个问题:

return new Promise((resolve, reject) => {
    mailbox.item.body.getAsync(window.Office.CoercionType.Html, asyncResult => {
        if (asyncResult.status == window.Office.AsyncResultStatus.Succeeded) {
            var $dom = $(asyncResult.value);
            var url = $dom.find('#x_hiddenURL').text();

            resolve(url);
        } else {
            reject(new Error('Failed to load email body'));
        }
   });
});