【问题标题】:Disqus Iframe not resizing on AMP installationDisqus Iframe 未在 AMP 安装时调整大小
【发布时间】:2019-12-18 16:27:45
【问题描述】:

我正在尝试将 Disqus 集成到我正在开发的使用 AMP 的 Hugo 主题中。

我遵循了推荐的installation guide,因此我的amp-iframe 在沙盒属性上具有allow-same-origin 值,而src url 来自不同的域。

我的 iframe 代码:

<amp-iframe width=600 height=180
    layout="responsive"
    sandbox="allow-scripts allow-same-origin allow-modals allow-popups allow-forms"
    resizable
    src="https://subdomain.asur.dev#code">

    <div overflow
        tabindex=0
        role=button
        aria-label="Load more"
        style="display:block;font-size:12px;font-weight:500;font-family:Helvetica Neue, arial, sans-serif;text-align:center;line-height:1.1;padding:12px 16px;border-radius:4px;background:rgba(29,47,58,0.6);color:rgb(255,255,255)">
        Load more
    </div>
</amp-iframe>

我的嵌入代码:

<div id="disqus_thread"></div>
<script>
window.addEventListener('message', receiveMessage, false);
function receiveMessage(event)
{
    if (event.data) {
        var msg;
        try {
            msg = JSON.parse(event.data);
        } catch (err) {
            // Do nothing
        }
        if (!msg)
            return false;

        if (msg.name === 'resize' || msg.name === 'rendered') {
            window.requestAnimationFrame(() => {
                window.parent.postMessage({
                sentinel: 'amp',
                type: 'embed-size',
                height: msg.data.height
                }, '*');
            });
        }
    }
}
</script>
<script>
    var disqus_config = function () {
        this.page.url = window.location;  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = window.location.hash; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
    (function() {
        var d = document, s = d.createElement('script');
        s.src = 'https://shortname.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>

我不确定这是错误还是我做错了什么,但iframe resizing 上的amp-example 工作正常,所以我的浏览器可以正常工作。

在这里,您可以在页面末尾找到 iframe 的示例:https://asur.dev/en/amperage/theme-kitchen-sink/

【问题讨论】:

  • 如何为 amp 设置 disqus。我遇到了 iframe 未完全显示的问题(小尺寸)。我也应该更改this.page.url = window.location; this.page.identifier = window.location.hash; 还是保持原样。

标签: javascript iframe amp-html disqus hugo


【解决方案1】:

我发现了问题。我正在从子域 subdomain.asur.dev 重定向到主域中的页面,例如 asur.dev/something

看起来 iframe 的初始 AMP 验证通过了,一切正常,没有错误,但由于组件中的内部逻辑,iframe 无法调整大小。

当我将嵌入的 HTML 移动到一个完全独立的子域且没有重定向时,问题得到了解决。

【讨论】:

  • 没有收到任何验证错误?因为这里直接使用了script标签
  • @Urock 嵌入页面不需要是有效的 AMP 页面。这正是我使用 iframe 的原因。
  • 是的,我很困惑 embed-size 请求将在 AMP 页面中,但该请求应该在嵌入式页面中,谢谢,有时我们无法控制嵌入式 iframe 添加此 postMessage 请求,因为情况我们如何处理这个
猜你喜欢
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
  • 2011-10-01
  • 2017-02-22
相关资源
最近更新 更多