5

我正在尝试使用 Disqus api,我需要运行一些修改 Disqus 评论线程的 javascript 代码。

Disqus线程加载后如何运行javascript代码?

4

5 回答 5

4

我遇到了类似的问题。我能想出的唯一可行的解​​决方案是运行setInterval()以​​检查 Disqus 容器 div 的高度。

例子:

var editable = true; // set a flag
setInterval(function() {
// Initially Disqus renders this div with the height of 0px prior to the comments being loaded. So run a check to see if the comments have been loaded yet.
  var disqusHeight = $('#dsq-2').height();
  if ( disqusHeight > 0 ) {
    if (editable) { // To make sure that the changes you want to make only happen once check to see if the flag has been changed, if not run the changes and update the flag.
      editable = false;
      // Your code here...
    }
  }
}, 100);
于 2014-02-20T16:03:04.840 回答
3

尝试这个:

function disqus_config() {
  this.callbacks.onReady.push(function () {
    // your code
  });
}
于 2017-02-20T21:51:05.103 回答
0

使用标志来避免循环:

evento.add(window, "load", function () {
    var w = window,
    d = document,
    a = d.getElementById("disqus_thread") || "",
    disqus_shortname = a ? (a.dataset.shortname || "") : "",
    embed_js_src = ("https:" == w.location.protocol ? "https" : "http") + "://" + disqus_shortname + ".disqus.com/embed.js",
    g = ".grid",
    h = ".grid-item",
    k = ".grid-sizer",
    grid = d.querySelector(g) || "";
    function build_layout() {
        if (grid) {
            if (w.Packery) {
                var pckry = new Packery(grid, {
                        itemSelector : h,
                        gutter : 0
                    });
            } else if (w.Masonry) {
                var msnry = new Masonry(grid, {
                        itemSelector : h,
                        columnWidth : k
                    });
            }
        }
    }
    build_layout();
    if (a && disqus_shortname) {
        w.loadJS && loadJS(embed_js_src, function () {
            if (grid) {
                var f = !1;
                setInterval(function () {
                    var disqus_thread_height = a.clientHeight || a.offsetHeight || "";
                    if (108 < disqus_thread_height && !1 === f) {
                        /* alert(disqus_thread_height); */
                        build_layout();
                        f = !0;
                    }
                }, 100);
            }
        });
    }
});
于 2016-03-22T11:09:27.863 回答
0

这是 Brandon Morse 为新版本 Disqus 修改的代码,当 Disqus 加载评论时脚本停止。

var interval = setInterval(function() {
    var $ = jQuery;
    var disqusHeight = $('#disqus_thread').height();
    if ( disqusHeight > 52 ) {  // height 52px is header of Disqus, more than 52px means that disqus load comments
      // Your code
        clearInterval(interval); // after loaded comment we stop this script
    }
}, 100);
于 2015-07-21T09:05:46.037 回答
0
  `<script> 
        var disqus_config = function () {
         this.callbacks.onReady = [function(data) {
            //your code here
          }];
         this.callbacks.afterRender= [function(data) {
            //your code here
          }];
         this.callbacks.beforeComment= [function(data) {
            //your code here
          }];
         this.callbacks.onInit= [function(data) {
            //your code here
          }];
         this.callbacks.onNewComment= [function(data) {
            //your code here
          }];
         this.callbacks.onPaginate= [function(data) {
            //your code here
          }];
         this.callbacks.preData= [function(data) {
            //your code here
          }];
         this.callbacks.preInit= [function(data) {
            //your code here
          }];
         this.callbacks.preReset= [function(data) {
            //your code here
          }];

        };
  </script>`
于 2018-01-12T06:42:24.667 回答