用于点击页面的 Google chrome 扩展程序

Google chrome extension to click on a page

提问人:Jhonnyer Mejia 提问时间:8/19/2023 最后编辑:XMehdi01Jhonnyer Mejia 更新时间:8/20/2023 访问量:18

问:

有谁知道我如何纠正错误,扩展程序符合单击,但不是每 5 秒一次,他们知道如何修复它吗?

manifest.json

{
  "manifest_version": 3,
  "name": "Sincronización Automática",
  "version": "1.0",
  "description": "Una extensión para sincronización automática",
  "permissions": ["activeTab", "storage", "scripting", "tabs"],
  "host_permissions": ["<all_urls>"],
  "icons": {
    "128": "logo.png"
  },
  "action": {
    "default_popup": "app.html"
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["script.js"]
    }
  ]
}

应用.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Contador de Tiempo de Sincronización</title>
    <style>
      .container {
        width: 300px;
        margin: auto;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        text-align: center;
        background-color: #1f2c3b;
        color: white;
      }

      #syncTime {
        border-radius: 5px;
        padding: 5px;
        width: 50px;
        margin-top: 5px;
      }

      #inicioS {
        margin-top: 10px;
        background-color: yellow;
        border: 1px solid #aaa;
        border-radius: 5px;
        padding: 8px 16px;
        cursor: pointer;
        font-size: 14px;
        font-weight: bold;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
      }

      h1 {
        margin-top: 0;
      }

      img {
        margin-top: -10px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <img src="/logo.png" alt="Imagen" style="width: 80px" />
      <h1>Tiempo de Sincronización</h1>
      <label for="syncTime">Selecciona el tiempo en segundos:</label>
      <br />
      <input
        type="number"
        id="syncTime"
        name="syncTime"
        min="1"
        step="1"
        value="1"
      />
      <br />
      <button id="inicioS">Comenzar Sincronización</button>
    </div>
    <script src="script.js"></script>
  </body>
</html>

脚本 .js

function injectTheScript(scriptFunction) {
  // Wuery the active tab, which will be only one tab and inject the script function in it.
  chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
    chrome.scripting.executeScript({
      target: { tabId: tabs[0].id },
      func: scriptFunction,
    });
  });
}

function clickButton() {
  const button = document.querySelector(
    'button[data-testid="SyncActionButton"]'
  );
  if (button) {
    button.click();
  }
}

document.getElementById("inicioS").addEventListener("click", () => {
  console.log("Button clicked");
  injectTheScript(clickButton);
  setInterval(clickButton, 5000);
});

如果你需要代码,它在我的 GITHUB 上有做测试的页面 https://github.com/JhonnM62/Extencion-para-appsheet

我尝试了很多代码

javascript css 函数 google-chrome dom

评论


答: 暂无答案