Lumi Project
Lumi Project

Reputation: 3

Chrome extension javascript

https://i.sstatic.net/S0u6W.png

For some context look at the image provided,

There are 3 type of box that appears in the site automatically every few seconds, "Hi", "Hello" and "Hey". You can click the box and it will be added to the right side.

    <div class="h-240 rounded relative bg-gray-400 cursor-pointer hover:bg-gray-300 transition duration-75 ease-in-out">

<span class="inline-block ml-10 text-gray-200"> (Hello)</span>

</div>

<div class="h-240 rounded relative bg-gray-400 cursor-pointer hover:bg-gray-300 transition duration-75 ease-in-out">

<span class="inline-block ml-10 text-gray-200"> (Hi)</span>

<div class="h-240 rounded relative bg-gray-400 cursor-pointer hover:bg-gray-300 transition duration-75 ease-in-out">

    <span class="inline-block ml-10 text-gray-200"> (Hey)</span>

</div>

<div class="h-240 rounded relative bg-gray-400 cursor-pointer hover:bg-gray-300 transition duration-75 ease-in-out">

        <span class="inline-block ml-10 text-gray-200"> (Hey)</span>

    </div>

 <div class="h-240 rounded relative bg-gray-400 cursor-pointer hover:bg-gray-300 transition duration-75 ease-in-out">

    <span class="inline-block ml-10 text-gray-200"> (Hello)</span>

    </div>

How do I make a content script to automatically click any box with "Hello".

Upvotes: 0

Views: 78

Answers (1)

GirkovArpa
GirkovArpa

Reputation: 4912

You can't do this in a background script. It has to be done in a content script.

script.js

setInterval(() => {
  document.querySelectorAll('div').forEach(div => {
    div.getElementsByTagName('span')[0].innerHTML.includes('Hello') && div.click();
  });
}, 1000);

manifest.json

{
  "manifest_version": 2,
  "name": "Hello Box Clicker",
  "permissions": [
  "activeTab"
  ],
  "version": "0.0.0.1",
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "script.js"
      ]
    }
  ]
}

Upvotes: 1

Related Questions