在 AJAX 驱动的站点上选择和激活正确的控件 [英] Choosing and activating the right controls on an AJAX-driven site

查看:14
本文介绍了在 AJAX 驱动的站点上选择和激活正确的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我试图让每次我访问任何 nike.com 运动鞋页面(没有 HTML 链接)时,它都会自动选择我的鞋码,将其添加到购物车,然后为我结账.

我目前正在尝试使用这个脚本(如下),但每次我进入运动鞋页面时,它都没有正确添加我想要的鞋码,而是直接去结账,我的购物车里什么都没有.

有人告诉我,我需要将代码与实际页面的 HTML 匹配,但我不知道该怎么做.请帮忙.

//==用户脚本==//@name _Nike 自动购买(!!!) 脚本//@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//==/用户脚本==/*- 需要@grant 指令来解决设计更改在 GM 1.0 中引入.它恢复沙箱.*/varokayToClickAddtoCart = false;//-- 假设 size 是标准的 <option>标签或类似...waitForKeyElements (".selectBox-label[value='10']", selectShoeSize);函数 selectShoeSize (jNode) {jNode.prop('选择',真);OKToClickAddtoCart = true;}waitForKeyElements (".add-to-cart.nike-button", clickAddToCart);功能 clickAddToCart (jNode) {如果(!okayToClickAddtoCart){返回真;//- 暂时不要点击.}var clickEvent = document.createEvent('MouseEvents');clickEvent.initEvent('click', true, true);jNode[0].dispatchEvent(clickEvent);}waitForKeyElements (".checkout-button", clickCheckoutButton);功能 clickCheckoutButton (jNode) {var clickEvent = document.createEvent('MouseEvents');clickEvent.initEvent('click', true, true);jNode[0].dispatchEvent(clickEvent);}



  • 我们使用 Firebug(或类似工具)获取关键节点的 HTML 结构.例如,SIZE 下拉列表的 HTML 如下所示:

    <span class="sizeDropdown selectBox-open">...<label class="dropdown-label selectBox-label-showing">SIZE</label>...<a class="selectBox size-dropdown mediumSelect shoes selectBox-dropdown" ...>...</a></span>

    链接实际触发的是 mousedown 事件,而不是点击.

    Firebug 为我们提供了一个 CSS 路径:

    html.js body div#body div#body-wrapper.fullheight div#body-liner.clear div#content div#pdp.footwear div#product-container.clear div.pdp-buying-tools-容器 div.pdp-box div.buying-tools-container div#PDPBuyingTools.buying-tools-gadget form.add-to-cart-form div.product-selections div.size-quantity span.sizeDropdown a.selectBox

    我们可以精简到:

    div.footwear form.add-to-cart-form span.sizeDropdown a.size-dropdown

    对于一个合理的选择器,它可能会在微不足道的页面更改中幸存下来,并且不太可能在不需要的页面/产品上触发.

    ~~~~~~~~~~~~~
    请注意,Firebug 还可以帮助我们查看哪些事件附加到哪些内容上,这在确定我们需要触发的内容时至关重要.例如,对于那个节点,我看到:

    那个链接没有href,也不监听click 事件.在这种情况下,我们必须触发 mousedown(或 keydown).

    ~~~~~~~~~~~~~
    对其他 4 个关键节点使用类似的过程,我们获得以下 CSS/jQuery 选择器:

    节点1:div.footwear form.add-to-cart-form span.sizeDropdown a.size-dropdown节点2:ul.selectBox-dropdown-menu li a:contains('10')(但这需要额外检查)节点3:div.footwear form.add-to-cart-form span.sizeDropdown a.selectBox span.selectBox-label:contains('(10)')节点 4:div.footwear form.add-to-cart-form div.product-selections div.add-to-cart节点 5:div.mini-cart div.cart-item-data a.checkout-button:visible


  • 最后,我们使用 waitForKeyElements 将所需的事件发送到关键节点,并按照正确的操作顺序进行排序.

  • 生成的完整的工作脚本是:

    //==用户脚本==//@name _Nike 自动买鞋(!!!) 脚本//@include http://store.nike.com/*//@include https://store.nike.com/*//@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//==/用户脚本==/*- 需要@grant 指令来解决设计更改在 GM 1.0 中引入.它恢复沙箱.*/var targetShoeSize = "10";//-- 第 1 步:激活大小下拉菜单.等待键元素 ("div.footwear form.add-to-cart-form span.sizeDropdown a.size-dropdown",激活大小下拉);函数 activateSizeDropdown (jNode) {triggerMouseEvent (jNode[0], "mousedown");//- 设置步骤 2.等待键元素 ("ul.selectBox-dropdown-menu li a:contains('" + targetShoeSize + "'):visible",选择所需的鞋码);}//-- 第 2 步:选择所需的鞋码.功能选择DesiredShoeSize(jNode){/*-- 因为这个节点的选择器容易误报,我们需要在这里进行额外的检查.*/if ($.trim (jNode.text () ) === targetShoeSize) {//-- 该节点需要一个三重事件triggerMouseEvent (jNode[0], "mouseover");triggerMouseEvent (jNode[0], "mousedown");triggerMouseEvent (jNode[0], "mouseup");//- 设置步骤 3 和 4.等待键元素 (div.footwear form.add-to-cart-form span.sizeDropdown a.selectBox"+ "span.selectBox-label:contains('(" + targetShoeSize + ")')",waitForShoeSizeDisplayAndAddToCart);}}//-- 第 3 步和第 4 步:等待鞋码显示并添加到购物车.函数 waitForShoeSizeDisplayAndAddToCart (jNode) {var addToCartButton = $(div.footwear form.add-to-cart-form div.product-selections div.add-to-cart");triggerMouseEvent (addToCartButton[0], "click");//- 设置步骤 5.等待键元素 ("div.mini-cart div.cart-item-data a.checkout-button:visible",单击结帐按钮);}//-- 第 5 步:单击结帐按钮.函数 clickTheCheckoutButton (jNode) {triggerMouseEvent (jNode[0], "click");//-  全部完成.结帐页面应加载.}函数triggerMouseEvent(节点,事件类型){var clickEvent = document.createEvent('MouseEvents');clickEvent.initEvent (eventType, true, true);node.dispatchEvent (clickEvent);}

    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.

    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.

    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.

    // ==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 <option> 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);
    }
    


    Link to the "target page"
    Snapshot of the target HTML (in case the target page is removed or changed by Nike)

    解决方案

    Rather than just alter the script from the question, I hope to make a quick outline of how to script these kinds of pages and actions with Greasemonkey/Tampermonkey.

    The steps are:

    1. Take careful note of what you do manually. Take special note of elements added/altered by the page's javascript, and the needed sequence of steps, if any.

    2. Using Firebug, and/or Firefox's inspector, and/or Chrome's Developer tools, determine CSS/jQuery selector's for all of the elements you will read or manipulate. This is especially easy to do using Firebug.

    3. Use jQuery to manipulate static HTML. Use waitForKeyElements to handle nodes added or changed by javascript (AJAX). Use the Greasemonkey API -- which is also supported by Tampermonkey and partially supported by Chrome userscripts -- to do any cross-domain page calls, or to store any values between page loads for cross-domain sets of pages.



    Specific example:

    1. For the OP's target pages, the OP wants to: (a) automatically select the shoe size, (b) add the shoes to the shopping cart, and (c) click the checkout button.

      This requires waiting for, and/or clicking on, five (5) page elements like so:


    2. Using Firebug (or similar tool) we obtain the HTML structure for the key nodes. For example, the SIZE dropdown has HTML like this:

      <div class="size-quantity">
          <span class="sizeDropdown selectBox-open">
              ...
              <label class="dropdown-label selectBox-label-showing">SIZE</label>
              ...
              <a class="selectBox size-dropdown mediumSelect footwear selectBox-dropdown" ...>
                  ...
              </a>
          </span>
      </div>
      

      Where the link actually fires off a mousedown event, not a click.

      Firebug gives us a CSS path of:

      html.js body div#body div#body-wrapper.fullheight div#body-liner.clear div#content div#pdp.footwear div#product-container.clear div.pdp-buying-tools-container div.pdp-box div.buying-tools-container div#PDPBuyingTools.buying-tools-gadget form.add-to-cart-form div.product-selections div.size-quantity span.sizeDropdown a.selectBox
      

      Which we can pare down to:

      div.footwear form.add-to-cart-form span.sizeDropdown a.size-dropdown
      

      for a reasonable selector that's likely to survive trivial page changes and unlikely to trigger on unwanted pages/products.

      ~~~~~~~~~~~~~
      Note that Firebug also helps us see what events are attached to what, which is crucial when determining what we need to trigger. For example, for that node, I see:

      That link has no href, nor does it listen for click events. In this case, we must trigger a mousedown (or keydown).

      ~~~~~~~~~~~~~
      Using a similar process for the other 4 key nodes, we obtain CSS/jQuery selectors of:

      Node 1:     div.footwear form.add-to-cart-form span.sizeDropdown a.size-dropdown
      
      Node 2:     ul.selectBox-dropdown-menu li a:contains('10')
                  (But this will need an additional check)
      
      Node 3:     div.footwear form.add-to-cart-form span.sizeDropdown a.selectBox span.selectBox-label:contains('(10)')
      
      Node 4:     div.footwear form.add-to-cart-form div.product-selections div.add-to-cart
      
      Node 5:     div.mini-cart div.cart-item-data a.checkout-button:visible
      


    3. Finally, we use waitForKeyElements to send the required events to the key nodes and to sequence through the proper order of operations.

    The resulting, complete, working script is:

    // ==UserScript==
    // @name     _Nike auto-buy shoes(!!!) script
    // @include  http://store.nike.com/*
    // @include  https://store.nike.com/*
    // @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 targetShoeSize  = "10";
    
    //-- STEP 1:    Activate size drop-down.
    waitForKeyElements (
        "div.footwear form.add-to-cart-form span.sizeDropdown a.size-dropdown",
        activateSizeDropdown
    );
    function activateSizeDropdown (jNode) {
        triggerMouseEvent (jNode[0], "mousedown");
    
        //-- Setup step 2.
        waitForKeyElements (
            "ul.selectBox-dropdown-menu li a:contains('" + targetShoeSize + "'):visible",
            selectDesiredShoeSize
        );
    }
    
    //-- STEP 2:    Select desired shoe size.
    function selectDesiredShoeSize (jNode) {
        /*-- Because the selector for this node is vulnerable to false positives,
            we need an additional check here.
        */
        if ($.trim (jNode.text () ) === targetShoeSize) {
            //-- This node needs a triplex event
            triggerMouseEvent (jNode[0], "mouseover");
            triggerMouseEvent (jNode[0], "mousedown");
            triggerMouseEvent (jNode[0], "mouseup");
    
            //-- Setup steps 3 and 4.
            waitForKeyElements (
                "div.footwear form.add-to-cart-form span.sizeDropdown a.selectBox "
                + "span.selectBox-label:contains('(" + targetShoeSize + ")')",
                waitForShoeSizeDisplayAndAddToCart
            );
        }
    }
    
    //-- STEPS 3 and 4: Wait for shoe size display and add to cart.
    function waitForShoeSizeDisplayAndAddToCart (jNode) {
        var addToCartButton = $(
            "div.footwear form.add-to-cart-form div.product-selections div.add-to-cart"
        );
        triggerMouseEvent (addToCartButton[0], "click");
    
        //-- Setup step 5.
        waitForKeyElements (
            "div.mini-cart div.cart-item-data a.checkout-button:visible",
            clickTheCheckoutButton
        );
    }
    
    //-- STEP 5:    Click the checkout button.
    function clickTheCheckoutButton (jNode) {
        triggerMouseEvent (jNode[0], "click");
    
        //-- All done.  The checkout page should load.
    }
    
    function triggerMouseEvent (node, eventType) {
        var clickEvent = document.createEvent('MouseEvents');
        clickEvent.initEvent (eventType, true, true);
        node.dispatchEvent (clickEvent);
    }
    

    这篇关于在 AJAX 驱动的站点上选择和激活正确的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆