Skip to content

feat: Add modifyScript option to injectScript #1762

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 2 commits into
base: main
Choose a base branch
from

Conversation

ion1
Copy link

@ion1 ion1 commented Jun 19, 2025

Note

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

In case some of the changes are rejected, I will rebase the subsequent PRs on top of main.

Overview

modifyScript enables the the modification 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), as shown in the example:

// entrypoints/example.content.ts
export default defineContentScript({
  matches: ['*://*/*'],
  async main() {
    await injectScript('/example-main-world.js', {
      modifyScript(script) {
        script.dataset['greeting'] = 'Hello there';
      },
    });
  },
});
// entrypoints/example-main-world.ts
export default defineUnlistedScript(() => {
  console.log(document.currentScript?.dataset['greeting']);
});

It can also be used to change script.async/script.defer or to set up event listeners for communication through the script element.

Manual Testing

It can be tested as shown in the example above.

Related Issue

#1755 proposes a higher level (and potentially type-checked) API for passing an object to the script.

@ion1 ion1 requested review from aklinker1 and Timeraa as code owners June 19, 2025 15:47
Copy link

netlify bot commented Jun 19, 2025

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit e040fb5
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/688fc23b80295f0008698ddd
😎 Deploy Preview https://deploy-preview-1762--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.

Copy link
Member

@aklinker1 aklinker1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but can we call it modify instead of manipulate?

@ion1
Copy link
Author

ion1 commented Aug 3, 2025

Looks good, but can we call it modify instead of manipulate?

Sure, I'll update my patch series in a bit.

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-manipulate-script branch from 55c59d4 to c4f2c2d Compare August 3, 2025 14:33
@ion1 ion1 changed the title feat: Add manipulateScript option to injectScript feat: Add modifyScript option to injectScript Aug 3, 2025
@ion1
Copy link
Author

ion1 commented Aug 3, 2025

Looks good, but can we call it modify instead of manipulate?

Updated.

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`).
@ion1 ion1 force-pushed the inject-script-manipulate-script branch from c4f2c2d to e040fb5 Compare August 3, 2025 20:10
@ion1
Copy link
Author

ion1 commented Aug 3, 2025

I had forgotten to rename one manipulateScript as modifyScript in the documentation. Fixed, and tested that the code in the documentation works.

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.

3 participants