Tampermonkey 脚本等待元素然后填充它们

ove*_*oon 6 javascript tampermonkey

我正在尝试完成一个填写结帐表格的脚本。它需要单击一个没有 ID 或名称的按钮,然后填写出现的表单的其余部分。我必须单击按钮的代码是:

document.querySelector('input[type="submit"][value="continue"]').click();
Run Code Online (Sandbox Code Playgroud)

但这对我不起作用,其他元素将出现在我的脚本之后:

// ==UserScript==
// @name         Script
// @include      https://checkout.bigcartel.com/*
// @include      http://*.bigcartel.com/product
// @include      http://*.bigcartel.com/cart
// @grant        none
// ==/UserScript==

// on "/cart" page click checkout button
if (window.location.origin !== "https://checkout.bigcartel.com") document.getElementsByName("checkout")[0].click();
else {
    // fill first three form fields
    document.getElementById("buyer_first_name").value = "John";
    document.getElementById("buyer_last_name").value = "Smith";
    document.getElementById("buyer_email").value = "john@doe.com";
    //click button for next section 
    document.querySelector('input[type="submit"][value="continue"]').click();
}
Run Code Online (Sandbox Code Playgroud)

Bro*_*ams 5

请参阅在 AJAX 驱动的站点上选择和激活正确的控件,并使用诸如waitForKeyElements().

以下是如何等待提交点击的元素和链状态(如果需要):

// ==UserScript==
// @name     _Wait for elements overkill and on separate pages
// @match    https://checkout.bigcartel.com/*
// @match    *://*.bigcartel.com/product*
// @match    *://*.bigcartel.com/cart*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.

//-- Track form field state, just in case. *Might* be needed for multistage forms...
var glblFlags = {firstFlld: false, lastFlld: false, emailFlld: false};

if (location.hostname === "checkout.bigcartel.com") {
    /*-- Fill first three form fields.
        Use separate WFKE's for multistage.  (Fun overkill for simpler forms.)
    */
    waitForKeyElements ("#buyer_first_name", jNd => {SetValue (jNd, "firstFlld", "John"); }, true);
    waitForKeyElements ("#buyer_last_name",  jNd => {SetValue (jNd, "lastFlld",  "Smith"); }, true);
    waitForKeyElements ("#buyer_email",      jNd => {SetValue (jNd, "emailFlld", "john@doe.com"); }, true);

    //-- Click button for next section
    waitForKeyElements (
        "form[action='/shipping'] button:contains('Next')",
        clickWhenFormIsReady, true
    );
}
else if (location.pathname === "/cart") {
    //-- On "/cart" page click checkout button
    waitForKeyElements ("[name='checkout']", clickNode, true);
}

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

function SetValue (jNode, flagVarName, newValue) {
    jNode.val (newValue);
    glblFlags[flagVarName] = true;
}

function clickWhenFormIsReady (jNode) {
    //-- Keep waiting if all flags are not true (form not yet filled out):
    for (let prpName in glblFlags) {
        if (glblFlags.hasOwnProperty (prpName) ) {
            if (glblFlags[prpName] === false)
                return true;
        }
    }
    clickNode (jNode);
}
Run Code Online (Sandbox Code Playgroud)

要添加其他表单字段,您可以:

  1. 向对象添加新的“filled”属性glblFlags。例如:

    var glblFlags = {firstFlld: false, lastFlld: false, emailFlld: false, phoneFlld: false};
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将新waitForKeyElements行添加到第一if部分。例如:

        waitForKeyElements ("#buyer_phone", jNd => {SetValue (jNd, "phoneFlld", "800-867-5309"); }, true);
    
    Run Code Online (Sandbox Code Playgroud)

    to 的第二个参数SetValue()是您在步骤 1 中添加的属性的名称
    。to 的 第三个参数SetValue()是您希望在表单字段中填写的值。

  3. 某些表单控件可能需要特殊处理。这超出了这个问题的范围。四处搜索,然后根据需要提出一个新问题。