Skip to content

Commit

Permalink
Load EPUBs/snapshots from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
AbeJellinek committed Sep 15, 2023
1 parent 81863a1 commit 9c1bf99
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/dom/common/dom-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ abstract class DOMView<State extends DOMViewState, Data> {
// 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<void> {
this._iframe.srcdoc = await this._getSrcDoc();
return new Promise<void>((resolve, reject) => {
this._iframe.addEventListener('load', () => {
this._handleIFrameLoad().then(resolve, reject);
Expand Down Expand Up @@ -153,7 +153,7 @@ abstract class DOMView<State extends DOMViewState, Data> {
+ `script-src ${scriptSrc}; child-src ${childSrc}; form-action ${formAction}`;
}

protected abstract _getSrcDoc(): string;
protected abstract _getSrcDoc(): MaybePromise<string>;

abstract getData(): Data;

Expand Down
7 changes: 6 additions & 1 deletion src/dom/epub/epub-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,16 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
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');
}
}

Expand Down
22 changes: 14 additions & 8 deletions src/dom/snapshot/snapshot-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,19 @@ class SnapshotView extends DOMView<SnapshotViewState, SnapshotViewData> {

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');

Expand All @@ -69,11 +78,8 @@ class SnapshotView extends DOMView<SnapshotViewState, SnapshotViewData> {

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');
}
}

Expand Down

0 comments on commit 9c1bf99

Please sign in to comment.