模拟点击GitHub';s新闻提要“;“更多”;JavaScript中的按钮不';行不通
我试图创建一个GreaseMonkey脚本,自动扩展GitHub新闻提要中的警报列表,但这不起作用 以下是我的代码(灵感来源于帖子): 但是,它没有像手动单击警报列表那样扩展警报列表,而是打开了链接指向的页面(模拟点击GitHub';s新闻提要“;“更多”;JavaScript中的按钮不';行不通,javascript,jquery,github,greasemonkey,Javascript,Jquery,Github,Greasemonkey,我试图创建一个GreaseMonkey脚本,自动扩展GitHub新闻提要中的警报列表,但这不起作用 以下是我的代码(灵感来源于帖子): 但是,它没有像手动单击警报列表那样扩展警报列表,而是打开了链接指向的页面(https://github.com/organizations/my_organization?page=2) 我该怎么做 编辑: 这是链接的HTML源代码,看起来没有与之关联的javascript或onClick事件: <a href="/organizations/my_org
https://github.com/organizations/my_organization?page=2
)
我该怎么做
编辑:
这是链接的HTML源代码,看起来没有与之关联的javascript或onClick事件:
<a href="/organizations/my_organization?page=2">More</a>
您的代码对我来说非常适合——在主页上,当登录时。尽管我建议您使用更具体的选择器,以避免误报 例如,使用:
var moreLink = $("#dashboard div.news div.pagination a:contains('More')");
- 您使用的Firefox和Greasemonkey版本是什么
- NoScript、Adblock或类似程序正在运行吗
- 您说看起来没有与链接关联的javascript或onClick事件。。。你怎么查的?Firebug显示了该节点上的多个事件侦听器
- 除了GitHub生成的无数CSS警告之外,Firefox的错误控制台(CtlShiftJ)中还出现了哪些错误
- 你的脚本对页面还有其他作用吗?特别是添加或删除内容或使用
?如果是,请发布或链接到完整脚本innerHTML
更新新信息:
@include
指令可能不会触发。它至少需要:
// @include https://github.com/
但是
可能更好,而且选择器足够具体,更广泛的包含应该不会造成伤害综上所述,下面的脚本适用于我,来自各种(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...");
您的代码对我来说非常适合——在主页上,当登录时。尽管我建议您使用更具体的选择器,以避免误报 例如,使用:
var moreLink = $("#dashboard div.news div.pagination a:contains('More')");
- 您使用的Firefox和Greasemonkey版本是什么
- NoScript、Adblock或类似程序正在运行吗
- 您说看起来没有与链接关联的javascript或onClick事件。。。你怎么查的?Firebug显示了该节点上的多个事件侦听器
- 除了GitHub生成的无数CSS警告之外,Firefox的错误控制台(CtlShiftJ)中还出现了哪些错误
- 你的脚本对页面还有其他作用吗?特别是添加或删除内容或使用
?如果是,请发布或链接到完整脚本innerHTML
更新新信息:
@include
指令可能不会触发。它至少需要:
// @include https://github.com/
但是
可能更好,而且选择器足够具体,更广泛的包含应该不会造成伤害综上所述,下面的脚本适用于我,来自各种(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...");
现在还不清楚“GitHub的新闻提要”是什么意思。链接到特定页面或RSS源。您需要登录才能看到它吗?是的,您需要登录才能看到它。这是您在
https://github.com
当您登录到您正在观看的回购协议的所有最新更新列表时。您不清楚“GitHub的新闻提要”是什么意思。链接到特定页面或RSS源。您需要登录才能看到它吗?是的,您需要登录才能看到它。这是您在https://github.com
当您登录到您正在观看的回购协议的所有最新更新列表时。我用完整脚本更新了原始帖子。我正在运行Firefox12和Greasemonkey 0.9.19。Firefox控制台中没有出现错误,只有CSS警告。当我运行这个脚本时,它只是在加载https://github.com/dashboard/index/2
(这是完全相同的页面)好的,请参阅更新的答案——它解释了比赛条件和AJAX延迟(无论如何,在我的测试中)。感谢您的帮助,这就是问题所在!不过,我还有一个问题:如何访问添加的新div元素$(“#dashboard div.news div.alert”)。即使单击之后,length
仍然返回30。请忽略我前面的问题,我遇到了相同的问题。。。(数据尚未加载)。谢谢你的帮助。我用完整的脚本更新了原来的帖子。我正在运行Firefox12和Greasemonkey 0.9.19。Firefox控制台中没有出现错误,只有CSS警告。当我运行此脚本时,它只是加载https: