Wait for specific element to appear

Q: I need to wait for a specific annotation, form field or document page to appear. How can I do so?

A: 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 we want to wait and focus a specific widget annotation:

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.