Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

514
Visualizações
Simulate Keyboard Input in web extension with Javascript

is there a way to simulate a raw Keyboard input in javascript, so that it is possible to f.e. fill in text on your webpage via a web extension?

My plan is, to load in a web extension (firefox) that f.e. writes a word into the searchbar of youtube.

All that I could find is the Keyboard events eg:

var e = new KeyboardEvent('keydown',{'keyCode':32,'which':32});
document.dispatchEvent(e);

Sadly this only triggers a keydown event, so that I can start and stop videos but cant type into the search bar text field.

Any help is really appriciated!

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are a couple of ways we can do this,

One of the easiest ways is to select the search DOM element, set the value of the search element, and then dispatch a click event on the search button.

document.querySelector('input').value = "Hello world";
document.querySelector('button#search-icon-legacy').click();

If you want to do it the way you mentioned, youtube supports a keyboard shortcut to access the search input. By pressing the '\' key you can get the focus on the search input. So you can access the search box using KeyboardEvent by dispatching a 'keydown' event to '\' (191).

The code looks something like this,

const slash = new KeyboardEvent('keydown', {'keyCode':191, 'which':191})
const k = new KeyboardEvent('keydown', {'keyCode':75, 'which':75})
const enter = new KeyboardEvent('keydown', {'keyCode':13, 'which':13})

document.dispatchEvent(slash)
document.dispatchEvent(k)
document.dispatchEvent(enter)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda