title: Userscript to click button
body: <p>I would like to click a buy now button using greasemonkey. </p>
  
  <pre><code>// ==UserScript==
  // @name        script
  // @namespace   sc
  // @include     *
  // @version     1
  // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
  // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
  // @grant    GM_addStyle
  // ==/UserScript==
  
  waitForKeyElements ("#buy-now", triggerMostButtons);
  
  function triggerMostButtons (jNode) {
      triggerMouseEvent (jNode[0], "mouseover");
      triggerMouseEvent (jNode[0], "mousedown");
      triggerMouseEvent (jNode[0], "click");
      triggerMouseEvent (jNode[0], "mouseup");
  }
  
  function triggerMouseEvent (node, eventType) {
      var clickEvent = document.createEvent('MouseEvents');
      clickEvent.initEvent (eventType, true, true);
      node.dispatchEvent (clickEvent);
  }
  </code></pre>
  
  <p>Html of the buttons:</p>
  
  <pre><code>&lt;div class="buy-now"&gt;&lt;a data-spm-anchor-id="0.0.0.0" data-widget-cid="widget-14" tabindex="-1" id="buy-now" class="buy-now-btn" href="#"&gt;Buy Now&lt;/a&gt;&lt;a id="add-to-cart" class="add-to-cart-btn" href="#"&gt;Add to Cart&lt;/a&gt;&lt;/div&gt;
  </code></pre>
  
  <p>I have tried other codes like</p>
  
  <pre><code>var TargetLink          = $("a:contains('Ik neem er een!')")
  if (TargetLink.length)
      window.location.assign (TargetLink[0].href);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>waitForKeyElements ("a.class:contains('buy-now-btn')", clickOnFollowButton);
  
  function clickOnFollowButton (jNode) {
      var clickEvent  = document.createEvent ('MouseEvents');
      clickEvent.initEvent ('click', true, true);
      jNode[0].dispatchEvent (clickEvent);
  }
  </code></pre>
  
  <p>Could someone give me an explenation what to put in the waitForKeyElements as first parameter for clicking the button?</p>