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

Add session#setPreloads() API to set the JS script content directly #14012

Closed
sindresorhus opened this issue Aug 10, 2018 · 1 comment
Closed
Assignees

Comments

@sindresorhus
Copy link
Contributor

Is your feature request related to a problem? Please describe.

Being able to set preloads for a session is very useful, unfortunately having to set an absolute path is problematic when you use some kind of bundler for the code, as the preload script has to be excluded and handled manually since the path to the preload no longer exists when bundled. This is especially problematic for reusable modules using session#setPreloads() as the bundling is out of its control. (Example: sindresorhus/electron-timber#17).

API: https://electronjs.org/docs/api/session#sessetpreloadspreloads

Describe the solution you'd like

I would like the ability to set session preloads as strings of content in addition to absolute paths.

The existing API can't be used for this as it already supports strings as paths.

I propose two solutions:

  1. Add a new session.setPreloadScripts() that accepts a string[] with JS code:
session.defaultSession.setPreloads(['/foo/bar/path']);

session.defaultSession.setPreloadScripts(['console.log("loaded")']);
  1. Add a session.PreloadScript() constructor that can be passed to session#setPreloads():
session.defaultSession.setPreloads([
	'/foo/bar/path',
	new session.PreloadScript('console.log("loaded")')
]);

This would also let us create dynamic preload scripts, which can be useful for modules.

@codebytere
Copy link
Member

codebytere commented Sep 23, 2018

This is effectively a global eval, which by most definitions would be considered a security hole and thus is something we don't think wise to implement.

This API is seemingly being requested to solve a web bundling problem which already has workable solutions (ex. webpack with webworkers). I would probably recommend instructing webpack users to add an entry point for a preload script into their webpack config, and then for developers who want this functionality, add an option to allow people to provide preloadPath or something similar into their modules.

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

4 participants