question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Injecting a script tag (or script code) to inside every iframe

See original GitHub issue

Hi,

My goal is to re-initialize certain Javascript read-only variables such as window.screen.pixelDepth for the entire page, including iframes in it. I can add a script tag to the top-level HTML by using this code:

await page.evaluate(scriptText => {
  const el = document.createElement('script');
  el.type = 'text/javascript';
  el.textContent = scriptText;
  document.head.prepend(el);
}, scriptToInject);

However, this does not get applied to the HTML pages loaded by <iframe> tags. I wonder how I can inject Javascript for all HTML pages inside iframes of a top-level page.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
gogo9thcommented, Nov 5, 2020

Yes, this works nicely. Thanks very much for your clever solution!

1reaction
vsemozhetbytcommented, Nov 4, 2020

Another attempt:

import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();

try {
  const [page] = await browser.pages();

  await page.evaluateOnNewDocument(() => {
    const observer = new MutationObserver((mutations) => {
      for (const mutation of mutations) {
        if (Array.from(mutation.addedNodes).some(node => node.nodeName === 'BODY')) {
          observer.disconnect();
          Object.defineProperty(document.body, 'offsetWidth', {
             value: 1000,
             writable: false,
             configurable: true
          });
        }
      }
    })
    observer.observe(document, { childList: true, subtree: true });
  });

  await page.goto('https://example.org/');

  const data = await page.evaluate(() => {
    return document.body.offsetWidth;
  });
  console.log(data);
} catch(err) { console.error(err); } finally { await browser.close(); }
Read more comments on GitHub >

github_iconTop Results From Across the Web

inject a javascript function into an Iframe - Stack Overflow
First of all you can only accomplish this if your frame and the page displaying it is within the same domain (Due to...
Read more >
Using Iframes vs Scripts for Embedding Components
Out of these methods, IFrame and Script Tags stand out. Both of them allow content to be embedded dynamically. In this article, I...
Read more >
inject JS code inside a frame that is inside an iframe
Hello,. I want to inject JavaScript code with a chrome extension, within a frame that is inside an iframe and that inside another...
Read more >
How to inject JavaScript into an iFrame - Jaspreet Chahal
The JavaScript · injectJS() { · var iFrameHead = window.frames["myiframe"].document.getElementsByTagName("head")[0]; · var myscript = document.
Read more >
javascript iframe injection - CodePen
URL Extension Required. When linking another Pen as a resource, make sure you use a URL Extension of the type of code you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found