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

215
Visualizações
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 Respostas
Responde à pergunta

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 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