I am trying to make a chrome extension that constantly checks for the button with the ID "product-addtocart-button", and as soon as it is found it will click.
I have came together with this javascript by learning and researching online. I don't know much about javascript, so I don't really know what is going wrong.
My old javascript file was very bare and I had it set up so when I clicked the extension button, the button would be automatically clicked.
Code:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id,{
code: "document.getElementById('product-addtocart-button').click();"
});
});
Now, from research, I have (attempted to) added a function where after clicking the extension button the script would loop check for the actual extension and then once found, click it.
background.js:
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);
}
}
});
});
When I click on the chrome extension button nothing happens. Any idea what is going on?