2012-07-01 32 views
6

我需要在disqus窗體獲得更新後執行一些重新計算。一個新的評論,錯誤消息只是僅舉幾例。實質上是任何導致Disqus iframe垂直擴展的事件。檢查了API,但沒有發現任何公共事件。似乎事件不是公開訪問atm。所以第一個問題是 - Disqus是否有任何公共活動要附加到?有公共活動要附加到?

第二個將是 - 如果我無法附加到來自Disqus的事件我不知道MutationEvent會爲我考慮Disqus stuff是否在iFrame內?

+0

你有沒有找到答案? :D – TimPietrusky

+0

不幸的是,蒂姆。 – spliter

回答

1

我不知道在特定的公共事件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); 

誠然,它基本上是一個黑客。但它應該工作!

+0

是的,這確實是一個黑客。此外,對我來說不是非常優雅 - 1秒延遲調整高度並不是我想要的。尤其是因爲無論iframe是否被更改,我都會使用計算能力來進行每秒鐘的輪詢。但是,無論如何,謝謝你的代碼片段。 – spliter

+0

長時間乾燥時,可能歡迎冰雹。該解決方案可能很有用。 – skobaljic

1

最好的我已經從這裏拿出到目前爲止

function disqus_config() { 
    this.callbacks.onNewComment = [function() { trackComment(); }]; 
} 

http://help.disqus.com/customer/portal/articles/466258-how-can-i-capture-disqus-commenting-activity-in-my-own-analytics-tool-

做的鉻控制檯console.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] 
等回調

and on and trigger methods

0

那麼,他們沒有記錄任何公共事件(據我所知)。但是,應用程序正在其父窗口上觸發大量事件。所以有可能傾聽他們並採取一些行動。你可以用下面的代碼來做到這一點:

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來測試它。

相關問題