Get the Entered Document Password

To capture the password entered by the user when opening a document, 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 is finally opened, you can rely on one MutationObserver to act upon the rendering of the main PSPDFKit container, and then add a second MutationObserver to capture a reference to the password input presented. All of this should be done 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.