1

I'm making a script to interact with a web page. So, I need to generate click events. (I'm using GreaseMonkey)

Well, my question is:

Why isn't this working from the user script:

$(document).ready(function(){
    $("a:contains('example')").click();
});
  • jQuery checked and tested. Working fine.
  • It works from the console! But NOT from the script
  • Everything else works...
  • The target has a href="JavaScript:void(0)" Could this be the problem?
PhearOfRayne
  • 4,990
  • 3
  • 31
  • 44
JosEvora
  • 134
  • 1
  • 9
  • 1
    It does work: http://jsfiddle.net/h3YtJ/ Next time when you experience some issues with jquery - reproduce it on http://jsfiddle.net – zerkms Dec 03 '13 at 22:19
  • 1
    it does work... form the console, yes... but not from my script... like i said above! how can i make it work from the user script in GreaseMonkey? and the the target has a href="JavaScript:void(0)", not "#".... – JosEvora Dec 03 '13 at 22:22
  • Are you sure this is the part of the code that is not working? – Blue Sheep Dec 03 '13 at 22:23
  • i have removed EVERYTHING else! the script is 100% what is there!!! on the console is a go... on the script is not!! – JosEvora Dec 03 '13 at 22:25
  • it's a security thing: maybe try unsafeWindow.setTimeout instead of $().ready()... – dandavis Dec 03 '13 at 22:36

2 Answers2

4

jQuery .click(), like jQuery .trigger(), only works reliably with events handled by jQuery. And in the case of userscripts and Greasemonkey scripts, the jQuery instance triggering click must be the same instance as the jQuery that set the event handler (usually).

Additionally, userscript jQuery interferes with the target page unless you explicitly take countermeasures.

There are two basic approaches in cases like this. (1) Use @grant none mode, (2) properly sandbox your script and generate a true click event.

(1) Quick and dirty @grant none method:

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant    none
// ==/UserScript==

var $ = window.jQuery;

$("a:contains('example')").click();

Warning: With this method, you cannot use the built-in GM_ functions. And, the page must be running jQuery to use jQuery functions.


(2) Recommended click event method:

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
var targLink = $("a:contains('example')");

clickNode (targLink);

function clickNode (jNode) {
    if (jNode  &&  jNode.length) {
        var clickEvent  = document.createEvent ('MouseEvents');
        clickEvent.initEvent ('click', true, true);
        jNode[0].dispatchEvent (clickEvent);
    }
}

Note that $(document).ready() is not needed in Greasemonkey scripts except when @run-at document-start is set.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thank you, i'll only be able to test that tomorrow, but it sounds right 'cause i just changed the "@grant" before running the script for the 1st time... i'll definitely try that! ;) – JosEvora Dec 03 '13 at 22:56
  • I've tried the 1st approach... the page doesn't load properly! (everything is in place, but it stays with the 'loading...' message, doesn't move from there!) With the 2nd approach, doesn't happen anything... i also tried to call the `clickNode` function from a `setTimeout`, just to be sure it runs after the page is loaded, but nothing happens!... – JosEvora Dec 04 '13 at 13:16
  • Do you advise me to use "Scriptish" instead of "GreaseMonkey" has a definitive way to solve this? – JosEvora Dec 04 '13 at 13:22
  • No. If this doesn't work in GM, it won't work in Scriptish either. Something you are not showing us is the problem. Remember that `:contains()` is case-sensitive. Link to the target page. – Brock Adams Dec 04 '13 at 18:54
  • You're right... On Scriptish, it works only when i run the script from the editor... when i run the script from the page it doesn't... – JosEvora Dec 04 '13 at 21:04
1

Your script does nothing because of javascript:void(0). click event is triggered but there is no action after that.

If you change it to

<body>
  <a href="style.css">example</a>

  <script>
  $("a:contains('example')").click();
  </script>
</body>

you will get, for example, file style.css open.

Anto Jurković
  • 11,188
  • 2
  • 29
  • 42