From 5b677b278df18e14b88e41afaac7c0f7ed9a2c02 Mon Sep 17 00:00:00 2001 From: Xmader Date: Sat, 24 Oct 2020 23:19:41 -0400 Subject: [PATCH] fix: auth magic --- src/file.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/file.ts b/src/file.ts index 10d6a5d..720ae1a 100644 --- a/src/file.ts +++ b/src/file.ts @@ -15,20 +15,23 @@ const getApiUrl = (id: number, type: FileType, index: number): string => { * I know this is super hacky. */ let magic: Promise | string = new Promise((resolve) => { - const reg = '^\\d+(img|mp3|midi)\\d(\\w+)$' + const reg = /^\d+(img|mp3|midi)\d(.+)$/ - const _encode = encodeURIComponent + const method = 'encodeURIComponent' + const _fn = window[method] - window.encodeURIComponent = (s) => { + // This script can run before anything on the page, + // so setting `encodeURIComponent` to be non-configurable and non-writable is no use. + window[method] = (s) => { const m = s.toString().match(reg) if (m) { // the auth string will be encoded using `encodeURIComponent` before `md5`, // so hook here resolve(m[2]) magic = m[2] - window.encodeURIComponent = _encode // detach + window[method] = _fn // detach } - return _encode(s) + return _fn(s) } })