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.
manifest.json:
{
"description": "Click a button with ID=product-addtocart-button",
"manifest_version": 2,
"name": "click-product-addtocart-button",
"version": "0.1",
"permissions": [
"activeTab"
],
"background": {
"scripts": [
"background.js"
]
},
"browser_action": {
"default_icon": {
"32": "icon.png"
},
"default_title": "Click product-addtocart-button"
}
}
background.js:
var button = document.getElementById("product-addtocart-button");
var time = 10;
chrome.browserAction.onClicked.addListener(function(tab)
{
chrome.tabs.executeScript(tab[0],
function waitForElementToDisplay(button, time) {
if(document.querySelector(button)!=null)
{
document.getElementById(button).click();
return;
}
else
{
setTimeout(function() {
waitForElementToDisplay(button, time);
}, time);
}
}
);
}
);
popup.html:
<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<style>
body {
}
#status {
}
</style>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>
I am getting these errors:
Error in event handler for browserAction.onClicked:
*which points me to this:
Then, this error
Error: Invocation of form tabs.executeScript(undefined, function) doesn't match definition tabs.executeScript(optional integer tabId, object details, optional function callback)
*which points me to this:
What do I do?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…