From 9c1bf99d5cf61e2d4796190ad9142aa682d59625 Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Fri, 15 Sep 2023 12:03:26 -0400 Subject: [PATCH] Load EPUBs/snapshots from URL --- src/dom/common/dom-view.tsx | 4 ++-- src/dom/epub/epub-view.ts | 7 ++++++- src/dom/snapshot/snapshot-view.ts | 22 ++++++++++++++-------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/dom/common/dom-view.tsx b/src/dom/common/dom-view.tsx index e16b16d1..a9a44a0c 100644 --- a/src/dom/common/dom-view.tsx +++ b/src/dom/common/dom-view.tsx @@ -113,11 +113,11 @@ abstract class DOMView { // support the csp attribute (currently all browsers besides Chrome derivatives) this._iframe.setAttribute('csp', this._getCSP()); this.initializedPromise = this._initialize(); - this._iframe.srcdoc = this._getSrcDoc(); options.container.append(this._iframe); } protected async _initialize(): Promise { + this._iframe.srcdoc = await this._getSrcDoc(); return new Promise((resolve, reject) => { this._iframe.addEventListener('load', () => { this._handleIFrameLoad().then(resolve, reject); @@ -153,7 +153,7 @@ abstract class DOMView { + `script-src ${scriptSrc}; child-src ${childSrc}; form-action ${formAction}`; } - protected abstract _getSrcDoc(): string; + protected abstract _getSrcDoc(): MaybePromise; abstract getData(): Data; diff --git a/src/dom/epub/epub-view.ts b/src/dom/epub/epub-view.ts index 2ef9f24c..f2ab120b 100644 --- a/src/dom/epub/epub-view.ts +++ b/src/dom/epub/epub-view.ts @@ -91,11 +91,16 @@ class EPUBView extends DOMView { this.book = Epub(options.data.buf.buffer); delete this._options.data.buf; } + else if (options.data.baseURI) { + this.book = Epub(options.data.baseURI, { + openAs: 'epub' + }); + } else if (options.data.book) { this.book = options.data.book; } else { - throw new Error('buf or book is required'); + throw new Error('buf, baseURI, or book is required'); } } diff --git a/src/dom/snapshot/snapshot-view.ts b/src/dom/snapshot/snapshot-view.ts index 36190645..7d8d9c7c 100644 --- a/src/dom/snapshot/snapshot-view.ts +++ b/src/dom/snapshot/snapshot-view.ts @@ -43,10 +43,19 @@ class SnapshotView extends DOMView { private _scale!: number; - protected _getSrcDoc() { - let enc = new TextDecoder('utf-8'); - if (this._options.data.buf) { - let text = enc.decode(this._options.data.buf); + protected async _getSrcDoc() { + if (this._options.data.srcDoc) { + return this._options.data.srcDoc; + } + else if (this._options.data.buf || this._options.data.baseURI !== undefined) { + let buf; + if (this._options.data.buf) { + buf = this._options.data.buf; + } + else { + buf = await fetch(this._options.data.baseURI!).then(r => r.arrayBuffer()); + } + let text = new TextDecoder('utf-8').decode(buf); delete this._options.data.buf; let doc = new DOMParser().parseFromString(text, 'text/html'); @@ -69,11 +78,8 @@ class SnapshotView extends DOMView { return new XMLSerializer().serializeToString(doc); } - else if (this._options.data.srcDoc) { - return this._options.data.srcDoc; - } else { - throw new Error('buf or srcDoc is required'); + throw new Error('buf, baseURI, or srcDoc is required'); } }