Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

javascript - Chrome Extension loop check for button Errors

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: Pic1 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: Pic2

What do I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"});
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...