!201 修复epub 跨域报错问题

Merge pull request !201 from 高雄/N/A
pull/202/head
陈精华 2023-08-06 14:14:47 +00:00 committed by Gitee
commit c249f68972
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 34 additions and 13 deletions

View File

@ -30,15 +30,35 @@
if (!url.startsWith(baseUrl)) { if (!url.startsWith(baseUrl)) {
url = baseUrl + 'getCorsFile?urlPath=' + encodeURIComponent(Base64.encode(url)); url = baseUrl + 'getCorsFile?urlPath=' + encodeURIComponent(Base64.encode(url));
} }
function blobToArrayBuffer(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
};
reader.readAsArrayBuffer(blob);
});
}
let xhr = new XMLHttpRequest();
xhr.open('GET',url); //文件所在地址
xhr.responseType = 'blob';
xhr.onload = () => {
var currentSectionIndex = 100; var currentSectionIndex = 100;
// Load the opf var book = ePub();
var book = ePub(url); let content = xhr.response;
//var rendition = book.renderTo("viewer", { flow: "scrolled-doc" }); let blob = new Blob([content]);
var bookData = blobToArrayBuffer(blob);
book.open(bookData, "binary");
var rendition = book.renderTo("viewer", { var rendition = book.renderTo("viewer", {
flow: "scrolled-doc", flow: "scrolled-doc",
width: "100%" width: "100%",
allowScriptedContent: true
// height: 600 // height: 600
}); });
var displayed = rendition.display();
var params = URLSearchParams && new URLSearchParams(document.location.search.substring(1)); var params = URLSearchParams && new URLSearchParams(document.location.search.substring(1));
var currentSectionIndex = (params && params.get("loc")) ? params.get("loc") : undefined; var currentSectionIndex = (params && params.get("loc")) ? params.get("loc") : undefined;
rendition.display(currentSectionIndex); rendition.display(currentSectionIndex);
@ -116,18 +136,19 @@
docfrag.appendChild(option); docfrag.appendChild(option);
}); });
$select.appendChild(docfrag); $select.appendChild(docfrag);
$select.onchange = function(){ $select.onchange = function(){
var index = $select.selectedIndex, var index = $select.selectedIndex,
url = $select.options[index].getAttribute("ref"); url = $select.options[index].getAttribute("ref");
rendition.display(url); rendition.display(url);
return false; return false;
}; };
}); });
}
xhr.send();
/*初始化水印*/ /*初始化水印*/
if (!!window.ActiveXObject || "ActiveXObject" in window) if (!!window.ActiveXObject || "ActiveXObject" in window)
{ {