How to automatically adjust iframe height for embedded wordpress posts

أوه، لقد تمكنت من جعله يعمل. كانت المشكلة أنني كنت أستخدم فئة مخصصة على علامة iframe وتقوم Discourse بإزالة جميع هذه العلامات. كان يجب أن أتعلم درسي منذ بضعة أيام (Formatting posts to look more like Wordpress blog - #6 by jimkleiber). قرأت أن هذا لسبب أمني لعدم عمل فئات HTML المخصصة هنا ولكنني لست متأكدًا من السبب :confused:

ومع ذلك، إليك الكود لضبط ارتفاع iframe باستخدام المحدد .topic-body 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>

تعديل: في الواقع، لم يعمل. لقد أضفت height="4000px" في علامة iframe وهذا هو السبب في أنه كان يعمل، أعتقد.

إعجاب واحد (1)