Dialog API 的 messageParent 不使用 OWA

Cod*_*eve 5 office-js outlook-web-addins

我正在将 Office Javascript API 用于我的 Outlook 加载项。我正在运行 Outlook-web-16.01 ( https://outlook.live.com/owa/)。

我可以通过调用UI.displayDialogAsync. 但是调用UI.messageParent不会导致在DialogMessageReceived父页面上被触发。我在同一个域上同时运行父级和对话框。

然而,手动关闭对话框会DialogEventReceived在父级上触发并带有12006.

我还注意到对话框在加载后立即在 JS 控制台上出现此错误,但不确定它是否相关:

无法执行'postMessage''DOMWindow':提供的目标来源('https://outlook.live.com')不匹配收件人窗口的原点('MyOrigin')。

我正在使用GitHub 中OfficeDev 示例

我在从这里复制的父页面上启动对话框的代码,

function dialogCallback(asyncResult) {
  if (asyncResult.status == "failed") {

      // In addition to general system errors, there are 3 specific errors for 
      // displayDialogAsync that you can handle individually.
      switch (asyncResult.error.code) {
          case 12004:
              console.log("Domain is not trusted");
              break;
          case 12005:
              console.log("HTTPS is required");
              break;
          case 12007:
              console.log("A dialog is already opened.");
              break;
          default:
              console.log(asyncResult.error.message);
              break;
      }
  }
  else {
      dialog = asyncResult.value;
      /*Messages are sent by developers programatically from the dialog using office.context.ui.messageParent(...)*/
      dialog.addEventHandler(Microsoft.Office.WebExtension.EventType.DialogMessageReceived, messageHandler);

      /*Events are sent by the platform in response to user actions or errors. For example, the dialog is closed via the 'x' button*/
      dialog.addEventHandler(Microsoft.Office.WebExtension.EventType.DialogEventReceived, eventHandler);

      console.log(`handler registered`);
  }
}

function messageHandler(arg) {
    console.log(`handler called`);
    dialog.close();
    console.log(arg.message);
}

function eventHandler(arg) {

    // In addition to general system errors, there are 2 specific errors 
    // and one event that you can handle individually.
    switch (arg.error) {
        case 12002:
            console.log("Cannot load URL, no such page or bad URL syntax.");
            break;
        case 12003:
            console.log("HTTPS is required.");
            break;
        case 12006:
            // The dialog was closed, typically because the user the pressed X button.
            console.log("Dialog closed by user");
            break;
        default:
            console.log("Undefined error in dialog window");
            break;
    }
}

  var dialogUrl = 'MyOrigin/outlook/function-file/dialog.html';
  Office.context.ui.displayDialogAsync(dialogUrl, { height: 50, width: 50 }, dialogCallback);
Run Code Online (Sandbox Code Playgroud)

对于对话框 HTML,我正在执行与此代码完全相同的操作

Ale*_*lov 1

在调试 Microsoft Office 库后我发现了这个问题。为了使消息传递正常工作,清单文件中的 URL 应以 https:// 为前缀

一旦我更新了清单,一切就开始工作了。

只需确保 AppDomains 中的 URL 带有 https:// 前缀即可

  <AppDomains>
    <AppDomain>https://app.polarcrm.com</AppDomain>
    <AppDomain>https://localhost:44321</AppDomain>
  </AppDomains>
Run Code Online (Sandbox Code Playgroud)

这是我的实现细节: https://yaplex.com/blog/office-addin-messageparent-not-working

在此输入图像描述