Skip to content

[WIP sketch] feat: Have injectScript return the result value from the script #1765

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ion1
Copy link

@ion1 ion1 commented Jun 20, 2025

Note

This PR builds on top of #1761, #1762 and #1763 at the moment. In case some of them are not accepted, I will rebase this on top of main.

Overview

A sketch for having injectScript return the result value from the script. The code works, but it currently has changes to virtual/unlisted-script-entrypoint.ts that only work with injectScript while being broken with the browser.scripting API.

I would like to get feedback on how you would like that to be resolved. Some options I can think of:

  • Have the entrypoint detect whether it is being run from injectScript or executeScript. I do not know how to achieve that reliably. document.currentScript can be overridden by the web page using Object.defineProperty.
  • Build separate entrypoints depending on whether the script is injected using injectScript or executeScript.

Manual Testing

Use injectScript on a script which either returns a value or throws an exception; see that injectScript returns the value or rethrows the exception.

Related Issue

#1754 proposes having a way to pass an UnlistedScriptDefinition rather than the filename to injectScript. If something like that was implemented, the type of the return value of injectScript could be based on the type of main within the UnlistedScriptDefinition.

ion1 added 2 commits June 19, 2025 16:16
Everything seems to work: the script is executed; onload, onerror
handlers do the right thing; document.currentScript invoked by the
script returns the detached script element.
It enables the manipulation of the script element just before it is
added to the DOM.

It can be useful for e.g. passing data to the script via the `dataset`
property (which can be accessed by the script via
`document.currentScript`).
@ion1 ion1 requested review from aklinker1 and Timeraa as code owners June 20, 2025 08:15
Copy link

netlify bot commented Jun 20, 2025

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit ca3ca14
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/6855786a22c8c50009e00696
😎 Deploy Preview https://deploy-preview-1765--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

The documentation states:

> `injectScript` returns a promise, that when resolved, means the script
> has been evaluated by the browser and you can start communicating with
> it.

However, currently `injectScript` returns as soon as the `script`
element has been added to the DOM.

Make `injectScript` behave according to the documentation.
@ion1 ion1 force-pushed the inject-script-return-value branch from d27a5e8 to 1f9dab4 Compare June 20, 2025 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant