Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
¿Cómo puedo activar una función cuando aparece cierto texto en un elemento específico?

Necesito agregar un código a una página web cuando aparece cierto texto en ella.

Así que cargo una página web, y aparece esto:

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

No quiero que pase nada. Pero en algún momento aparece esto:

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

Cuando aparezca el texto, quiero que Greasemonkey lo agregue en algún lugar de la página.

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

No me importa dónde se agrega esto. No necesita ser anexado a nada. Ni siquiera necesita ser visible en la página. Solo quiero que aparezca en algún lugar del HTML de la página web.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use un MutationObserver para detectar cambios de texto en su elemento:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!