Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

516
Views
Chrome extension content script - .click() not working

I've been trying for hours and I can't get this working. I've written a content script that executes when the user loads a page. The click() method just simply won't work when I try click an element. I've tried multiple ways and it always works from the console, but not when I load the page.

The element I want to click:

<a id="startButton" href="javascript: popupHand.showPopup('popupLoginV2','loginHand.init()','','',true);" class="buttonGreen1 buttonSize1">Megpörgetem</a>

manifest.json - content script part

{
  "content_scripts": [
    {
      "matches": ["*://*.bonuszbrigad.hu/bonuszkerek"],
      "js": ["plugin/jquery-3.2.0.min.js", "js/page_wheel.js"],
      "run_at": "document_end"
    }
  ],
}  

page_wheel.js

function spinWheel() {
    var spinButton = $("#startButton");

    if (spinButton.text() === "Már pörgettél") {
        return "wait";
    }

    else if (spinButton.text() === "Megpörgetem") {
        console.log("spin is available");

        spinButton.on("click",
            function () {
                console.log("inside click action");

                var popupLogin = $("#popup_login");
                console.log(popupLogin);

                if (popupLogin === null) {
                    return "login"
                } else {
                    return "success";
                }
            }
        );

        spinButton.trigger("click");
    }
}

When I load the page, the console logs show that it managed to enter

  • spinButton.on("click", function (){...});

But there weren't any actual clicks made on the website. When I test the click method in the console, both of the below solutions work:

  • $("#startButton").click()
  • document.getElementById("startButton").click();

Also I see the same console logs - when executing click method from the console - but this way it's actually working.

Does anyone have an idea what is the problem? :/

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I got this problem. In my case, I realized that there were HTML DOM and script load synchronizes. So when you call the click event, it was still fulfill loaded. So I added the script that invokes the click event into the window.load and it worked.

My script looks like below:

window.onload = function () {
  const btn = document.getElementById('J_LinkBuy')

  if (btn !== null) {
    btn.dispatchEvent(new MouseEvent('click'))
  }
}
about 4 years ago · Santiago Trujillo Report

0

Solution 1: You can use onclick attribute on any HTML tag and use it to call JavaScript functions.

<a onclick="sayHi('Hi')"> Click me </a>
<script>function sayHi(message){alert(message)}</script>

Solution 2: Use jquery click event function.

Remember, the jquery click should be called only after the target html element is generated. the click call won't work or else

$(css_selector).click();
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!