简体   繁体   中英

Tampermonkey Script - How to prevent infinite submitting

I'm trying to automate a form filling and submitting by coding a Tampermonkey script.

Below is my script:

 // ==UserScript== // @name FBA Calculator // @namespace http://use.iEyour.homepage/ // @version 0.1 // @description enter something useful // @match https://sellercentral.amazon.com/fba/profitabilitycalculator/index?lang=en_US // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js // @copyright 2012+, You // ==/UserScript== $(document).ready(function(){ $("#search-string").val("B017C1Q7TM"); $("input.a-button-input[type='submit']").eq(0).click(); }); 

but when the script works, it redirects to the same url, so the script just run again at this matching url, so it keeps submitting, of course that is not what I meant to do.

Any help to fix this would be much appreciated!!!

Perform the action only if the "loading" indicator is not visible:

if (!$("#please-wait-loading").is(":visible")) {
    $("#search-string").val("B017C1Q7TM");
    $('#search-form input[type="submit"]').click();
}

Or use your own flag:

if (!document.body.__searching) {
    document.body.__searching = true;
    $("#search-string").val("B017C1Q7TM");
    $('#search-form input[type="submit"]').click();
}

Thanks WOxxOm,that is a very inspiring answer.

Only thing is the default value of the input box is "Enter your product name, UPC, EAN, ISBN, or ASIN". So I change the code as below.

 // ==UserScript== // @name FBA Calculator Test // @namespace http://use.iEyour.homepage/ // @version 0.1 // @description enter something useful // @match https://sellercentral.amazon.com/fba/profitabilitycalculator/index?lang=en_US // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js // @copyright 2012+, You // ==/UserScript== $(document).ready(function(){ if($("#search-string").val() == "Enter your product name, UPC, EAN, ISBN, or ASIN") { $("#search-string").val("B017C1Q7TM"); $("input.a-button-input[type='submit']").eq(0).click(); } }); 

But unfortunately if you try to run the code, you will find every time it submits, it still redirects to the same page, and still keep submitting :(

Any other idea to fix?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM