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]: webContents.setWindowOpenHandler cannot set preload #32136

Closed
3 tasks done
IrvingZhao opened this issue Dec 9, 2021 · 8 comments
Closed
3 tasks done

[Bug]: webContents.setWindowOpenHandler cannot set preload #32136

IrvingZhao opened this issue Dec 9, 2021 · 8 comments

Comments

@IrvingZhao
Copy link

IrvingZhao commented Dec 9, 2021

Preflight Checklist

Electron Version

16.0.2

What operating system are you using?

Windows

Operating System Version

Windows 11 21H2

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

const conversationWindow = new BrowserWindow({
  webPreferences: {
    devTools: true,
    preload: path.resolve(__dirname, 'preload.js')
  }
})

  conversationWindow.webContents.setWindowOpenHandler(({ url }) => {
    if (url === 'about:blank') {
      return {
        action: 'allow',
        overrideBrowserWindowOptions: {
          frame: false,
          fullscreenable: false,
          backgroundColor: 'black',
          webPreferences: {
            preload: resolve(__dirname, 'preload2.js')
          }
        }
      }
    }
    return { action: 'deny' }
  })

In conversationWindow, use window.open('about:blank') to open a new window, execute preload2.js

Actual Behavior

it also execute preload.js

Testcase Gist URL

index.ts

Additional Information

No response

@thorsent
Copy link

I think that preloads will not run in window.open when contextIsolation=true for the parent window. This appears to be the case for both setWindowOpenHandler() as well as the deprecated new-window event (when setting newGuest).

I'm running Electron 13.6.1 . Since the OP is version 16.0.2, I think this is possibly a generalized problem.

@IrvingZhao can you try contextIsolation=false to see if the preload runs? (Perhaps it's defaulting to true in recent releases?)

@thorsent
Copy link

Same with -add-new-content (#26802). Preloads will only run when contextIsolation is false in the parent.

@thorsent
Copy link

This issue was mentioned in another thread: #16224 (comment)

@bastecklein
Copy link

I am having a similar issue. When my app was using Electron 14.x and I called window.open, the preload script was working on the child window (which is what I wanted). I have now upgraded to Electron v16.0.5 and the preload script is no longer loading on my child window, and I am not seeing anything in the changelogs to indicate why this is happening. I need to have contextisolation: true, and it worked fine before updating electron.

@t57ser
Copy link
Contributor

t57ser commented Feb 1, 2022

This is happening because when navigating to about:blank the window will take over the webpreferences from the parent and it is not possible to change them.

// When a child window is created with window.open, its WebPreferences will
// be copied from its parent, and Chromium will intialize JS context in it
// immediately.

Note that this is only happening for about:blank. See:
// FIXME(zcbenz): Chromium does not do any browser side navigation for
// window.open('about:blank'), so there is no way to override WebPreferences
// of it. We should not delay Node.js initialization as there will be no
// further loadings.

Other cases will work fine

@general-ice
Copy link

It looks like fixed. Latest electron 20.0.2

@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 Nov 15, 2022
@github-actions
Copy link
Contributor

This issue has been closed due to inactivity, and will not be monitored. If this is a bug and you can reproduce this issue on a supported version of Electron please open a new issue and include instructions for reproducing the issue.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants