-
Hi, I need to access a variable that is defined on the window of the page that my extension is used on. What is the best method for interacting with that? I thought setting I found some useful information on stackoverflow but I'm not totally sure how to translate one of those methods into the wxt-way of doing things. In case its helpful, ultimately my use case is sending messages to the page's window and executing functions there based off the message contents. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Firefox doesn't support main world content scripts, so I would recommend not using those, and manually adding a 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 = document.createElement('script');
s.src = browser.runtime.getURL('/injected.js');
s.onload =() => s.remove();
(document.head || document.documentElement).appendChild(s);
window.addEventListener("MyEvent", console.log);
},
}); This should give you access to the global you're looking for. |
Beta Was this translation helpful? Give feedback.
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.
Then you can load it as described in method 1 from your link (which is supported by all browsers):