How to automatically adjust iframe height for embedded wordpress posts

ugh,我终于搞定了。问题在于我在 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); // 根据需要调整延迟                  
      },
      { id: "component-id", onlyStream: true}
    );
</script>

编辑:实际上,它不起作用。我在 iframe 标签中添加了 height="4000px",我认为这就是它起作用的原因。

1 个赞