【问题标题】:Are Firefox Keyboard Shortcuts only activated at browser start-up?Firefox 键盘快捷键是否仅在浏览器启动时激活?
【发布时间】:2014-07-03 16:08:09
【问题描述】:

在 Firefox 无需重启的插件中,键盘快捷键在禁用和启用插件后会消失。控制台不记录任何错误 (try{}catch{})。

在检查Browser Toolbox 时,key 被插回<keyset id="mainKeyset">,但快捷方式不起作用,并且修饰符不会出现在上下文菜单中。

所以问题是,我错过了什么还是键盘快捷键仅在浏览器启动时激活?

【问题讨论】:

  • 既然你自己解决了这个问题(太好了!),正确的方法是回答你自己的问题并接受你自己的答案,而不是编辑你的问题;)

标签: javascript firefox firefox-addon firefox-addon-restartless


【解决方案1】:

我发现了问题……

虽然keysetcontext menuitem 的(创建/插入)顺序在浏览器启动时并不重要,但在重新启用无重启插件时却很重要。将keyset创建/插入移到menuitem创建/插入之前,解决了问题。

根据要求:
起初我先有/* ContextMenu Menuitem */ 部分,然后是/* keyset */ 部分。 通过放置 /* 键集 */section first, thekeysetwas created & inserted before inserting thecontextmenu menutiem` 并解决了问题。

let contextMenu = window.document.getElementById('contentAreaContextMenu');

/* keyset */
let mainKeyset = window.document.getElementById('mainKeyset'); // parent -> #main-window
let keyset = window.document.createElement('keyset');
//keyset.setAttribute('id', this.id + '-keyset'); // if you need to have an id
let key = window.document.createElement('key');
key.setAttribute('id', this.id + '-key');
key.setAttribute('modifiers', 'accel shift');
key.setAttribute('keycode', 'VK_F2');
key.setAttribute('oncommand', 'void(0);');
key.addEventListener('command', this, false);
keyset.appendChild(key); // add the key to keyset
mainKeyset.parentNode.appendChild(keyset);  // add the keyset to the window.document

/* ContextMenu Menuitem */
let docfrag = window.document.createDocumentFragment(); // temporary container
let menuseparator = window.document.createElement('menuseparator');
let menuitem = window.document.createElement('menuitem');
//menuitem.setAttribute('id', this.id + '-menuitem'); // if you need to have an id
menuitem.setAttribute('class', 'menuitem-iconic');
menuitem.setAttribute('label', this.menuitemLabel);
//menuitem.setAttribute('hidden', 'true'); // starts from hidden
menuitem.setAttribute('accesskey', 'R');
menuitem.setAttribute('key', this.id + '-key');
//menuitem.style.listStyleImage = 'url(chrome://' + this.id  + '/skin/icon16.png)'; // this also works
menuitem.setAttribute('style', 'list-style-image: url(chrome://' + this.id  + '/skin/icon16.png);');
menuitem.addEventListener('command', this, false);

docfrag.appendChild(menuseparator); // adding the menuseparator to temporary container
docfrag.appendChild(menuitem); // add the menuitem to temporary container
contextMenu.appendChild(docfrag); // add the temporary container to the contextMenu

【讨论】:

  • 感谢分享人。您可以发布您在修复之前和之后所做的代码吗?这会有很大帮助。
  • @Noitidart ...我为您提供了最终代码。之前的代码完全相同,但/* keyset */ 部分在/* ContextMenu Menuitem */ 之后
猜你喜欢
  • 1970-01-01
  • 2010-12-31
  • 2011-04-10
  • 2019-03-29
  • 2010-12-15
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
  • 2022-12-31
相关资源
最近更新 更多