-
For example: I am using but when the user visits the website, he first enters and then the user clicks the This is a simple change, but I observe that the content script is not injected. It is injected only when I refresh the page manually. How can I inject these scripts when the user enters the matching page? Waiting for your expert advice. @aklinker1 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Seems like the site you're working with is a SPA, or the website uses HTML 5 history to switch pages without a full reload. In this case, you need to load your content script for the whole site, not just the Thankfully, WXT provides utils for this. export default defineContentScript({
matches: ["*://test.local/<id>/*"],
main(ctx) {
const editPattern = new MatchPattern("*://test.local/<id>/edit");
if (editPattern.matches(location)) editMain(ctx);
ctx.addEventListener(window, "wxt:locationchange", (event) => {
if (editPattern.matches(event.newUrl) && !editPattern.matches(event.oldUrl))
editMain(ctx);
})
}
})
function editMain(ctx: ContentScriptContext) {
// Your "/edit" code here
}
|
Beta Was this translation helpful? Give feedback.
Try
window
instead ofdocument
:wxt/packages/wxt-demo/src/entrypoints/location-change.content.ts
Lines 6 to 8 in 394cb1a