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

296
Visualizações
How to run a function from content script, when an injected button was clicked?

I am injecting a button through the content script of my chrome extension like this:

btnHtml += '<button id="btn1" style="background-color: #2c3f82; height: 25px; width: 50px; text-align: center; cursor: pointer; border-radius: 4px; color: white; border: 1px solid rgba(0, 0, 0, 0.8); font-size: 15px; margin: 2px;">someText</button>'
const div = document.createElement('div');
div.className = 'ownSizes';
div.innerHTML = '<div style="display: inline-grid; grid-template-columns: auto auto auto auto; background-color: #1d76ce; border-radius: 8px; padding: 10px;">'+btnHtml+'</div>';
document.getElementsByClassName('_4NtqZU')[0].appendChild(div);

That works fine. Now how can I add a function to the button? Do I have to inject a new js into the page or is there an easier way?

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

0

I think the issue you are having is due to not being able to reach your scope because you're adding the element by injecting strings of html.

Alternatively, you could create the button by using the createElement function to give you an actual node that you can inject into the page.

let btn = document.createElement('button' [, options]);

Then you can bind you click event from the current scope

btn.onclick = //some function

Lastly inject into the page using appendChild as you have in your question.

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