Sunday, 8 September 2013

Greasemonkey (userscript): Clicking on element?

Greasemonkey (userscript): Clicking on element?

I'm trying to create a simple Greasemonkey userscript that periodically
triggers mouse click on < a> element (that is binded to do something with
JavaScript). There is always one < a> element inside the "awesome-div" but
the class and name of that < a> may vary.
HTML:
...
<div id="awesome-div">
<a class="some random class">this randomly named link needs to be clicked</a>
</div>
...
Here's my userscript
// ==UserScript==
// @name My Awesome Script
// @namespace http://example.com/myawesomescript
// @description Not working
// @include http://mywebsite.com/*
// @require
http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
// ==/UserScript==
var p = unsafeWindow;
var $ = p.$;
function clickLink() {
$('div#awesome-div').find('a').click();
}
$(document).ready(function() {
setInterval(clickLink, 5000); // repeat every 5 seconds
}
But this userscript is not working. What goes wrong?
// @include http://mywebsite.com/* is correct in the actual script.

No comments:

Post a Comment