Wait for a Specific Element to Appear

If you need to wait for a specific annotation, form field, or document page to appear, you can use a MutationObserver right after the PSPDFKit instance loads and query the DOM after the annotation or page element appears. When it finally appears, you can remove the observer.

For instance, if you want to wait and focus a specific widget annotation, use the following code:

PSPDFKit.load(configuration).then((instance) => {
  const observer = new MutationObserver(function () {
    const element = instance.contentDocument.querySelector(
      '.PSPDFKit-Annotation-Widget-Text[name="Description 1"]'
    );
    if (element) {
      observer.disconnect();
      element.focus();
    }
  });
  observer.observe(instance.contentDocument, {
    childList: true,
    subtree: true
  });
});

This has been tested with PSPDFKit for Web 2020.5.1.