我正在尝试创建一个chrome扩展名,一旦单击chrome扩展名,脚本便会启动,并将每隔1毫秒循环检查一次ID为“ product-addtocart-button”的按钮。因此,一旦循环找到按钮,就需要立即单击它。
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>
我收到这些错误:
browserAction.onClicked事件处理程序中的错误:
错误:表单tabs.executeScript(未定义,函数)的调用与定义tabs.executeScript(可选的整数tabId,对象详细信息,可选的函数回调)不匹配
我该怎么办?
第三次回答问题(请停止发布相同问题的新问题):
根据规范,您必须像这样调用executeScript:
chrome.tabs.executeScript(tab.id,{code:"yourCodePackedIntoOneString"});
或者
chrome.tabs.executeScript(tab.id,{file:"yourCodeFile.js"});
但您正在打电话:
chrome.tabs.executeScript(tab.id,{function()etc...});
。
试试这个:
有一个名为myWaitingLoop.js的文件:
function waitForElementToDisplay(){
var button = document.querySelector("#product-addtocart-button");
if (button){
button.click();
} else {
setTimeout(waitForElementToDisplay,100);
}
}
waitForElementToDisplay();
然后,在您的background.js脚本中:
chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.executeScript(tab.id,{file:"myWaitingLoop.js"});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句