Skip to content

Conversation

@ion1
Copy link
Contributor

@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 Timeraa and aklinker1 as code owners June 19, 2025 15:47
@netlify
Copy link

netlify bot commented Jun 19, 2025

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit c47193b
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/6941d27d5a8d4400080902d7
😎 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
Contributor 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
Contributor 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
Contributor 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.

@aklinker1 aklinker1 changed the title feat: Add modifyScript option to injectScript feat: Add modifyScript option to injectScript Dec 16, 2025
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.

Getting all your PRs merged...

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 16, 2025

Open in StackBlitz

@wxt-dev/analytics

npm i https://pkg.pr.new/@wxt-dev/analytics@1762

@wxt-dev/auto-icons

npm i https://pkg.pr.new/@wxt-dev/auto-icons@1762

@wxt-dev/browser

npm i https://pkg.pr.new/@wxt-dev/browser@1762

@wxt-dev/i18n

npm i https://pkg.pr.new/@wxt-dev/i18n@1762

@wxt-dev/module-react

npm i https://pkg.pr.new/@wxt-dev/module-react@1762

@wxt-dev/module-solid

npm i https://pkg.pr.new/@wxt-dev/module-solid@1762

@wxt-dev/module-svelte

npm i https://pkg.pr.new/@wxt-dev/module-svelte@1762

@wxt-dev/module-vue

npm i https://pkg.pr.new/@wxt-dev/module-vue@1762

@wxt-dev/runner

npm i https://pkg.pr.new/@wxt-dev/runner@1762

@wxt-dev/storage

npm i https://pkg.pr.new/@wxt-dev/storage@1762

@wxt-dev/unocss

npm i https://pkg.pr.new/@wxt-dev/unocss@1762

@wxt-dev/webextension-polyfill

npm i https://pkg.pr.new/@wxt-dev/webextension-polyfill@1762

wxt

npm i https://pkg.pr.new/wxt@1762

commit: c47193b

@codecov
Copy link

codecov bot commented Dec 16, 2025

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.63%. Comparing base (938c25c) to head (c47193b).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/wxt/src/utils/inject-script.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1762      +/-   ##
==========================================
- Coverage   76.76%   76.63%   -0.13%     
==========================================
  Files         113      113              
  Lines        3025     3026       +1     
  Branches      686      686              
==========================================
- Hits         2322     2319       -3     
- Misses        622      624       +2     
- Partials       81       83       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@aklinker1 aklinker1 merged commit 6f84d45 into wxt-dev:main Dec 16, 2025
19 checks passed
@github-actions
Copy link
Contributor

Thanks for helping make WXT better!

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