如何在Chrome扩展程序中调度KeyboardEvent

时间:2017-06-23 20:47:34

标签: javascript google-chrome google-chrome-extension keyboard-events

Chrome是否允许通过在扩展程序中使用内容脚本来触发KeyboardEvent?我试图将输入到页面的每个字符更改为相同的字符,但是当我加载扩展名时," dispatchEvent()"函数给了我" Uncaught RangeError:超出最大调用堆栈大小",有人可以检查我的代码吗?谢谢!

所以在内容脚本中我首先标准键盘输入,然后创建一个新的keyboardevent并发送它。错误发生在" dispatchEvent()"功能

的manifest.json

{
    "manifest_version": 2,
    "name": "Input_Enc",
    "description": "This is to encrypt the input",
    "version": "1.0",

    "browser_action":{
        "default_title": "Test"
    },

    "content_scripts": [
    {   "matches": ["http://*/*", "https://*/*"],
        "js": ["jquery.js", "myScript.js"],
        "run_at": "document_start"
    }
    ]

}

myScript.js

window.addEventListener("keydown", myFunc);

function myFunc(event){
    event.preventDefault();
    event.stopImmediatePropagation();

    var evt = document.createEvent('KeyboardEvent');
    evt.initKeyboardEvent('keydown', true, true, window, false, false, false, false, 69, 69);
    evt.keyCode = 69;
    evt.which = 69;
    evt.charCode = 69;
    document.dispatchEvent(evt);
}

0 个答案:

没有答案