1

我正在为 chrome 编写一个扩展,其中包含一些我认为在 firefox 的 vimperator 插件中最有用的功能。

目前,我在网页之前捕获击键时遇到了一些问题。“最简单”的例子是google.com。当我在搜索字段中没有焦点的情况下键入内容时,会自动选择该字段,并且我输入的任何文本都会输入到该字段中。

本质上我想停止这种行为,以便当我按下按钮时焦点不会移动到搜索字段。(之后我希望扩展根据按下的键做出反应,但如果我可以阻止焦点移动,我已经或多或少地工作了。)

到目前为止,我已经在我的扩展的内容脚本中尝试了 removeEventListener() 和 jquery unbind() 以及其他一些东西(或者疯狂的猜测,如果你喜欢的话。)的各种组合,但到目前为止还没有运气。按下字母数字键时,焦点仍会移至搜索字段。有没有人对如何做到这一点或我可以在哪里寻找答案有任何建议?

我很抱歉以前有人问过这个问题,但我无法从我发现的任何问题中获得任何帮助。

PS:如果您对更多上下文感兴趣,我到目前为止的代码可以在这里找到。但我应该认为可以回答这个问题,而无需任何人看到这个(混乱)而头疼。

4

1 回答 1

0

在阅读了element.focus()方法focus()之后,我编写了以下代码来模糊在调用返回到事件循环之前由文档关注的元素。

我们的想法是,我们为每个元素添加一个焦点监听器,然后在 onload 之后移除焦点监听器,以便focus()在用户事件之后调用的网站(例如 jsfiddle.com 或 Google 结果页面)在页面有之后仍然可以正常工作加载。

警告:我还无法弄清楚如何让 Chrome 禁用autofocus字段

内容脚本(称之为 unfocus.js):

document.addEventListener('DOMNodeInsertedIntoDocument', onInsertedIntoDocument, true);
document.addEventListener('DOMNodeRemovedFromDocument', onRemovedFromDocument, true);
window.addEventListener('load', function(e) {
  setTimeout(function() {
    removeOnFocus(document.documentElement);
    document.removeEventListener('DOMNodeInsertedIntoDocument', onInsertedIntoDocument, true);
    document.removeEventListener('DOMNodeRemovedFromDocument', onRemovedFromDocument, true);
  }, 1);
}, false);


// Whenever an element is inserted into document, listen for
// simple event named 'focus'.
function onInsertedIntoDocument(e) {
  var elt = e.target;
  if (elt.nodeType === 1)
    elt.addEventListener('focus', onfocus, false);
}
function onRemovedFromDocument(e) {
  var elt = e.target;
  if (elt.nodeType === 1)
      removeOnFocus(elt);
}
function onfocus(e) {
  // In Chrome, caller is null if the user initiated the focus,
  // and non-null if the focus was caused by a call to element.focus().
  var causedByUser = (onfocus.caller == null);

  console.log('onfocus ' + e.target.nodeName +
      ': caused by user? ' +causedByUser +
      (e.target.autofocus ? ' autofocus' : ''));

  if (! causedByUser) {
    e.target.blur();
  }
}
// Clean up by removing all the 'focus' event listeners.
function removeOnFocus(elt) {
  elt.removeEventListener('focus', onfocus, false);
  for (var i = 0; i < elt.children.length; i++)
    removeOnFocus(elt.children[i]);
}

而这个 manifest.json:

{
  "name": "unfocus",
  "version": "1.0",
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["unfocus.js"],
      "run_at": "document_start"
    }
  ]
}
于 2011-07-29T23:54:25.223 回答