Disable the enter key Chrome extension
I want to disable the enter key in Chrome extension using JavaScript for a particular page. For example: On a login page, instead of pressing enter to login the user should click on the Log in button.
I am using message passing in my Chrome extension.
document.addEventListener("k开发者_C百科eydown", function(e) {
var keys = e.which;
chrome.extension.sendRequest({method: "getHTML", data: keys});
});
Just call preventDefault() on your handler to consume the event and stop it's propagation.
To make this work on certain pages (not in every one), you can create 2 content scripts: one with the enter prevention function and another with the rest of the functionality.
Then you would include them on the manifest like this:
{
"name": "My extension",
...
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["common_content_script"]
},
{
"matches": ["http://page_i_want_to_prevent_enter"],
"js" : ["enter_prevention_script"]
}
],
...
}
精彩评论