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

294
Visualizações
How can I trigger a function when certain text on a specific element appears?

I need to add some code to a webpage when a certain text appears on it.

So I load a webpage, and this appears:

<div id="someid" class="someclass" style=""></div>

I don't want anything to happen. But at some point this appears:

<div id="someid" class="someclass" style="">Some text here...</div>

When the text appears, I want Greasemonkey to add this somewhere on the page.

<div id="idbla">This is text...</div>

I don't care where this is added. It doesn't need to be appended to anything. It doesn't even need to be visible on the page. I just want it to appear somewhere in the HTML of the webpage.

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

0

Use a MutationObserver to detect text changes to your element:

function main() {
  console.log('Text added!');
}

const wantedDiv = document.querySelector('#someid');
const button = document.querySelector('#add-text');
button.addEventListener('click', () => wantedDiv.innerText = 'text...');

const observer = new MutationObserver(() => {
  if (wantedDiv.innerText) main();
  observer.disconnect();
});
const config = {
  characterData: false,
  attributes: false,
  childList: true,
  subtree: false
};

observer.observe(wantedDiv, config);
<div id="someid" class="someclass" style=""></div>

<button id="add-text">Add text</button>

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