2013-05-27 35 views
0

我有一个简单的chrome脚本,它修改了特定网页的tab键。 这工作得很好,直到铬v27来了。这是代码:修改tab键的Chrome用户脚本停止工作了

的script.js:

// ==UserScript== 
// @name   Name 
// @namespace  http://www.someNameSpace.com/ 
// @description Description 
// @include  http://weburl1.com/* 
// @include  http://weburl2.com/* 
// ==/UserScript== 

function key_event(event){ 
    if(event.keyCode == 9){ //get tab pressed 
     /* do something here */ 
    } 
} 
document.addEventListener("keydown", key_event, true); 

manifest.json的:

{ 
    "content_scripts": [ { 
     "all_frames" : true, 
     "exclude_globs": [ ], 
     "include_globs": [ "http://weburl1.com/*", "http://weburl2.com/*" ], 
     "js": [ "script.js" ], 
     "matches": [ "http://*/*", "https://*/*" ], 
     "run_at": "document_idle" 
    } ], 
    "converted_from_user_script": true, 
    "description": "Description", 
    "key": "kVJUyHgHhlZtX2koEeV1ZF7yYHXfLyCyprC+I18+QzI=", 
    "name": "Name", 
    "version": "1.01" 
} 

编辑: 我原来的剧本仍在运行,但只能在initally加载帧。所以我加了

“all_frames”:真实,

到清单中,没有工作。

有什么我可以做的吗? 感谢您的帮助

+0

它调用事件侦听器还是根本不调用事件侦听器? –

+0

我已更新问题... – spoekes

+0

您是否尝试过我的答案? –

回答

0

内容脚本与当前页面不在同一个上下文中。 你应该通过另外的标签

var actualCode = 'function key_event() {' 
       + ' if(event.keyCode == 9){' 
       +  'alert('tab'); 
       + '}'; 

var script = document.createElement('script'); 
script.textContent = actualCode; 
(document.head||document.documentElement).appendChild(script); 
script.parentNode.removeChild(script);