为什么我的executeScript 不工作? Chrome 扩展开发:

发布于 2025-01-11 12:58:51 字数 1455 浏览 0 评论 0原文

我正在开发一个 chrome 扩展程序,它从用户那里获取一个名称和第二个名称,并用用户所在选项卡上的第二个名称替换所有名称实例。逻辑确实很简单,但是,从用户那里获取输入并在页面上使用它却极具挑战性。

这就是我所拥有的:

Manifest.json:

{
  "manifest_version": 3,
  "name": "Background Changer",
  "version": "1.0.0",
  "description": "Does stuff with the background.",
  "action": {
    "default_title": "lol",
    "default_popup": "index.html"
  },
  "permissions": ["scripting", "tabs", "activeTab"],
  "host_permissions": ["<all_urls>"]
}

index.html

 <h2>Dead Name</h2>
  <input type="text" id="deadName">

  <h2>Real name</h2>
  <input type="text" id="realName">

  <input type="submit" id="submitButton">
    <script src="popup.js"></script>

popup.js

let realName = document.getElementById("realName")
let deadName = document.getElementById("deadName")

document.getElementById("submitButton").addEventListener('submit', injectJS);

const tabId = getTabId();
function injectJS(){
    chrome.scripting.executeScript(
        {
          target: {tabId: tabId, allFrames: true},
          files: ['content.js'],
        },
        () => {});
}

content.js

alert("LOL")

简而言之,此代码旨在测试从扩展弹出窗口向用户所在的选项卡注入 Javascript。尽管我附加了事件侦听器并单击它,但警报并未出现。

I am working on a chrome extension that takes a name and a second name from user, and replaces all instances of name with second name on the tab the user is on. The logic is really easy, however, getting the input from the user and using it on the page is extremely challenging.

This is what I have:

Manifest.json:

{
  "manifest_version": 3,
  "name": "Background Changer",
  "version": "1.0.0",
  "description": "Does stuff with the background.",
  "action": {
    "default_title": "lol",
    "default_popup": "index.html"
  },
  "permissions": ["scripting", "tabs", "activeTab"],
  "host_permissions": ["<all_urls>"]
}

index.html

 <h2>Dead Name</h2>
  <input type="text" id="deadName">

  <h2>Real name</h2>
  <input type="text" id="realName">

  <input type="submit" id="submitButton">
    <script src="popup.js"></script>

popup.js

let realName = document.getElementById("realName")
let deadName = document.getElementById("deadName")

document.getElementById("submitButton").addEventListener('submit', injectJS);

const tabId = getTabId();
function injectJS(){
    chrome.scripting.executeScript(
        {
          target: {tabId: tabId, allFrames: true},
          files: ['content.js'],
        },
        () => {});
}

content.js

alert("LOL")

Simply put, this code is meant to be a test of injecting Javascript from the extension pop-out to the tab the user is on. Despite me attaching the event listener, and clicking on it, the alert does not appear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文