gpt4 book ai didi

javascript - 在 AJAX 驱动的站点上选择和激活正确的控件

转载 作者:可可西里 更新时间:2023-11-01 01:38:49 26 4
gpt4 key购买 nike

因此,我试图在每次访问任何 nike.com 运动鞋页面(没有 HTML 链接)时,它都会自动选择我的鞋码,将其添加到购物车,并为我结帐。

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

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

// ==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 (以防 Nike 删除或更改目标页面)

最佳答案

我希望快速概述如何使用 Greasemonkey/Tampermonkey 编写此类页面和操作的脚本,而不是仅仅根据问题修改脚本。

步骤是:

  1. 请仔细记下您手动执行的操作。特别注意页面的 javascript 添加/更改的元素,以及所需的步骤顺序(如果有)。

  2. 使用 Firebug 和/或 Firefox 的检查器,和/或 Chrome 的开发者工具,确定所有的 CSS/jQuery 选择器您将阅读或操作的元素。使用 Firebug 尤其容易做到这一点。

  3. 使用 jQuery 来操作静态 HTML。使用 waitForKeyElements处理由 javascript (AJAX) 添加或更改的节点。使用 the Greasemonkey API -- 这也受 Tampermonkey 支持,部分受 Chrome 用户脚本支持 -- 执行任何跨域页面调用,或在跨域页面集的页面加载之间存储任何值。



具体例子:

  1. the OP's target pages ,OP 想要:(a) 自动选择鞋码,(b) 将鞋子添加到购物车,以及 (c) 单击结帐按钮。

    这需要等待和/或点击五 (5) 个页面元素,如下所示:

    Set the size

    Check out


  2. 我们使用 Firebug(或类似工具)获取关键节点的 HTML 结构。例如,SIZE 下拉菜单有这样的 HTML:

    <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>

    链接实际上触发了 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-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

    我们可以简化为:

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

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

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

    Events for key first node

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

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

    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. 最后,我们使用 waitForKeyElements 将所需的事件发送到关键节点并按正确的操作顺序排序。

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

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

关于javascript - 在 AJAX 驱动的站点上选择和激活正确的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15048223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com