Skip to content

Commit

Permalink
fix(caa upload): compatibility with GM3
Browse files Browse the repository at this point in the history
Under GM3, the script wasn't properly switching contexts when
processing `Uint*Array` instances created from `ArrayBuffer`s.
See #224 (comment)
  • Loading branch information
ROpdebee committed Nov 11, 2021
1 parent 761af1c commit 8991394
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export function cloneIntoPageContext<T>(object: T): T {
* running in the page context, returns the object from this context's window
* instead.
*/
export function getFromPageContext<M extends keyof Window>(name: M): Window[M] {
export function getFromPageContext<M extends keyof Window>(name: M): Window[M];
export function getFromPageContext<M extends keyof typeof globalThis>(name: M): (typeof globalThis)[M];
export function getFromPageContext<M extends keyof (Window | typeof globalThis)>(name: M): (Window | typeof globalThis)[M] {
if (typeof unsafeWindow !== 'undefined') {
return unsafeWindow[name];
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/util/blob.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { getFromPageContext } from '@src/compat';

function hexEncode(buffer: ArrayBuffer): string {
// https://stackoverflow.com/a/40031979
const Uint8Array = getFromPageContext('Uint8Array');
return [...new Uint8Array(buffer)]
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
Expand Down
2 changes: 2 additions & 0 deletions src/mb_enhanced_cover_art_uploads/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getMaximisedCandidates } from './maximise';
import { getProvider } from './providers';
import { ArtworkTypeIDs } from './providers/base';
import type { CoverArt, CoverArtProvider } from './providers/base';
import { getFromPageContext } from '@src/compat';

interface ImageContents {
requestedUrl: URL;
Expand Down Expand Up @@ -195,6 +196,7 @@ export class ImageFetcher {
const reader = new FileReader();
// istanbul ignore next: Copied from MB.
reader.addEventListener('load', () => {
const Uint32Array = getFromPageContext('Uint32Array');
const uint32view = new Uint32Array(reader.result as ArrayBuffer);
if ((uint32view[0] & 0x00FFFFFF) === 0x00FFD8FF) {
resolve('image/jpeg');
Expand Down

0 comments on commit 8991394

Please sign in to comment.