Skip to content

fix: Make injectScript wait until script is actually loaded#1763

Merged
aklinker1 merged 7 commits intowxt-dev:mainfrom
ion1:inject-script-wait
Dec 16, 2025
Merged

fix: Make injectScript wait until script is actually loaded#1763
aklinker1 merged 7 commits intowxt-dev:mainfrom
ion1:inject-script-wait

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, #1763 (you are here), #1838, #1765.

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

Overview

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.

This PR makes injectScript behave according to the documentation.

Manual Testing

Invoke injectScript and observe that it will wait until the script has loaded.

Related Issue

N/A

@ion1 ion1 requested review from Timeraa and aklinker1 as code owners June 19, 2025 16:33
@netlify
Copy link

netlify bot commented Jun 19, 2025

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 4502982
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/6941d407cee07b0008680f31
😎 Deploy Preview https://deploy-preview-1763--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 changed the title feat: Add waitUntilLoaded to injectScript fix: Make injectScript wait until loaded Jun 19, 2025
@ion1 ion1 force-pushed the inject-script-wait branch from 4617287 to de58942 Compare June 19, 2025 18:59
@ion1 ion1 force-pushed the inject-script-wait branch from de58942 to 5af7647 Compare June 20, 2025 14:39
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 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.
@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@1763

@wxt-dev/auto-icons

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

@wxt-dev/browser

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

@wxt-dev/i18n

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

@wxt-dev/module-react

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

@wxt-dev/module-solid

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

@wxt-dev/module-svelte

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

@wxt-dev/module-vue

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

@wxt-dev/runner

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

@wxt-dev/storage

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

@wxt-dev/unocss

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

@wxt-dev/webextension-polyfill

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

wxt

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

commit: 4502982

@codecov
Copy link

codecov bot commented Dec 16, 2025

Codecov Report

❌ Patch coverage is 0% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.09%. Comparing base (6f84d45) to head (4502982).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/wxt/src/utils/inject-script.ts 0.00% 15 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1763      +/-   ##
==========================================
- Coverage   76.70%   76.09%   -0.61%     
==========================================
  Files         113      113              
  Lines        3026     3041      +15     
  Branches      686      686              
==========================================
- Hits         2321     2314       -7     
- Misses        624      643      +19     
- Partials       81       84       +3     

☔ 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 changed the title fix: Make injectScript wait until loaded fix: Make injectScript wait until script is actually loaded Dec 16, 2025
@aklinker1 aklinker1 merged commit adeef6e into wxt-dev:main Dec 16, 2025
18 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.

2 participants