Chrome扩展循环检查按钮错误 [英] Chrome Extension loop check for button Errors
问题描述
我试图创建一个Chrome扩展,一旦我点击Chrome扩展,脚本就会启动,并会循环检查ID为product-addtocart-button的按钮的每1毫秒。所以,一旦循环找到按钮,它需要立即点击。
manifest.json:
<$ p
description:点击一个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;
setTimeout(function(){
waitForElementToDisplay(button,time);
},time);
}
}
);
}
);
popup.html:
<!doctype html>
< html>
< head>
< title>入门Extension的弹出式菜单< / title>
< style>
body {
}
#status {
}
< / style>
< script src =popup.js>< / script>
< / head>
< body>
< / body>
< / html>
我收到这些错误:
browserAction.onClicked的事件处理程序错误:
*它指向我:
然后,这个错误
错误:调用表单tabs.executeScript(undefined,function )不匹配定义tabs.executeScript(可选的整数tabId,对象详细信息,可选的函数回调)
*它指向我:
,您必须调用executeScript,如:
chrome.tabs.executeScript(tab.id,{code:yourCodePackedIntoOneString});
或
chrome.tabs.executeScript(tab.id,{file:yourCodeFile.js});
但您打电话给:
chrome.tabs.executeScript(tab.id,
试试这个:
p>有一个名为 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}) ;
});
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?
Answering the question for the 3rd time (please stop posting new questions for the same problem):
According to specifications, you have to invoke executeScript like:
chrome.tabs.executeScript(tab.id,{code:"yourCodePackedIntoOneString"});
or
chrome.tabs.executeScript(tab.id,{file:"yourCodeFile.js"});
but you are calling:
chrome.tabs.executeScript(tab.id,{function()etc...});
.
Try this:
Have one file called myWaitingLoop.js:
function waitForElementToDisplay(){
var button = document.querySelector("#product-addtocart-button");
if (button){
button.click();
} else {
setTimeout(waitForElementToDisplay,100);
}
}
waitForElementToDisplay();
and then, in your background.js script:
chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.executeScript(tab.id,{file:"myWaitingLoop.js"});
});
这篇关于Chrome扩展循环检查按钮错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!