在 Chrome 浏览器扩展程序中使用命令和热键
我正在尝试制作一个简单的浏览器扩展,当我使用热键时运行一个功能。但是,即使我觉得我已经遵循了 文档非常密切(以及其他教程)。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论