模拟点击GitHub的新闻源JavaScript中的“更多”按钮不起作用

时间:2012-05-16 20:40:31

标签: javascript jquery github greasemonkey

我正在尝试创建一个GreaseMonkey脚本,该脚本会自动扩展GitHub新闻源中的警报列表,但这不起作用。

以下是我的代码(灵感来自this帖子):

var moreLink = $("a:contains('More')");
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
moreLink[0].dispatchEvent(evt);

但是,不是像手动点击它那样消费警报列表,而是只打开链接指向的页面(https://github.com/organizations/my_organization?page=2

我该怎么做?

编辑: 这是链接的HTML源代码,看起来没有与之关联的javascript或onClick事件:

<a href="/organizations/my_organization?page=2">More</a>

编辑2: 这是我的完整润滑脂脚本:

// ==UserScript==
// @name        test
// @namespace   test
// @description test
// @include     https://github.com
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// @version     1
// ==/UserScript==

var moreLink = $("#dashboard div.news div.pagination a:contains('More')");
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
moreLink[0].dispatchEvent(evt);
//alert($(".alert").length);

1 个答案:

答案 0 :(得分:1)

您的代码对我来说非常适合 - 在主页上登录时。虽然我建议您使用更具体的选择器,以避免误报。

EG,使用:

var moreLink = $("#dashboard div.news div.pagination a:contains('More')");

  • 您使用的是什么版本的Firefox和Greasemonkey?
  • 是否正在运行NoScript,Adblock或类似内容?
  • 你说看起来没有与链接相关的javascript或onClick事件......你是怎么检查的? Firebug在该节点上显示了几个事件监听器。
  • 除了GitHub生成的数以万计的CSS警告外,Firefox的错误控制台( Ctl Shift J )中出现了哪些错误?
  • 您的脚本是否对页面执行了其他操作?特别是添加或删除内容或使用innerHTML?如果是这样,请发布或链接到完整的脚本。

更新新信息:

  1. 在进一步测试时,从更多环境来看,现在好像GitHub的分页并不总是在Greasemonkey脚本触发时初始化。要解决此问题,请在尝试单击链接之前使用the waitForKeyElements() utility并检查分页引擎准备情况。见下文。

  2. Greasemonkey 0.9.19因为全部离开而失灵 - 因此它只活动了几天。 转到the Greasemonkey Version History page安装版本0.9.20或版本0.9.18。

  3. @include指令可能无法在您需要时触发。它至少需要:

    // @include https://github.com/
    

    但是

    // @include https://github.com/*
    

    可能会更好,并且选择器足够具体,更广泛的包含应该不会造成伤害。

  4. 升级到jQuery版本1.7.2 - 我们广泛使用它没有任何问题。 (1.5.1可能不是问题,但最好消除该变量。)


  5. 将所有这些放在一起,以下脚本适用于各种(Windows)环境。我留下了大部分调试代码,以防万一...

    // ==UserScript==
    // @name        _GitHub "news" item auto-paging
    // @namespace   _pc
    // @include     https://github.com/
    // @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
    // @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
    // ==/UserScript==
    
    if (typeof unsafeWindow.console.clear != 'undefined') {
        unsafeWindow.console.clear ();
    }
    unsafeWindow.console.log ("Start...");  //-- Does not require Firebug.
    
    waitForKeyElements (
        "#dashboard div.news div.pagination a:contains('More')",
        clickAjaxMoreLink,
        true    //-- Stop on first successful click.
    );
    
    function clickAjaxMoreLink (jNode) {
        this.numRuns    = this.numRuns || 1;
    
        unsafeWindow.console.log (
            "moreLink:", jNode,
            " | Parent classes:", jNode.parent ().attr ("class"),
            " | Run:", this.numRuns
        );
        this.numRuns++;
        /*--- COMMENT THIS NEXT CHECK OUT, if waitForKeyElements is set to
            continually click via clickAjaxMoreLink. (The last param is false.)
        */
        if (this.numRuns > 25) {
            unsafeWindow.console.log ("*** Excessive runcount, abort! ***");
            return false;
        }
    
        if (jNode.parent ().hasClass ("loading") ) {
            return true;    //-- Cancel the "found" status.
        }
        var unsfJQ_Body = unsafeWindow.$(document.body);
        if (    ! unsfJQ_Body
            ||  ! unsfJQ_Body.length
            ||  document.readyState != "complete"   //-- Order is important here
            ||  (typeof unsfJQ_Body.pageUpdate) != "function"
        ) {
            return true;
        }
    
        unsafeWindow.console.log ("Num news items, start:", $(".alert").length);
    
        setTimeout ( function () {
            unsafeWindow.console.log (
                "Num news items, after AJAX delay:", $(".alert").length
            );
        }, 2333);
    
        var evt = document.createEvent ("MouseEvents");
        evt.initEvent ("click", true, true);
        jNode[0].dispatchEvent (evt);
    
        return false;
    }
    
    unsafeWindow.console.log ("Setup complete...");