I am developing a web extension where a user uploads a book in epub
format to the client to parse.
To test my application, I have tried uploading several epub files but I can't seem to get past the book.open()
function that is within my handleFileSelection(event)
function.
I get the following error message:
GET https://www.fiverr.com/META-INF/container.xml 406
Failed to load book: {status: 406, message: 'Not Acceptable <br /><a href="https://www.fiverr.com/">Back</a>', stack: 'Error\n at XMLHttpRequest.handler (chrome-extens…cokhmkcaamghhkkhdlgfbcnemijkdil/epub.js:15977:18)'}
The container.xml
does not exist remotely and I believe this is what might be causing the issue.
Find below my code:
function handleFileSelection(event) { const file = event.target.files[0]; // Get the selected file const reader = new FileReader();
console.log('You are in handleFileSelection');
reader.onload = function (event) {
console.log('You are inside reader.onload');
const fileData = event.target.result; // Get the file data
const meta = document.createElement('meta');
meta.setAttribute('http-equiv', 'Content-Security-Policy');
meta.setAttribute('content', "script-src 'self' 'unsafe-inline'");
document.head.appendChild(meta);
const epubScript = document.createElement('script');
epubScript.src = chrome.runtime.getURL('./epub.js');
document.body.appendChild(epubScript);
const jszipScript = document.createElement('script');
jszipScript.src = chrome.runtime.getURL('./jszip.min.js');
document.body.appendChild(jszipScript);
const jszipUtilsScript = document.createElement('script');
jszipUtilsScript.src = chrome.runtime.getURL('./jszip-utils.min.js');
document.body.appendChild(jszipUtilsScript);
Promise.all([
new Promise((resolve) => {
epubScript.onload = resolve;
}),
new Promise((resolve) => {
jszipScript.onload = resolve;
}),
new Promise((resolve) => {
jszipUtilsScript.onload = resolve;
}),
])
.then(() => {
console.log('EPUB.js and JSZip libraries are loaded');
const book = ePub();
const blob = new Blob([fileData], { type: 'text/plain' });
const url = URL.createObjectURL(new Blob([fileData]));
book.open(url, { containerPath: 'META-INF/container.xml' })
.then(() => {
console.log(`Book "${book.loaded.metadata.title}" loaded successfully`);
})
.catch((error) => {
console.error('Failed to load book:', error);
});
})
.catch((error) => {
console.error('Failed to load EPUB.js or JSZip:', error);
});
};
reader.readAsArrayBuffer(file);
}