Skip to content

[Advice needed] feat: Have injectScript return the return value of 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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ion1
Copy link

@ion1 ion1 commented Jun 20, 2025

Note

The patch series in which each PR builds on top of the previous one: #1761, #1762, #1763, #1838, #1765 (you are here).

In case some of the changes are rejected, I will rebase the subsequent PRs 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, so its value might not be a reliable way to determine that.
  • 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 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 3aebc7c
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/688fcde20acd6c0008bab271
😎 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.

@ion1 ion1 force-pushed the inject-script-return-value branch 2 times, most recently from 1f9dab4 to ca3ca14 Compare June 20, 2025 15:04
@ion1 ion1 marked this pull request as draft June 30, 2025 05:47
@ion1 ion1 changed the title [WIP sketch] feat: Have injectScript return the result value from the script [Advice needed] feat: Have injectScript return the result value from the script Jun 30, 2025
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.
@ion1 ion1 force-pushed the inject-script-return-value branch 2 times, most recently from f0006db to 5b335b1 Compare August 3, 2025 15:42
ion1 added 2 commits August 3, 2025 23:10
It enables the modification of the script element just before it is
added to the DOM.

It can be used to e.g.  modify `script.async`/`script.defer`, add event
listeners to the element, or pass data to the script via
`script.dataset` (which can be accessed by the script via
`document.currentScript`).
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 5b335b1 to d72f1ca Compare August 3, 2025 20:26
@ion1 ion1 changed the title [Advice needed] feat: Have injectScript return the result value from the script [Advice needed] feat: Have injectScript return the return value of the script Aug 3, 2025
ion1 added 2 commits August 3, 2025 23:58
It can be used to e.g. send messages to the script in the form of custom
events. The script can add an event listener for them via
`document.currentScript`.
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