K
K
Kirill Morozov2017-03-16 17:09:59
JavaScript
Kirill Morozov, 2017-03-16 17:09:59

How to assign hotkeys in a Chrome extension to follow certain links and execute JS functions?

Hello!
There is a working code:

document.onkeypress=function(e){
   
   if(e.keyCode==49 ){
     e.preventDefault();
     alert('Переход на целевую страницу при нажатии 1');
         window.location.href="http://yandex.ru";
     } 
 };

However, if you assign the keys F1, F2, F3 ... Chrome does not perform the assigned function, but only the default functions of the browser, i.e. calls for help, etc. Prevent this by writing in code "e.preventDefault();" - it did not work, there was no effect at all.
Tell me how to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Pautov, 2017-03-16
@bootd

Well, why do you need these keyboard shortcuts? I think chrome specifically does not allow this to be done, tk. they already have the necessary functionality for the browser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question