Chrome extension javascript

拜拜、爱过 提交于 2020-06-29 03:55:28

问题


https://i.stack.imgur.com/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".


回答1:


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"
      ]
    }
  ]
}


来源:https://stackoverflow.com/questions/62348183/chrome-extension-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!