Skip to content

Support Node / Preload scripts in iframes or workers #12505

Closed

Description

@pfrazee
  • Electron version: v2.0.0-beta.6
  • Operating system: MacOS 10.12

Expected behavior

Hey all! Thanks to #10430, the preload script should run in renderers, webviews, iframes, and web workers.

Actual behavior

It only runs in renderers and webviews.

How to reproduce

https://github.com/pfrazee/electron-bug-test-preload

Console will emit true/false for whether the preload ran in the renderer, an iframe, and a worker. At time of writing this demo, only true for renderer.

git clone https://github.com/pfrazee/electron-bug-test-preload
cd electron-bug-test-preload
npm install
npm start

Activity

  1. MarshallOfSound commented on Apr 2, 2018

    @MarshallOfSound
    Member

    Please note that that PR wasn't intended to make preloads run in iframes. This should be considered an enhancement request not a bug 👍

  2. pfrazee commented on Apr 2, 2018

    @pfrazee
    ContributorAuthor

    @MarshallOfSound Oh my bad, I misread that.

    Well if there's nobody that can hit the enhancement easily, I can try to slot some time to implement it.

  3. Tim91084 commented on Apr 13, 2018

    @Tim91084

    This would be a great enhancement. I've also tried using extensions to run code within iframes, and that does not work either. Seems like the extension manifest option "all_frames" is also not supported within electron. So, as far as I can tell, there is no way to accomplish running a script within an iframe similar to how a preload script works for a webview.

    @pfrazee, I'd be happy to help out in any way if you needed.

  4. pfrazee commented on Apr 13, 2018

    @pfrazee
    ContributorAuthor
  5. pfrazee commented on Jul 4, 2018

    @pfrazee
    ContributorAuthor

    I explored what this might require for Web Workers. See the changes made here: master...pfrazee:worker-preload-exploration

    The approach I took was to set nodeIntegrationInWorker to true in my test app and then copy over a lot of behaviors from the lib/renderer JS.

    Results of this exploration:

    • I was able to successfully include the session preloads.
    • Through some hacks, I was able to get the ipcRenderer API in my preload, but the methods didn't work because the internal implementation didn't find a frame for the Worker. At that point I was out of my depth so I quit. (Note, the ipcRenderer is the only reason I want preloads in iframes and workers: I use it to add Web APIs.)

    I believe this approach requires node to be attached to every Web Worker, regardless of whether a preload script is being used or nodeIntegrationInWorker is enabled. I assume that's the same cost behind every other preload script.

    Hope this was helpful.

  6. InputNeuron commented on Aug 15, 2018

    @InputNeuron

    Seems like this is still broken as of 3.0.0-beta4

  7. changed the title [-]Session setPreloads() not working for iframes or workers[/-] [+]Support Node / Preload scripts in iframes or workers[/+] on Jan 8, 2019
  8. added 2 commits that reference this issue on Jan 17, 2019
    f5a166d
    aadee6c
  9. pfrazee commented on Jan 22, 2019

    @pfrazee
    ContributorAuthor
  10. added 2 commits that reference this issue on Jan 27, 2019
    f883aa0
    0890b0c
  11. Spongman commented on Aug 4, 2019

    @Spongman

    is it possible to run preloads when nodeIntegrationInSubFrames is false? nodeIntegrationInSubFrames breaks the code in my iframes as they don't expect the node stuff to be defined, and they break if it is. but i need to run the preloads there regardless. is this possible?

  12. MarshallOfSound commented on Aug 4, 2019

    @MarshallOfSound
    Member

    @Spongman nodeIntegrationInSubFrames: true && nodeIntegration: false

  13. Obi-Dann commented on Oct 9, 2020

    @Obi-Dann

    Sorry for resurrecting an old issue...
    It looks like preload scripts don't run in web workers, it looks like it only the PR only implemented it for iframes #16425, right? I wonder if supporting preloads for web workers can be added as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions