Skip to content

Best practise for injecting scripts / accessing variables on the page's window #523

Answered by aklinker1
rcoopr asked this question in Q&A
Discussion options

You must be logged in to vote

Firefox doesn't support main world content scripts, so I would recommend not using those, and manually adding a script element to the page to support both browsers (method 1).

Right now, WXT doesn't provide any tools to help with this. The only thing you need to do is ensure your injected script is defined as an unlisted script in the entrypoints directory.

// entrypoints/injected.ts
export default defineUnlistedScript(() => {
  window.dispatchEvent(new CustomEvent("MyEvent", { details: window.someGlobal }));
});

Then you can load it as described in method 1 from your link (which is supported by all browsers):

export default defineContentScript({
  matches: [...],
  main() {
    const s = d…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@rcoopr
Comment options

@aklinker1
Comment options

Answer selected by rcoopr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants