Viewer messages

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

Security

First a little note about security.

Improperly implemented message listeners could open your site up to cross-site
scripting (XSS) attacks.

To mitigate risk, you should check the origin of the incoming message and ensure that you properly
santize any data that you may pass to your server as a result of the message.

See here
for more information.

Messages

Message Description
scpDialogPrintShow Print dialog was displayed
scpDialogPrintApprove User launched browser print dialog
scpDialogPrintDeny User cancelled printing
scpOnBeforePrint Triggered before the browser displays its print dialog
scpOnAfterPrint Triggered after the browser print dialog has been closed

Examples

Listening for messages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const Messages = {
DIALOG_PRINT_SHOW: 'scpDialogPrintShow',
DIALOG_PRINT_APPROVE: 'scpDialogPrintApprove',
DIALOG_PRINT_DENY: 'scpDialogPrintDeny',
ON_BEFORE_PRINT: 'scpOnBeforePrint',
ON_AFTER_PRINT: 'scpOnAfterPrint',
};

// 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 reading