Skip to content

Viewer messages

The viewer emits messages triggered by user actions, allowing you to customise the experience that your site provides in response.

The viewer emits messages to the parent window using the window.postMessage API. You can listen for these messages by adding an event listener to the window object.

Messages

MessageDescription
scpDialogPrintShowPrint dialog was displayed
scpDialogPrintApproveUser launched browser print dialog
scpDialogPrintDenyUser cancelled printing
scpOnBeforePrintTriggered before the browser displays its print dialog
scpOnAfterPrintTriggered after the browser print dialog has been closed
scpOnDownloadClickTriggered when the download button is clicked

Examples

Listening for messages

This example demonstrates how to listen for messages from the viewer. On receipt of a message, a log message is written to the console.

js
const Messages = {
  DIALOG_PRINT_SHOW: "scpDialogPrintShow",
  DIALOG_PRINT_APPROVE: "scpDialogPrintApprove",
  DIALOG_PRINT_DENY: "scpDialogPrintDeny",
  ON_BEFORE_PRINT: "scpOnBeforePrint",
  ON_AFTER_PRINT: "scpOnAfterPrint",
  ON_DOWNLOAD_CLICK: "scpOnDownloadClick",
};

// Add event listener for message event
window.addEventListener(
  "message",
  (event) => {
    switch (event.data) {
      case Messages.DIALOG_PRINT_SHOW:
        console.log("SCP print dialog shown");
        break;
      case Messages.DIALOG_PRINT_APPROVE:
        console.log("SCP print dialog approved");
        break;
      default:
        console.warn(`Unhandled message ${event.data}`);
        break;
    }
  },
  false,
);

Further information