通过弹出窗口或选项分配命令键盘快捷方式 [英] Assign command keyboard shortcut from popup or options
问题描述
Chrome API是否可以让用户从扩展程序弹出窗口或选项页面中分配键盘快捷键?无需他们去扩展页面,滚动到底部并打开键盘快捷菜单.
Is it possible with the Chrome API to let users assign a keyboard shortcut from within the extension popup or options page? Without them having to go to extensions page, scroll to the bottom and open keyboard shortcut menu.
推荐答案
在Chrome中,无法通过编程方式分配快捷键,但是您可以在扩展程序弹出窗口中添加按钮或链接,以打开内置对话框.
In Chrome there's no method to assign a shortcut key programmatically, but you can add a button or a link in the extension popup that will open the built-in dialog.
popup.html:
popup.html:
<button id="hotkey">Assign a shortcut key</button>
<script src="popup.js"></script>
popup.js:
document.getElementById('hotkey').onclick = () => chrome.tabs.create({
url: 'chrome://extensions/configureCommands'
});
注意:
-
chrome://URL只能通过chrome/WebExtensions API方法打开,
但不能直接通过<a href="...">
链接.
chrome:// URLs can be opened only via chrome/WebExtensions API methods,
but not via<a href="...">
links directly.
您仍然可以将标准的<a>
链接与上面显示的点击侦听器一起使用;只是别忘了阻止默认的click事件以避免控制台中的错误:
You can still use a standard <a>
link with a click listener shown above; just don't forget to prevent the default click event to avoid an error in the console:
document.getElementById('hotkey').onclick = event => {
chrome.tabs.create({url: 'chrome://extensions/configureCommands'});
event.preventDefault();
};
在Opera浏览器中,URL为opera://settings/configureCommands
您可以使用navigator.userAgent
字符串
In Opera browser the URL is opera://settings/configureCommands
You can detect the browser using navigator.userAgent
string
在Firefox中,当前无法以编程方式打开此用户界面,因此您必须显示说明以打开about:addons
页面,单击齿轮图标,然后选择管理扩展快捷方式".但是,Firefox允许使用 browser.commands.update .
In Firefox there's currently no way to open this UI programmatically so you'll have to show an instruction to open about:addons
page, click the gear icon, then choose "Manage extension shortcuts". However, Firefox allows setting the hotkeys programmatically using browser.commands.update.
这篇关于通过弹出窗口或选项分配命令键盘快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!