<h3 dir="auto">Preflight Checklist</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have read the <a href="https://github.com/electron/electron/blob/master/CONTRIBUTING.md">Contributing Guidelines</a> for this project.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I agree to follow the <a href="https://github.com/electron/electron/blob/master/CODE_OF_CONDUCT.md">Code of Conduct</a> that this project adheres to.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have searched the <a href="https://www.github.com/electron/electron/issues">issue tracker</a> for a feature request that matches the one I want to file, without success.</li>
</ul>
<h3 dir="auto">Problem Description</h3>
<p dir="auto">Looking to set my preload script directly inline in the webPreferences of main.js.</p>
<p dir="auto">I found these two PRs that seems to be about that (please correct if wrong): <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="809434242" data-permission-text="Title is private" data-url="https://github.com/electron/electron/issues/27741" data-hovercard-type="pull_request" data-hovercard-url="/electron/electron/pull/27741/hovercard" href="https://github.com/electron/electron/pull/27741">#27741</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="796554257" data-permission-text="Title is private" data-url="https://github.com/electron/electron/issues/27546" data-hovercard-type="pull_request" data-hovercard-url="/electron/electron/pull/27546/hovercard" href="https://github.com/electron/electron/pull/27546">#27546</a></p>
<p dir="auto">But the threads state no updated information on when this is going to be released and/or the syntax to use this feature.</p>
<p dir="auto">So, Is this doable at the moment ? If so, could you post a short example on how to do that ?</p>
<p dir="auto">Thanks.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Electron 12.0.5
Win x64"><pre class="notranslate"><code class="notranslate">Electron 12.0.5
Win x64
</code></pre></div>
<h3 dir="auto">Proposed Solution</h3>
<p dir="auto">n/a</p>
<h3 dir="auto">Alternatives Considered</h3>
<p dir="auto">n/a</p>
<h3 dir="auto">Additional Information</h3>
<p dir="auto">n/a</p>
<p dir="auto"><strong>Is your feature request related to a problem? Please describe.</strong></p>
<p dir="auto">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 <code class="notranslate">session#setPreloads()</code> as the bundling is out of its control. (Example: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="345481692" data-permission-text="Title is private" data-url="https://github.com/sindresorhus/electron-timber/issues/17" data-hovercard-type="issue" data-hovercard-url="/sindresorhus/electron-timber/issues/17/hovercard" href="https://github.com/sindresorhus/electron-timber/issues/17">sindresorhus/electron-timber#17</a>).</p>
<p dir="auto">API: <a href="https://electronjs.org/docs/api/session#sessetpreloadspreloads" rel="nofollow">https://electronjs.org/docs/api/session#sessetpreloadspreloads</a></p>
<p dir="auto"><strong>Describe the solution you'd like</strong></p>
<p dir="auto">I would like the ability to set session preloads as strings of content in addition to absolute paths.</p>
<p dir="auto">The existing API can't be used for this as it already supports strings as paths.</p>
<p dir="auto">I propose two solutions:</p>
<ol dir="auto">
<li>Add a new <code class="notranslate">session.setPreloadScripts()</code> that accepts a <code class="notranslate">string[]</code> with JS code:</li>
</ol>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="session.defaultSession.setPreloads(['/foo/bar/path']);

session.defaultSession.setPreloadScripts(['console.log(&quot;loaded&quot;)']);"><pre class="notranslate"><span class="pl-s1">session</span><span class="pl-kos">.</span><span class="pl-c1">defaultSession</span><span class="pl-kos">.</span><span class="pl-en">setPreloads</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-s">'/foo/bar/path'</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-s1">session</span><span class="pl-kos">.</span><span class="pl-c1">defaultSession</span><span class="pl-kos">.</span><span class="pl-en">setPreloadScripts</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-s">'console.log("loaded")'</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<ol start="2" dir="auto">
<li>Add a <code class="notranslate">session.PreloadScript()</code> constructor that can be passed to <code class="notranslate">session#setPreloads()</code>:</li>
</ol>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="session.defaultSession.setPreloads([
	'/foo/bar/path',
	new session.PreloadScript('console.log(&quot;loaded&quot;)')
]);"><pre class="notranslate"><span class="pl-s1">session</span><span class="pl-kos">.</span><span class="pl-c1">defaultSession</span><span class="pl-kos">.</span><span class="pl-en">setPreloads</span><span class="pl-kos">(</span><span class="pl-kos">[</span>
	<span class="pl-s">'/foo/bar/path'</span><span class="pl-kos">,</span>
	<span class="pl-k">new</span> <span class="pl-s1">session</span><span class="pl-kos">.</span><span class="pl-c1">PreloadScript</span><span class="pl-kos">(</span><span class="pl-s">'console.log("loaded")'</span><span class="pl-kos">)</span>
<span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">This would also let us create dynamic preload scripts, which can be useful for modules.</p>