Refer to Choosing and activating the right controls on an AJAX-driven site, and use a utility like waitForKeyElements()
.
Here's how to wait for elements and chain states (in case it's needed) for the submit click:
// ==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);
}
To add additional form fields, you would:
Add a new "filled" property to the glblFlags
object. For example:
var glblFlags = {firstFlld: false, lastFlld: false, emailFlld: false, phoneFlld: false};
Add a new waitForKeyElements
line to the first if
section. For example:
waitForKeyElements ("#buyer_phone", jNd => {SetValue (jNd, "phoneFlld", "800-867-5309"); }, true);
The second parameter to SetValue()
is the name of the property you added in step 1.
The third parameter to SetValue()
is the value you wish to fill in the form field with.
Some form controls may require special handling. That's beyond the scope of this question. Search around and then open a new question, if needed.