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

211
Views
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 answers
Answer question

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