How to dispatch KeyboardEvent in Chrome extensions

删除回忆录丶 提交于 2020-01-17 06:39:27

问题


Does Chrome allow firing KeyboardEvent by using content script in Extensions? I was trying to change every character entered to the page to the same character, but when I load the extension, the "dispatchEvent()" function gave me "Uncaught RangeError: maximum call stack size exceeded", can someone check my code? Thanks!

So in the content script I first standard keyboard input, then create a new keyboardevent and dispatch it. The error is on "dispatchEvent()" function.

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);
}

来源:https://stackoverflow.com/questions/44729709/how-to-dispatch-keyboardevent-in-chrome-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!