-3

Hi guys, 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.

I guess if it's necessary, the site is Adidas.ae and once a new shoe comes out there will be a countdown to the shoe being available, and once the countdown is over the button add to card will be available to click, and I need to click that right away.

--

I took in @Barmar's suggestion and changed the id"product-addtocard-button" to a variable and using it in the function parameters. I also took away the braces from the function after "tab.id".

My original code:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id,{
function waitForElementToDisplay(#product-addtocart-button, 10) {
        if(document.querySelector(#product-addtocart-button)!=null) {
            alert("document.getElementById('product-addtocart-button').click()")
            return;
        }
        else {
            setTimeout(function() {
                waitForElementToDisplay(#product-addtocart-button, 10);
            }, 10);
        }
    }

        });
});

The updated code:

var button = document.getElementById("product-addtocart-button");
var time = 10;

chrome.browserAction.onClicked.addListener(function(tab) 
    {
    chrome.tabs.executeScript(tab.id,

        function waitForElementToDisplay(button, time) {
                if(document.querySelector(button)!=null) 
                {
                    code: "document.getElementById(button).click();"
                    return;
                }
                else 
                {
                    setTimeout(function() {
                        waitForElementToDisplay(button, time);
                    }, time);
                }
            }
        );
    }
);

Note: This is an extension to this question: Why is my Javascript Chrome extension code not working? (Loop check for button). I have taken in the suggestions and need a little more help, therefore am posting my updated code.

The problem with the extension is when it is clicked nothing is happening. Any help?

Community
  • 1
  • 1
  • 1
    you have syntax errors. look closely how the callback should be defined. question can get closed as typo. – Zig Mandel Oct 28 '16 at 13:08
  • Without the error messages from the console (specifically, ones related to syntax), we can't do much with "nothing is happening." – ssube Oct 28 '16 at 13:57
  • @ssube I have provided the error messages here:http://stackoverflow.com/questions/40303653/chrome-extension-loop-check-for-button-errors – Morris Park Oct 28 '16 at 14:20
  • Thanks everyone for downvoting and marking as off-topic... Hope you feel better about yourselves you heroes – Morris Park Oct 30 '16 at 14:39

1 Answers1

0

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"});
});
Iván Nokonoko
  • 4,888
  • 2
  • 19
  • 27
  • You are a legend man!!! Works exactly how I want to. I kept posting because I was scared nobody would see the question, based on I am in different time regioun (United arab emirates) and time difference in America nobody would see. thank you very much from Dubai :) – Morris Park Oct 28 '16 at 15:22
  • And still someone downvoted it... :( – Iván Nokonoko Oct 28 '16 at 15:37