From baf187ee46f8f99e436831e0573f0d0d7e3491b3 Mon Sep 17 00:00:00 2001 From: Xmader Date: Mon, 30 Nov 2020 14:23:24 -0500 Subject: [PATCH] fix: error handling --- src/file.ts | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/file.ts b/src/file.ts index 979b401..92763d6 100644 --- a/src/file.ts +++ b/src/file.ts @@ -14,31 +14,35 @@ const TYPE_REG = /id=(\d+)&type=(img|mp3|midi)/ const magicHookConstr = (() => { const l = {} - hookNative(document.body, 'append', (fn) => { - return function (...nodes: Node[]) { - fn.call(this, ...nodes) + try { + hookNative(document.body, 'append', (fn) => { + return function (...nodes: Node[]) { + fn.call(this, ...nodes) - if (nodes[0].nodeName === 'IFRAME') { - const iframe = nodes[0] as HTMLIFrameElement - const w = iframe.contentWindow as Window + if (nodes[0].nodeName === 'IFRAME') { + const iframe = nodes[0] as HTMLIFrameElement + const w = iframe.contentWindow as Window - hookNative(w, 'fetch', (fn) => { - return function (url, init) { - const token = init?.headers?.Authorization - if (typeof url === 'string' && token) { - const m = url.match(TYPE_REG) - if (m) { - const type = m[2] - // eslint-disable-next-line no-unused-expressions - l[type]?.(token) + hookNative(w, 'fetch', (fn) => { + return function (url, init) { + const token = init?.headers?.Authorization + if (typeof url === 'string' && token) { + const m = url.match(TYPE_REG) + if (m) { + const type = m[2] + // eslint-disable-next-line no-unused-expressions + l[type]?.(token) + } } + return fn(url, init) } - return fn(url, init) - } - }) + }) + } } - } - }) + }) + } catch (err) { + console.error(err) + } return async (type: FileType) => { return new Promise((resolve) => {