Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Frame unable to use preload scripts when contentWindow is used to early. #34727

Open
3 tasks done
markleutloff opened this issue Jun 24, 2022 · 13 comments
Open
3 tasks done
Labels

Comments

@markleutloff
Copy link

markleutloff commented Jun 24, 2022

Preflight Checklist

Electron Version

19.0.4, 20.1.0, 21.3.1

What operating system are you using?

Windows

Operating System Version

Windows 10 Pro 21H2

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

Preload scripts should load in sub frames when nodeIntegrationInSubFrames is set to true.

Actual Behavior

Preload scripts wont load in sub frames.

Testcase Gist URL

https://gist.github.com/83f23ea2dea571eedb1b9048c8d8c111

Additional Information

By adding iframes dynamically to the DOM and then accessing the contentWindow or contentDocument property of the iframe the loading of preload scripts for this iframe wont happen until the iframe navigates to a different src or is reloaded.

Simple sample:

const iframe = document.createElement("iframe");
iframe.src = <Some URL>;
document.body.appendChild(iframe);

iframe.contentDocument; // this is enough to start the issue

Executing a script directly inside the webContents frame-created event will also ensure that the preload scriptes are not loaded:

webContents.on("frame-created", (event, details) => {
    const { frame } = details;
    const { parent } = frame;
    if (!parent) return;
   
    frame.executeJavaScript("location.href"); // <- preload scripts will no longer load
});

This happens very often in the wild, just browsing a little bit and I can see that many frames wont load the preload scripts.

Workaround:
A way around it seems to be to check if new frames have executed the preload script which is mentioned in #32453. And as mentioned there, this is a very janky workaround

webContents.on("frame-created", (event, details) => {
    const { frame } = details;
    const { parent } = frame;
    if (!parent) return;
   
    frame.once("dom-ready", () => fixPreloadScripts(frame))
});

async function fixPreloadScripts(frame)  {
    const hasPreloaded = await frame.executeJavaScript("window._hasPreloaded");
    if (hasPreloaded) return;

    frame.reload();
}

In most cases this "workaround" will break whatever a website intended to do with such an IFrame, so use it with caution.

This issue is similar to #32453 but I created a separate issue because the way of causing this effect seems to be different.

Possible related to #32661.

@markleutloff markleutloff changed the title [Bug]: Preload scripts wont load for dynamically added sub frames when accessing frames contentWindow or contentDocument after adding them into the DOM. [Bug]: Sub frames wont load preload scripts when accessing them to early (iframe.contentWindow, iframe.contentDocument, webFrameMain.executeJavaScript). Jun 27, 2022
@markleutloff markleutloff changed the title [Bug]: Sub frames wont load preload scripts when accessing them to early (iframe.contentWindow, iframe.contentDocument, webFrameMain.executeJavaScript). [Bug]: Frame unable to use preload scripts when contentWindow is used to early. Aug 29, 2022
@markleutloff
Copy link
Author

Reproducable with Electron 20.1.0.

@markleutloff
Copy link
Author

Reproducable with Electron 21.3.1.

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

@github-actions github-actions bot added the stale label Feb 24, 2023
@markleutloff
Copy link
Author

This is still an issue with Electron 23.1.1. Also tested and an issue with v24.0.0-alpha.5.

@github-actions github-actions bot removed the stale label Feb 28, 2023
@github-actions
Copy link
Contributor

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

@github-actions github-actions bot added the stale label May 30, 2023
@markleutloff
Copy link
Author

The problem still exists with Electron v25.0.0 and possible the previous versions too.

@github-actions github-actions bot removed the stale label Jun 1, 2023
@noahheck
Copy link

I can verify I have this same issue as well, though I haven't tried the workaround described above. Electron v25.0.1

@sanperrier
Copy link

bump

@electron-issue-triage
Copy link

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

@witcher112
Copy link

bump

@electron-issue-triage
Copy link

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

@alexi
Copy link

alexi commented Mar 31, 2024

This issue is still affecting us.

@dmsch
Copy link

dmsch commented Apr 4, 2024

bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants