injectScript passing data to the script
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 10.5k
- Forks
- 564
- PR merge metrics
- No merged PRs in 30d
Description
Feature Request
It would be nice if something like this was possible:
await injectScript("/script.js", {
parameter: [1, 2, 3],
});
// entrypoints/script.ts
export default defineUnlistedScript({
main<number[]>(ctx) {
console.info(ctx.parameter); // [1, 2, 3]
},
);
where injectScript would do something like this:
if (parameter !== undefined)
script.dataset["wxtParameter"] = JSON.stringify(parameter);
and the build result from entrypoints/script.ts would include something like this:
const parameterString = document.currentScript?.dataset?.["wxtParameter"];
const parameter = parameterString !== undefined ? JSON.parse(parameterString) : undefined;
return await defintion.main({ parameter });
Additionally, if #1754 is implemented, this could type-check the parameter against the type of the function.
// entrypoints/content.ts
import script from "./script";
export default defineContentScript({
matches: ["https://example.net/*"],
async main() {
await injectScript(script, {
parameter: [1, 2, 3], // type-checked
});
},
});
Thanks!
Is your feature request related to a bug?
N/A
What are the alternatives?
Leaving things as they are.
Additional context
N/A
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the existing injectScript implementation and the generated entrypoint code for entrypoints/script.ts and entrypoints/content.ts. Review how script data is passed today, then trace the proposed parameter flow from injectScript to defineUnlistedScript's main context. Consider the relationship with #1754 and verify the resulting parameter behavior with the project's relevant tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- build-system, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100