【问题标题】:Counting instances of a class - injecting jQuery into a third party website using Firebug?计算类的实例 - 使用 Firebug 将 jQuery 注入第三方网站?
【发布时间】:2015-06-03 14:40:28
【问题描述】:

我正在访问带有 cmets 部分的第三方网页。

相关网站: http://www.cbc.ca/news/technology/canadian-led-research-looks-to-grow-strawberries-on-mars-1.2704433

cmets 部分仅显示几个 cmets,并显示“显示更多”按钮。我正在编写一个脚本,它将继续自动按下“显示更多”按钮,直到显示所有 cmets。一旦“显示更多”按钮不再显示,我想触发一个新功能。

我试图通过 Firefox (30.0) 中的 Firebug (2.0.1) 中的命令行将我自己的 JavaScript “注入”到第三方页面中来实现这一点。

使用 Firebug 我发现“显示更多”按钮的类是vf-load-more。我创建了一个setInterval() 函数调用,并且能够成功地重复“单击”按钮。

现在我正在尝试实现“cmets 结束”识别。我通过计算页面上vf-load-more 的实例来做到这一点。如果没有vf-load-more 的实例,则启动下一个函数。

numShowMore = $('.vf-load-more').length;
if (numShowMore == 0) {
    clearInterval(interval1);
    nextFunction(); //This function has not been written yet
}

问题: $(".vf-load-more").length; 正在返回 undefined。为什么?按钮在页面上。

完整的代码:

//This injects a jQuery reference into the header
var script = document.createElement('script');
script.src = '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js';
document.body.appendChild(script);

//If jQuery is loaded execute the rest of the code
if (!window.jQuery)
{
  alert('jQuery is GO');

  var numShowMore;

  function clickButton()
  {
    numShowMore = $('.vf-load-more').length;
    alert('numShowMore = ' + numShowMore); //This alert returns "undefined"??
    if (numShowMore == 0) {
      clearInterval(interval1);
      nextFunction(); //This function has not been written yet
    }
    $('.vf-load-more').click();
  }

  var interval1 = setInterval(clickButton, 3000);
}

现在变得很奇怪。如果我让间隔继续运行并“单击”“显示更多”按钮,直到显示所有 cmets,它最终将停止显示“显示更多”按钮。然后,我通过在命令编辑器中运行 clearInterval(interval1); 来强制停止间隔,然后当我运行以下代码时,它会提醒 numShowMore = 1 但按钮不再存在。

numShowMore = $('.vf-load-more').length;
alert('numShowMore = ' + numShowMore);

为什么不是0?

【问题讨论】:

    标签: javascript jquery html firebug


    【解决方案1】:

    我尝试将 jQuery $ 替换为 jQuery 并且成功了。我不知道为什么,但我认为 jQuery 可能与页面上加载的另一个库发生冲突。

    $('.vf-load-more').length;
    

    替换为

    jQuery('.vf-load-more').length;
    

    $('.vf-load-more').click();
    

    替换为

    jQuery('.vf-load-more').click();
    

    我还注意到你的代码有问题,在检查jQuery是否加载的if条件中,条件相反,应该是

    if (window.jQuery){
    //do stuff
    }
    

    但是,由于您正在加载一个可能需要时间来加载的外部脚本,如果没有加载库,则条件可能为 false,因此最好使用 setIntervalsetTimeout 来检查是否加载了 jQuery 或没有,如果已加载,则执行您的代码。

    类似这样的:

    //If jQuery is loaded execute the rest of the code
    var jqueryCheckTimer = setInterval(function(){
    if(window.jQuery){
            clearInterval(jqueryCheckTimer);
             var interval1 = setInterval(clickButton, 3000);
        }
    },100);
    

    【讨论】:

    • 我接受您的解决方案作为答案。如果我将“$”的所有实例更改为“jQuery”,它仍然不起作用,所以我发现我必须选择在哪里进行更改才能使其工作(例如,我仍然必须使用“$”和.click() 调用或按钮未被点击。
    • 我选择你的解决方案的主要原因是因为我也相信这是一个冲突的框架问题,你已经向我指出了 jQuery.noConflict() 的方向,我相信这将大大有助于解决我遇到的所有问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多