在 disqus 表单获得更新后,我需要执行一些重新计算。一个新的评论,错误信息仅举几例。本质上,任何导致 Disqus iframe 垂直扩展的事件。检查了 API,但没有找到任何公共事件。似乎这些事件不是公开可访问的 atm。所以第一个问题是——Disqus 是否有任何公共活动可以附加?
第二个是——如果我无法附加到来自 Disqus 的事件,考虑到 Disqus 的东西在 iFrame 中,我想知道 MutationEvent 会为我解决问题吗?
在 disqus 表单获得更新后,我需要执行一些重新计算。一个新的评论,错误信息仅举几例。本质上,任何导致 Disqus iframe 垂直扩展的事件。检查了 API,但没有找到任何公共事件。似乎这些事件不是公开可访问的 atm。所以第一个问题是——Disqus 是否有任何公共活动可以附加?
第二个是——如果我无法附加到来自 Disqus 的事件,考虑到 Disqus 的东西在 iFrame 中,我想知道 MutationEvent 会为我解决问题吗?
到目前为止我想出的最好的
function disqus_config() {
this.callbacks.onNewComment = [function() { trackComment(); }];
}
在 chrome 控制台中执行 aconsole.log(DISQUS)
会显示 disqus 对象,并且还提到了其他回调
_callbacks: Object
switches.changed: Array[2]
window.click: Array[2]
window.hashchange: Array[2]
window.resize: Array[2]
window.scroll: Array[2]
和on
和trigger
方法
我不确定 Disqus 的公共事件,但如果您只需要监视 iframe 高度的变化,这里有一种方法:
var iframe = document.getElementById('myIframe');
var iframeHeight = iframe.clientHeight;
setInterval(function() {
if(iframe.clientHeight != iframeHeight) {
// My iframe's height has changed - do some stuff!
iframeHeight = iframe.clientHeight;
}
}, 1000);
当然,这基本上是一个黑客。但它应该工作!
好吧,他们没有记录任何公共事件(据我所知)。但是,应用程序在其父窗口上触发了很多事件。因此,可以倾听他们的意见并采取一些行动。您可以使用以下代码段执行此操作:
window.addEventListener('message', function (event) {
// if message is not from discus frame, leap out
if (event.origin != 'https://disqus.com' && event.origin != 'http://disqus.com') return;
// parse data
var data = JSON.parse(event.data);
// do stuff with data. type of action can be detected with data.name
// property ('ready', 'resize', 'fakeScroll', etc)
}, false);
在基于 webkit 的浏览器中它工作得很好。使用 Firefox 可能会有一些问题。使用 IE ......好吧,我手头没有任何 IE 来测试它。
您可以在embed payload中找到可用事件列表:
callbacks:{
preData:[],
preInit:[],
onInit:[],
afterRender:[],
onReady:[],
onNewComment:[],
preReset:[],
onPaginate:[],
onIdentify:[],
beforeComment:[]
}
我没有找到任何文档(除了的示例onNewComment
),因此您需要根据事件名称猜测它们是如何工作的。
您可以通过以下方式使用它们:
var disqus_config = function () {
this.callbacks.onNewComment = [
function() {
// your code
}
];
};
或者
var disqus_config = function () {
this.callbacks.onNewComment = this.callbacks.onNewComment || [];
this.callbacks.onNewComment.push(function() {
// your code
});
}
在旁注中,我发现它们对于检测 iframe 评论高度的变化完全没用。我以使用ResizeSensor
from结束css-element-queries
。