title: Choosing and activating the right controls on an AJAX-driven site
body: <p>So, I am trying to make it where every time I visit any nike.com sneaker page (without the HTML link), it automatically picks my shoe size, adds it to the cart, and checks out for me. </p>
  
  <p>I am currently trying to use this script (below), but every time I go to the sneaker page, it does not properly add the shoe size I want, but just goes straight to checkout with nothing in my cart. </p>
  
  <p>I'm told that I need to match the code to the actual page HTML, but I don't know how to do that.  Please help.</p>
  
  <pre><code>// ==UserScript==
  // @name     _Nike auto-buy(!!!) script
  // @include  http://*/*
  // @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==
  /*- The @grant directive is needed to work around a design change
      introduced in GM 1.0.   It restores the sandbox.
  */
  
  var okayToClickAddtoCart = false;
  
  //-- Assumes that size is a standard &lt;option&gt; tag or similar...
  waitForKeyElements (".selectBox-label[value='10']", selectShoeSize);
  
  function selectShoeSize (jNode) {
      jNode.prop ('selected', true);
  
      okayToClickAddtoCart = true;
  }
  
  
  waitForKeyElements (".add-to-cart.nike-button", clickAddToCart);
  
  function clickAddToCart (jNode) {
      if ( ! okayToClickAddtoCart) {
          return true;    //-- Don't click yet.
      }
  
      var clickEvent  = document.createEvent ('MouseEvents');
      clickEvent.initEvent ('click', true, true);
      jNode[0].dispatchEvent (clickEvent);
  }
  
  
  waitForKeyElements (".checkout-button", clickCheckoutButton);
  
  function clickCheckoutButton (jNode) {
      var clickEvent  = document.createEvent ('MouseEvents');
      clickEvent.initEvent ('click', true, true);
      jNode[0].dispatchEvent (clickEvent);
  }
  </code></pre>
  
  <p><br>
  <a href="http://store.nike.com/us/en_us/?l=shop,pdp,ctr-inline/cid-1/pid-656545/pgid-656543" rel="noreferrer">Link to the "target page"</a><br>
  <a href="http://pastebin.com/6M7cMw40" rel="noreferrer">Snapshot of the target HTML</a> (in case the target page is removed or changed by Nike)</p>