-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(caa upload): compatibility with Greasemonkey #224
Conversation
`document-load` doesn't exist. I meant `document-end`...
Greasemonkey scripts run in the content context, not the page context, so the `fetch` calls don't properly resolve relative URLs. Easiest way to resolve this is by providing absolute URLs.
Mostly typing changes
…hanges `GM_xmlhttpRequest` -> `GM.xmlHttpRequest` `GM_getResourceURL` -> `GM.getResourceUrl`
These changes ensure the GM API is polyfilled only after the core-js polyfills are added and that they are transpiled by babel.
We cannot access the MB object defined in `window` since we're not running in a page context in Greasemonkey. Although we could circumvent this using `unsafeWindow`, this leads to issues with permission errors because we're crossing context boundaries with callback functions.
Greasemonkey scripts that use granted APIs run in the content context, not the page context, and therefore have no access to the jQuery object defined by MB. We "escape" this through `unsafeWindow` and `cloneInto` to trigger the event. We also trigger the handler manually rather than triggering the event via jQuery, since jQuery tries to call some functions defined on the event but the page scripts don't have that permission.
ImageMaxUrl only exports after all `GM.setValue` and `GM.getValue` calls have been done. With `GM.*` APIs, these are asynchronous, but there's no way for us to await them. Therefore, we retry on a timeout until IMU is exported.
/deploy-preview |
Codecov Report
@@ Coverage Diff @@
## main #224 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 39 39
Lines 857 860 +3
Branches 158 158
=========================================
+ Hits 857 860 +3
Continue to review full report at Codecov.
|
Deploying a preview is currently broken because it doesn't clone the submodules (will be fixed when PR is merged though), so here's one I made manually: https://github.com/ROpdebee/mb-userscripts/raw/dist-preview-224/mb_enhanced_cover_art_uploads.user.js @Freso could you give the preview above a try and see if it works for you? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, I also tested with GM, TM and VM in Firefox 94 and everything I tried is working as it should.
|
It still doesn't work in GM <4, see https://chatlogs.metabrainz.org/libera/musicbrainz/msg/4904321/. Tested with Waterfox Classic 2021.10, running GM3.17. Root cause is another piece of context switching between page and content scripts, more specifically in the derivation of the mime type. It seems to be related to greasemonkey/greasemonkey#2034. The script freezes once it gets to this line:
For @CatQuest, it throws a (surprisingly helpful) error about TypedArray s and Xrays, for me personally, it just freezes without any error and doesn't get past that line.
In short: Solution: use For some reason, under GM4.1 in the same Waterfox version, it works just fine. I guess it uses a different implementation of the sandbox. |
3b9ed95
to
f0a76c7
Compare
We're using the compat layer now.
They exist to ensure compatibility between different userscript engines and browsers, so they belong there.
Under GM3, the script wasn't properly switching contexts when processing `Uint*Array` instances created from `ArrayBuffer`s. See #224 (comment)
f0a76c7
to
8991394
Compare
dd9f16d
to
b341a25
Compare
/deploy-preview should work again now because we're not using git submodules anymore |
This PR changes 1 built userscript(s):
|
fix(caa upload): compatibility with Greasemonkey (#224)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good without the unnecessary polyfills and hacks to make those work, thanks.
Under GM3, the script wasn't properly switching contexts when processing `Uint*Array` instances created from `ArrayBuffer`s. See #224 (comment)
🚀 Released 1 new userscript version(s):
|
fix(caa upload): compatibility with Greasemonkey (#224)
Closes #123.
run-at
valueGM.*
API by default and polyfill them when the userscript engine doesn't support them.GM.*
andGM_*
version, one of the two (or both) will be granted.If theGM_
version is granted, we polyfill theGM.*
version based on that. This is done through thegm4-polyfill
requirement.gm4-polyfill
is injected into the script itself rather than@require
d so that we can transpile it for compatibility with older browsers. It's currently included as a submodule, so if you want to build it locally, you may have to init and update the submodule first. The build process is a bit hack-y sincegm4-polyfill
requiresthis
, which isn't available in a rolled-up IIFE, so we patch it to have one. Eventually, I'd like to separate that out of the IIFE (perhaps as a separate@require
).GM.*
APIs also means we have to make some changes for IMU. Most importantly, we now have to deal with IMU's exports possibly being defined asynchronously, becauseGM.getValue
andGM.setValue
are asynchronous.window
globals defined by the page itself. This includeswindow.MB
and jQuery. Although we can access those globals throughunsafeWindow
, we can't provide our content globals to the page without cloning it first.fetch
doesn't support relative URLs, so we're using absolute ones now.MB.CoverArt.validate_file
in content scripts, since we provide content context functions to be called by page content, which leads to permission errors. Therefore, we duplicate MB's implementation.Tested with: