Outlook加载项On send功能未调用validateBody函数问题求解
修复方案
1. 将validateBody函数挂载到全局作用域
Office调用On-send指定的函数时,只会在window全局作用域查找,你当前定义的validateBody是局部函数,全局作用域访问不到,所以不会被触发。修改command.js里的validateBody定义:
// 替换原有validateBody函数定义 window.validateBody = function(event) { mailboxItem.body.getAsync("html", { asyncContext: event }, checkBodyOnlyOnSendCallBack); }
2. 修复回调函数逻辑漏洞,避免重复调用completed方法
当前checkBodyOnlyOnSendCallBack函数命中违禁词调用completed阻止发送后,没有终止代码执行,会继续往下跑允许发送的completed逻辑,重复调用completed方法会直接触发Office报错。修改后的代码如下:
async function checkBodyOnlyOnSendCallBack(asyncResult) { const listOfBlockedWords = new Array("dammit", "porra", "caralho"); const wordExpression = listOfBlockedWords.join("|"); const regexCheck = new RegExp("\\b(" + wordExpression + ")\\b", "i"); const checkBody = regexCheck.test(asyncResult.value); if (checkBody) { mailboxItem.notificationMessages.addAsync("NoSend", { type: "errorMessage", message: "Blocked words have been found in the body of this email. Please remove them.", }); asyncResult.asyncContext.completed({ allowEvent: false }); // 新增return终止后续代码执行 return; } asyncResult.asyncContext.completed({ allowEvent: true }); }
3. 补全manifest的必要配置
- 首先删除Event标签里的
FunctionExecution="synchronous"属性,该属性属于废弃配置,新版Office对该属性存在兼容问题,修改后为:
<Event Type="ItemSend" FunctionName="validateBody" />
- 确认manifest中已配置最低要求集和对应权限,否则On-send功能无法正常触发:
<!-- 新增要求集配置,放在manifest根节点下 --> <Requirements> <Sets DefaultMinVersion="1.8"> <Set Name="Mailbox" MinVersion="1.8" /> </Sets> </Requirements> <!-- 确认权限配置为读写邮箱 --> <Permissions>ReadWriteMailbox</Permissions>
4. 检查本地资源加载有效性
- 确认
https://localhost:3000/commands.html可以正常在Edge浏览器中访问,没有证书报错。如果是自签证书,需要导入到系统「受信任的根证书颁发机构」中,否则Outlook会拦截资源加载。 - 确认
commands.html中引入顺序正确:必须先引入office.js,再引入command.js,示例如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script> <script src="command.js"></script> </head> <body> </body> </html>
内容的提问来源于stack exchange,提问作者Rogério Ferreira de Melo
相关产品推荐
相关产品推荐

