繁体   English   中英

如何检测到已响应滚动触发加载了新内容?

[英]How can I detect that new content has been loaded in response to a scroll trigger?

我正在Facebook上运行一个脚本,该脚本需要我在“朋友”窗口中获取人的ID(这可能不是完成此特定任务的最有效方法,但由于我想知道如何执行此操作一般来说,这是一个很好的例子)。

这意味着,如果我的朋友数量不多,则必须向下滚动Facebook,才能将其添加到页面中。

我添加了将页面向下滚动到页脚的逻辑,但是我不知道如何强制获取内容的ID在加载内容后运行。

现在,我已经使用setTimeout了几秒钟-显然,不能保证在适当的时间这样做,所以我想知道如何正确执行此操作:

var k;
function doit(){

    k = document.getElementsByClassName("_698");

    var g= Array.prototype.slice.call(k);
    confirm(g.length);
    // the confirm is just to make sure it's working 
    // (if i don't use setTimeout it'll return a smaller number 
    //  since not all the friends were included)

} 

window.addEventListener("load", function(){ 
  document.getElementById( "pageFooter" )
    .scrollIntoView();setTimeout(doit,3000);
  });

Crayon Violent在他对JavaScript检测AJAX事件的回答中详细介绍了如何实现这一点。 技巧是钩住基础XMLHttpRequest对象,以便检测何时发送请求。

我对逻辑进行了重新编写,使其更适合您的需求:

//
// Hooks XMLHttpRequest to log all AJAX requests. 
// Override ajaxHook.requestCompleted() to do something specific 
// in response to a given request.
//
var ajaxHook = (function()
{
   // we're using a self-executing function here to avoid polluting the global
   // namespace. The hook object is returned to expose just the properties 
   // needed by client code.
   var hook = { 
     // by default, just logs all requests to the console. 
     // Can be overridden to do something more interesting.
     requestCompleted: function(xmlHttp, url, method) { console.log(url); } 
   };

   // hook open() to store URL and method
   var oldOpen = XMLHttpRequest.prototype.open;
   XMLHttpRequest.prototype.open = function(method, url)
   {
      this.hook_method = method;
      this.hook_url = url;
      oldOpen.apply(this, arguments);
   }

   // hook send() to allow hooking onreadystatechange
   var oldSend = XMLHttpRequest.prototype.send;
   XMLHttpRequest.prototype.send = function()
   {
      var xmlhttp = this;

      //hook onreadystatechange event to allow processing results
      var oldReadyStateChange = xmlhttp.onreadystatechange;
      xmlhttp.onreadystatechange = function()
      {
         oldReadyStateChange.apply(xmlhttp, arguments);

         if ( this.readyState === 4 ) // completed
         {
            hook.requestCompleted(xmlhttp, 
              xmlhttp.hook_url, xmlhttp.hook_method);
         }
      };

      oldSend.apply(this, arguments);
   };

   return hook;
})();

在用户脚本中加载了这段代码之后,您可以按照以下方式实现逻辑:

var k;
function doit()
{
    k = document.getElementsByClassName("_698");

    var g= Array.prototype.slice.call(k);
    confirm(g.length);
} 

window.addEventListener("load", function()
{     
   ajaxHook.requestCompleted = function(xmlhttp, url, method)
   {
      // is this the request we're interested in? 
      // (Facebook appears to load friends from a URL that contains this string)
      if ( /AllFriendsAppCollectionPagelet/.test(url) ) 
      {
         // Facebook defers rendering the results here, 
         // so we just queue up scraping them until afterwards
         setTimeout(doit, 0); 
      }
   };

  // trigger loading of more friends by scrolling the bottom into view
  document.getElementById( "pageFooter" )
    .scrollIntoView();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM