Hi guys, I am trying to create a chrome extension that once I click the chrome extension, the script will start and will loop check every 1 millisecond for a button with the id "product-addtocart-button". So, once the loop finds the button it needs to be clicked right away.
I guess if it's necessary, the site is Adidas.ae and once a new shoe comes out there will be a countdown to the shoe being available, and once the countdown is over the button add to card will be available to click, and I need to click that right away.
--
I took in @Barmar's suggestion and changed the id"product-addtocard-button" to a variable and using it in the function parameters. I also took away the braces from the function after "tab.id".
My original code:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id,{
function waitForElementToDisplay(#product-addtocart-button, 10) {
if(document.querySelector(#product-addtocart-button)!=null) {
alert("document.getElementById('product-addtocart-button').click()")
return;
}
else {
setTimeout(function() {
waitForElementToDisplay(#product-addtocart-button, 10);
}, 10);
}
}
});
});
The updated code:
var button = document.getElementById("product-addtocart-button");
var time = 10;
chrome.browserAction.onClicked.addListener(function(tab)
{
chrome.tabs.executeScript(tab.id,
function waitForElementToDisplay(button, time) {
if(document.querySelector(button)!=null)
{
code: "document.getElementById(button).click();"
return;
}
else
{
setTimeout(function() {
waitForElementToDisplay(button, time);
}, time);
}
}
);
}
);
Note: This is an extension to this question: Why is my Javascript Chrome extension code not working? (Loop check for button). I have taken in the suggestions and need a little more help, therefore am posting my updated code.
The problem with the extension is when it is clicked nothing is happening. Any help?