开发者

jquery script to monitor disqus comment addition?

开发者 https://www.devze.com 2023-02-28 17:50 出处:网络
I would like to write a small script to monitor the disqus comments added on a page and fire some notification callbacks whenever new comments are added.

I would like to write a small script to monitor the disqus comments added on a page and fire some notification callbacks whenever new comments are added.

The simplest way for me to do it would be to periodically monitor the contents of the #dsq-comments div and trigger the callback whenever its html content changes.

While this will work, i'am wondering if there are any cleaner methods out there ?.


Edit : As suggested by @Pekka i implemented the onNewComment callback on my page and it works beautifully. This was one of my major concerns about integrating Disqus to my site and now i have a workable solution.

function disqus_config() {
  this.callbacks.onNewComment = [function() {
       $.get("/notifications/newco开发者_如何学编程mment?...", function(data) {
       });
  }];
}


There seems to be an onNewComment callback. See How can I capture Disqus commenting activity in my own analytics tool?

This blog post does a good job at documenting it.


Disqus commenting activity can be captured via callbacks, by using:

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

Where you will replace trackComment(); by your own function.

Here is an example that loads Disqus comments dynamically using JavaScript, where you can find the discus_config function that will be fired when a new comment is added.

var loadDisqus = function() {
  window.disqus_identifier = 'your thread id';
  window.disqus_shortname = 'disqus_shortname';
  window.disqus_url = 'http://your_domain.com/path';
  window.disqus_title ='page title';
  window.disqus_config = function() {
    this.callbacks.onNewComment = [function() { myFunction() }];
  };

  (function() {
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] ||
      document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();

};
0

精彩评论

暂无评论...
验证码 换一张
取 消