在 Chrome 浏览器扩展程序中使用命令和热键

发布于 2025-01-10 07:03:20 字数 1711 浏览 0 评论 0原文

我正在尝试制作一个简单的浏览器扩展,当我使用热键时运行一个功能。但是,即使我觉得我已经遵循了 文档非常密切(以及其他教程)。

chrome.runtime.onInstalled 可以很好地触发toggleDark()。然而,热键却没有。

注意 - 我已经尝试了大量不同的命令,以确保不仅仅是 CTRL+U 被另一个扩展或浏览器命令覆盖。

 {
  "manifest_version": 3,
  "name": "extension",
  "version": "0.0.1",
  "description": "description of the extension",
  "icons": {
    "16": "icon.png",
    "48": "icon.png",
    "128": "icon.png"
  },
  "action": {
    "default_icon": {
      "16": "icon.png",
      "24": "icon.png",
      "32": "icon.png"
    },
    "default_title": "Name of extension",
    "default_popup": "popup/popup.html"
  },
  "commands": {
    "_execute_action": {
      "suggested_key": {
        "default": "Ctrl+U",
        "mac": "Command+U"
      },
      "description": "Does a thing"
    }
  },
  "options_page": "options.html",
  "permissions": [
    "activeTab",
    "contentSettings",
    "contextMenus",
    "scripting",
    "storage"
  ],
  "background": {
    "service_worker": "background.js"
  }
}
// This works in background.js
chrome.runtime.onInstalled.addListener(() => {
  toggleDark();
});

// This doesn't work in background.js or popup.js
chrome.action.onClicked.addListener((tab) => {
  console.log("Console log doesn't make it this far");
  chrome.scripting.executeScript({
    target: { tabId: tab.id },
    function: toggleDark,
  });
});

function toggleDark() {
  console.log("toggledark");
}

注意 - 根据下面的评论,我尝试将不起作用的脚本添加到我的 popup.js 文件的顶部。但是,它仍然没有运行。

I'm trying to make a simple browser extension that runs a function when I use a hotkey. However, I can't get the simplest hotkey to work in the background.js file even though I feel like I've followed the documentation very closely (and other tutorials).

chrome.runtime.onInstalled fires off toggleDark() just fine. However, the hotkey does not.

Note - I've tried a ton of different commands to make sure it wasn't just CTRL+U being overridden by another extension or browser command.

 {
  "manifest_version": 3,
  "name": "extension",
  "version": "0.0.1",
  "description": "description of the extension",
  "icons": {
    "16": "icon.png",
    "48": "icon.png",
    "128": "icon.png"
  },
  "action": {
    "default_icon": {
      "16": "icon.png",
      "24": "icon.png",
      "32": "icon.png"
    },
    "default_title": "Name of extension",
    "default_popup": "popup/popup.html"
  },
  "commands": {
    "_execute_action": {
      "suggested_key": {
        "default": "Ctrl+U",
        "mac": "Command+U"
      },
      "description": "Does a thing"
    }
  },
  "options_page": "options.html",
  "permissions": [
    "activeTab",
    "contentSettings",
    "contextMenus",
    "scripting",
    "storage"
  ],
  "background": {
    "service_worker": "background.js"
  }
}
// This works in background.js
chrome.runtime.onInstalled.addListener(() => {
  toggleDark();
});

// This doesn't work in background.js or popup.js
chrome.action.onClicked.addListener((tab) => {
  console.log("Console log doesn't make it this far");
  chrome.scripting.executeScript({
    target: { tabId: tab.id },
    function: toggleDark,
  });
});

function toggleDark() {
  console.log("toggledark");
}

Note - Based on the comments below, I've tried adding the script that doesn't work to the top of my popup.js file. However, it still doesn't run.

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

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

发布评论

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