# faustwp/0.7.7/includes/replacement/previewlinks.js

Faust.js, version 0.7.7. 56 lines.

- Page: https://pluginprobe.com/plugins/faustwp/0.7.7/code/includes/replacement/previewlinks.js
- Raw: https://pluginprobe.com/plugins/faustwp/0.7.7/raw/includes/replacement/previewlinks.js
- Modified: 2021-12-02T16:40:04+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/faustwp/0.7.7/code/includes/replacement/previewlinks.js#L10-L20`.

```javascript
/**
 * XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998
 */

window.addEventListener('DOMContentLoaded', function () {
  wp.domReady(function () {
    // Get the correct preview link via wp_localize_script
    const previewLink = window._faustwp_preview_link
		? window._faustwp_preview_link._preview_link
      : undefined;

    /**
     * Check to make sure there is a preview link before continuing, as there may not be a preview link
     * for every instance the block editor is enqueued (e.g. /wp-admin/widgets.php)
     */
    if (!previewLink) {
      return;
    }

    const intervalId = setInterval(function () {
      const previewButton = document.querySelectorAll(
        'button[class~="block-editor-post-preview__button-toggle"]',
      );

      if (!previewButton.length) {
        return;
      }

      clearInterval(intervalId);
      previewButton[0].addEventListener('click', function () {
        setTimeout(function () {
          const links = document.querySelectorAll('a[target*="wp-preview"]');

          if (!links.length) {
            return;
          }

          links.forEach((link) => {
            link.href = previewLink;

            var copy = link.cloneNode(true);
            copy.addEventListener('click', function (ev) {
              previewButton[0].click();

              wp.data.dispatch('core/editor').autosave();
            });

            link.parentElement.insertBefore(copy, link);
            link.style.display = 'none';
          });
        }, 100);
      });
    }, 100);
  });
});

```
