Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...