I am trying to automatically click a button through javascript when the page is loaded. I do not have access to HTML file; therefore, I have to fetch the class of the button and implement through javascript. I have tried many things, even tried to import jQuerry in JS file but nothing seems to work.
Error 1) If I call click function solely on JS than it gives error that click is not defined
Error 2) If I import jQuerry and use built in function, it gives error of ‘$’ not defined.
Here is the code
// //Save Draft trigger
// var draft = await document.getElementsByClassName('Button_button__1HmfN Button_primary__7rvYY Button_inline__3k2so Button_truncated__3pQ92')
// draft.click();
//document.querySelectorAll('Button_button__1HmfN Button_primary__7rvYY Button_inline__3k2so Button_truncated__3pQ92').click();
// window.onload = function(){
// var draft = document.querySelectorAll('.Button_button__1HmfN Button_primary__7rvYY Button_inline__3k2so Button_truncated__3pQ92');
// draft.click();
// //document.querySelectorAll('.Button_button__1HmfN Button_primary__7rvYY Button_inline__3k2so Button_truncated__3pQ92').dispatchEvent(new MouseEvent("click"));
// //document.querySelectorAll('.Button_button__1HmfN Button_primary__7rvYY Button_inline__3k2so Button_truncated__3pQ92').dispatchEvent(new MouseEvent("click"))
// // setInterval(function(){
// // draft.click();
// // },1000); // clicks again every 1000 miliseconds
// }
document.head.innerHTML += "<script src='https://code.jquery.com/jquery-3.6.0.min.js' integrity='sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=' crossorigin='anonymous'></script>"
var script = document.createElement('script');
var x = setInterval(function(){
if(typeof jQuery !== 'undefined'){
// do your stuff
$(document).ready(function(){
$('.Button_button__1HmfN Button_primary__7rvYY Button_inline__3k2so Button_truncated__3pQ92').trigger('click');
});
} else {
console.log('it did not work')
}
}, 50);
// script.src = 'https://code.jquery.com/jquery-3.6.0.min.js';
// script.type = 'text/javascript';
// document.getElementsByTagName('head')[0].appendChild(script);
// var waitForLoad = function () {
// if(typeof jQuery != "undefined"){
// $.get("myfile.php")
// } else{
// window.setTimeout(waitForLoad, 1000)
// }
// }
// window.setTimeout(waitForLoad, 1000)
// $(document).ready(function(){
// $('.Button_button__1HmfN Button_primary__7rvYY Button_inline__3k2so Button_truncated__3pQ92').trigger('click');
// });
// var draft = document.querySelectorAll('.Button_button__1HmfN Button_primary__7rvYY Button_inline__3k2so Button_truncated__3pQ92');
// draft.click();
//document.getElementsByClassName('Button_button__1HmfN Button_primary__7rvYY Button_inline__3k2so Button_truncated__3pQ92').click();
//document.querySelector('button.Button_button__1HmfN Button_primary__7rvYY Button_inline__3k2so Button_truncated__3pQ92').click();
//console.log(title);
//console.log(product.title);