I'm trying to create a simple script to autoclick a buy button as soon as the browser goes from the cart page to the checkout page. The script doesn't work unless I manually refresh the checkout page. It appears the site uses AJAX.
I started with this simple script
// ==UserScript==
// @name Auto place order
// @version 0.1
// @description Autoclicks on buy button
// @author You
// @match https://www.mywebsite.com/buy/placeOrder
// @grant none
// ==/UserScript==
(function() {
'use strict';
alert("Matched"); //debug
const buybutton = document.querySelector('#submitOrderButtonId-announce');
if(buybutton) {
buybutton.click()
}
})();
Upon reading other suggestions I tried the below
// ==UserScript==
// @name Auto place order
// @version 0.2
// @description Autoclicks on buy button
// @author You
// @match https://www.mywebsite.com/buy/placeOrder
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// ==/UserScript==
waitForKeyElements ("#submitOrderButtonId", () => {
alert("Matched");
document.querySelector('#submitOrderButtonId-announce').click(); });
but it didn't help. Still needs refresh.
If I use the main domain in @match it works but I can't set it like that.
Could anyone please suggest the exact code I should write to avoid refreshing?