如何在Disqus加载后运行JavaScript

6

我正在尝试与Disqus API合作,需要运行一些修改Disqus评论线程的JavaScript代码。

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

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);

1
太棒了。这对我来说是一个足够好的解决方案,但是你需要改变实例化setInterval的方式,因为即使测试已经完成,它仍将继续运行。将其分配给一个变量,并在测试通过后清除间隔。 - Scotty

4

试试这个:

function disqus_config() {
  this.callbacks.onReady.push(function () {
    // your code
  });
}

这是唯一有效的答案。这些函数可能没有文档说明,但它们一直存在,并且在2020年仍然可用。 - Mike 'Pomax' Kamermans

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);
            }
        });
    }
});

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>`

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);

请添加一些解释。 - Nilambar Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接