How to automatically adjust iframe height for embedded wordpress posts

ああ、うまくいきました。問題は、iframeタグにカスタムクラスを使用していたことで、Discourseはそのようなタグをすべて削除してしまうのです。数日前の教訓を活かすべきでした(https://meta.discourse.org/t/formatting-posts-to-look-more-like-wordpress-blog/301133/6)。カスタムHTMLクラスがここで機能しないのはセキュリティ上の理由だと聞きましたが、なぜなのかよくわかりません:confused:

しかし、.topic-body iframeセレクターを使用してiframeの高さを調整するためのコードは次のとおりです(これが他の人にも機能するかどうかはわかりませんが、私には機能するようです)。

<script type="text/discourse-plugin" version="0.8.18">
    api.decorateCookedElement(
      element => {
        setTimeout(function() {
          let iframes = element.querySelectorAll('.topic-body iframe');
          if (iframes) {
            iframes.forEach(function(iframe) {
              iframe.onload = function() {
                let iframeDocument = this.contentDocument || this.contentWindow.document;
                let contentHeight = Math.max(
                  iframeDocument.body.scrollHeight,
                  iframeDocument.documentElement.scrollHeight
                ) + 'px';
                this.style.height = contentHeight;
              };
            });
          }
        }, 5000); // Adjust the delay as needed                  
      },
      { id: "component-id", onlyStream: true}
    );
</script>

編集:実際には、機能しません。iframeタグにheight="4000px"を追加したため、機能していたのだと思います。

「いいね!」 1