Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
How to run script only if div:contains in an iFrame - Greasemonkey

So let's say we have this code in an iFrame. I'm pretty sure the iFrame is from the same domain, that matters I think.

<div id="10">You become 1 best coder</div>

I want a Greasemonkey script which will run a script only if both "div id=10" and the text (or part of the text) is true.

I tried this:

// ==UserScript==
// @name        Script
// @include     https://www.somesite.com/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

var intv = setInterval(function() {
    
    const sp = document.querySelector('#10'); // find id (some)
if (sp && sp.textContent === 'You become 1 best coder') {

alert('Hello world!');
    
    }

      }, 5000);

And this works on a regular page with regular content. But when the content I need the script to identify in order to run loads inside an iFrame, the script doesn't run.

Here is an example of the iFrame code:

<iframe src="something.php?text=M0000310y100g190e500A78014607601&amp;lalala=tT6&amp;ctrl=032a4c3342e76e97ea21821a5c5f800e&amp;mamama=c2ed5535532cc20aef064db2b4" width="100%" height="100%" frameborder="0"></iframe>

I assume the iFrame is from the same domain, because if it was from a different domain the src="" would be different, I think...

Can anyone help? Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

First you need to get the iframe. If that is the only one then you can try ....

Note: Updated based on comment

// ==UserScript==
// @name        Script
// @include     https://www.somesite.com/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

const intv = setInterval(function() {
  const iframe = document.querySelector('iframe[src*="something.php"]');
  const sp = iframe && iframe.contentWindow.document.body.querySelector('#10'); // find id (some)
  if (sp && sp.textContent === 'You become 1 best coder') {
    alert('Hello world!');
  }
}, 5000);

Update

iframe content are also document. If the code doesn't need to interact with the parent frame, you can inject the userscript in the target iframe document and run it there.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda