TLDR; Is it possible to load an ES6 module into the sandboxed DOM of GreaseMonkey in a way that is similar to doing a @require
?
Going a little nuts here. I've been writing a GM script in Firefox to "enhance" a certain website with custom functionality, backed by my GM script interacting with a database on my local server to store and retrieve the data that I generate when using my enhancements.
For this particular website (sorry for being vague, but these sites aren't... Let's called it, safe for work), my hands were pretty free as to how to structure and run my code, as the site doesn't restrict anything via CORS or CSP. In other words, I could simply add script tags to the site's DOM via GM, and then it'd run fine.
However, I'm now extending my GM script's scope to an additional site - "Site B" - which is indeed very restrictive about what I'm allowed to do there. For instance, while I can add a script tag and load my code that way, I cannot access my database from that code (via PouchDB), as it tries to XHR to my local server's domain which is prohibited by that site's CSP.
I can still modify and read Site B's page from the GM sandbox while having full access to my own resources on the server, as the code then runs from within that sandboxed environment, which doesn't restrict XHR or anything like that, and the regular interface that GM provides is sufficient for my needs.
So, I thought I'd rewrite again, and that I'd try to use ES6 modules this time because my code is already very spaghetti bolognese, and it's about time to clean it up a bit anyway.
Thing is, to load a javascript as a module, you need to use type=module
on the script
tag, and I'm no longer injecting script tags (due to CORS and CSP). And the way @require
works it assumes a regular script, not a module.
I've tried to search for ways to access the sandboxed DOM (if it even has one?), to see if I might programatically load a script as a module inside the sandbox (something like document.append(createdScriptTagThatHasAttribueTypeSetToModule)
), but everything I find is about how to load stuff into, and mess with, the page DOM, never the GM DOM. I've looked at the GM documentation about unsafeWindow
and GM.*
methods, but everything is more aimed at how someone gains more access to the browser page's DOM, and not how one gets more access to fiddle with the sandboxed DOM.
Basically, I just really want to be able to load my external script as a module, so that I can start working with a less spaghetti-esque structure to my rapidly increasing code base.
Thanks in advance for any tips :)