Get entered document password

Q: How can I capture the password entered by the user when opening a document?

You will need to add an event listener to change events on the password input DOM element rendered by PSPDFKit when a document is password-protected and an input is needed.

Since the PSPDFKit.Instance reference doesn’t resolve until a password is used and the document finally opened, you can rely on a MutationObserver to act upon the rendering of the main PSPDFKit iframe and then add another one to capture a reference to the password input presented. All of this before the call to PSPDFKit.load.

let passwordEntered = "";

const config = {
childList: true,
subtree: true
};

const observer = new MutationObserver((_, observer) => {
const mainIframe = defaultConfiguration.container.querySelector(
    'iframe[title="PSPDFKit"]'
);
if (mainIframe) {
    const innerObserver = new MutationObserver((_, observer) => {
    const passwordInput = mainIframe.contentDocument.querySelector(
        'input[name="password"]'
    );
    if (passwordInput) {
        passwordInput.addEventListener("change", e => {
        passwordEntered = e.target.value;
        });
        observer.disconnect();
    }
    });
    innerObserver.observe(mainIframe.contentDocument, config);

    observer.disconnect();
}
});

observer.observe(defaultConfiguration.container, config);

PSPDFKit.load(defaultConfiguration);

This has been tested with PSPDFKit for Web 2021.2.0.