1

I am trying to make a chrome extension that constantly checks for the button with the ID "product-addtocart-button", and as soon as it is found it will click.

I have came together with this javascript by learning and researching online. I don't know much about javascript, so I don't really know what is going wrong.

My old javascript file was very bare and I had it set up so when I clicked the extension button, the button would be automatically clicked.

Code:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id,{
        code: "document.getElementById('product-addtocart-button').click();"
    });
});

Now, from research, I have (attempted to) added a function where after clicking the extension button the script would loop check for the actual extension and then once found, click it.

background.js:

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

        });
});

When I click on the chrome extension button nothing happens. Any idea what is going on?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The ids are strings, so change `document.querySelector(#product-addtocart-button`) by `document.querySelector("#product-addtocart-button")` – Yosvel Quintero Oct 27 '16 at 18:43
  • Hi, if this is my new 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); } } }); }); It is still doing nothing? – Morris Park Oct 27 '16 at 18:56
  • You seem to have a syntax error, you shouldn't have `{ ... }` around `function waitForElementToDisplay` – Barmar Oct 27 '16 at 19:32
  • 1
    Check your Javascript console for error messages. – Barmar Oct 27 '16 at 19:32
  • 1
    When you're defining a function, the formal parameters must be variable names, not strings and integers. – Barmar Oct 27 '16 at 19:33
  • Your first block of code is identical to what was provided in [this answer](http://stackoverflow.com/a/40248170/3773011). If it was copied from there, you are welcome to use it, but should provide attribution. However, given the relatively generic nature of the code it is quite possible you found it somewhere else. On the other hand, given the lack of understanding of JavaScript and knowledge of what is needed for [chrome.tabs.executeScript()](https://developer.chrome.com/extensions/tabs#method-executeScript) demonstrated by your other code, I doubt that you wrote it. Is this a group project? – Makyen Oct 27 '16 at 23:23
  • It is unclear to me what you want to happen based on "where after clicking the extension button the script would loop check for the actual extension and then once found, click it." Please provide much more detail description of what you are attempting to accomplish. – Makyen Oct 27 '16 at 23:25
  • @Barmar, what should I do instead? Just delete the {}s? Also regarding the variable names and integers I will try fixing that next. – Morris Park Oct 28 '16 at 08:07
  • @Mayken, that seems like it describes it entirely. But if you want it in other words, I want to clikc the chrome extension button, then the script would start loop checking for a button named "product-addtocart-button" and once found, click it. – Morris Park Oct 28 '16 at 08:07
  • @MorrisPark You should use correct Javascript syntax. It looks like you don't understand the basics of how to define functions, and I can't figure out what you're actually trying to do from the totally invalid syntax. Are you trying to pass an anonymous function, or are you trying to call a function? – Barmar Oct 28 '16 at 14:26

2 Answers2

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 that and see how it goes.

Iván Nokonoko
  • 4,888
  • 2
  • 19
  • 27
-1

Use content script file instead of background.js if you want to trigger the click for a specific page.

  "content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "css": ["mystyles.css"],
      "js": ["jquery.js", "myscript.js"]
    }
  ],

download JQuery into your local folder if you want to use JQuery .

if($('#product-addtocart-button').length>0)
$('#product-addtocart-button').click()