You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何捕获Outlook邮件正文中带自定义文本的超链接?

解决Outlook加载项捕获带自定义文本超链接的问题

当前代码使用"text"格式获取邮件正文,这种模式下只会返回超链接的显示文本(比如"click me"),无法提取对应的URL。要同时获取超链接的文本和地址,需要改用"html"格式获取正文,再解析HTML内容。

修改后的代码示例

Office.context.mailbox.item.body.getAsync(
    "html",
    function (result) {
        if (result.status === Office.AsyncResultStatus.Succeeded) {
            const htmlBody = result.value;
            // 创建临时DOM元素解析HTML
            const tempDiv = document.createElement('div');
            tempDiv.innerHTML = htmlBody;
            
            // 提取所有超链接
            const links = tempDiv.querySelectorAll('a');
            const linkInfo = Array.from(links).map(link => ({
                text: link.textContent,
                url: link.href
            }));
            
            // 将结果展示到页面,可根据需求调整格式
            let displayText = '';
            linkInfo.forEach(link => {
                displayText += `链接文本:${link.text},URL:${link.url}\n`;
            });
            $('#item-Content').text(displayText);
        }
    }
);

关键说明

  • 改用"html"格式获取正文,能保留完整的HTML结构,包括超链接的href属性
  • 通过临时DOM元素解析HTML,可安全提取所有<a>标签的文本和URL
  • 可根据实际需求调整结果展示方式,比如保留HTML格式显示,或只提取特定超链接

内容的提问来源于stack exchange,提问作者Pearl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 09:13:03