问题
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