Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我怎样才能避免把焦点转移到chrome扩展中的google搜索栏?_Javascript_Jquery_Javascript Events_Google Chrome Extension_Vimperator - Fatal编程技术网

Javascript 我怎样才能避免把焦点转移到chrome扩展中的google搜索栏?

Javascript 我怎样才能避免把焦点转移到chrome扩展中的google搜索栏?,javascript,jquery,javascript-events,google-chrome-extension,vimperator,Javascript,Jquery,Javascript Events,Google Chrome Extension,Vimperator,我正在为chrome编写一个扩展,其中包含了我发现在firefox的vimperator插件中最有用的一些功能 目前,我在抓取击键之前遇到了一些问题。最简单的例子是。当我在搜索字段中没有焦点的情况下键入某个内容时,该字段将自动选中,并且我输入的任何文本都将输入该字段 基本上,我想停止这种行为,这样当我按下按钮时,焦点不会移动到搜索字段。之后,我希望分机根据按下的键做出反应,但如果我能阻止焦点移动,我已经或多或少地工作了 到目前为止,我已经尝试了removeEventListener和jquery

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

目前,我在抓取击键之前遇到了一些问题。最简单的例子是。当我在搜索字段中没有焦点的情况下键入某个内容时,该字段将自动选中,并且我输入的任何文本都将输入该字段

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

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

我很抱歉以前有人问过我这个问题,但是我找不到任何问题的帮助

PS:如果您对更多的上下文感兴趣,可以找到我到目前为止的代码。但是我认为这个问题是可以回答的,没有人会因为看到这样的混乱而感到头痛。

在阅读了之后,我编写了以下代码,以在焦点调用返回到事件循环之前模糊文档所聚焦的元素

我们的想法是在每个元素中添加一个焦点侦听器,然后在加载后删除焦点侦听器,这样在用户事件(如jsfiddle.com或谷歌结果页面)后调用焦点的网站在页面加载后仍能正常工作

警告:我还没有弄明白如何让Chrome禁用

内容脚本称之为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]);
}

谢谢你的回答。我发现关于js中的事件有很多我不知道的事情。我还没有时间做这么多测试,可能有一段时间我也没有时间,但我会在看到它后再报告。
{
  "name": "unfocus",
  "version": "1.0",
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["unfocus.js"],
      "run_at": "document_start"
    }
  ]
}