I’m trying to create a GreaseMonkey script that automatically extends the list of alerts in GitHub’s newsfeed but that doesn’t work.
Here is my code (inspired from this post):
var moreLink = $("a:contains('More')");
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
moreLink[0].dispatchEvent(evt);
But instead of expending the list of alerts like it does when you manually click on it, it just opens the page the link points too (https://github.com/organizations/my_organization?page=2)
How can I do this?
Edit:
Here is the HTML source code of the link, it looks like there is no javascript or onClick event associated to it:
<a href="/organizations/my_organization?page=2">More</a>
Edit 2:
Here is my full greasemonkey script:
// ==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);
Your code works perfectly for me — on the main page, when logged in. Though I would recommend that you use a more specific selector, to avoid false positives.
EG, use:
innerHTML? If so, post or link to the full script.Update for new information:
On further testing, from more environments, it now appears as if GitHub’s pagination is not always initialized by the time the Greasemonkey script fires. To get around that, use the waitForKeyElements() utility and check for pagination-engine readiness before attempting to click the link. See below.
Greasemonkey 0.9.19 was buggy as all get-out — hence it was only active for a few days.
Go to the Greasemonkey Version History page and install either version 0.9.20 or version 0.9.18.
The
@includedirective may not be firing when you want it. It needs to be at least:But
might be better, and the selector is specific enough that a broader include should cause no harm.
Step up to jQuery version 1.7.2 — we’ve used it extensively with no problems. (1.5.1 is probably not the problem, but best to eliminate that variable.)
Putting all that together, the following script works for me, from a variety of (Windows) environments. I left most of the debugging code in, just in case…