View Single Post
Old 06-27-2021, 09:19 AM   #1
avnermoshkovitz
Member
avnermoshkovitz began at the beginning.
 
Posts: 22
Karma: 10
Join Date: Jun 2021
Device: Any ereader for Android or iOS
How to load javascript module in epub xhtml

I have an ebook (ebook1.epub) that loads a javascript file (script1.js) like so:

Code:
cat EPUB/xhtml/raw/ch1.xhtml
<!--?xml version='1.0' encoding='UTF-8'?-->
<!DOCTYPE html><html xml:lang="en-us" lang="en-us" xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:xlink="http://www.w3.org/1999/xlink">
    <head>
        <script type="text/javascript" src="../../js/script1.js"></script>
...
    </head>
...
</html>

# --------------------------------------------------------------

cat EPUB/package.opf
...
<item id="js4" href="js/script1.js" media-type="text/javascript"/>
This works ok. I view the book with:

Code:
calibre ebook1.epub
script1 works as expected, (e.g. calling console.log("foo1") from within script1 responds as expected and prints to the terminal where calibre was launched).

-------

Now, I need to use Javascript module file (ES6) (script2.js)
The javascript module file is loaded like so:

Code:
cat EPUB/xhtml/raw/ch1.xhtml
<!--?xml version='1.0' encoding='UTF-8'?-->
<!DOCTYPE html><html xml:lang="en-us" lang="en-us" xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:xlink="http://www.w3.org/1999/xlink">
    <head>
        <script type="module" src="../../js/script2.js"></script>
...
    </head>
...
</html>

# --------------------------------------------------------------

cat EPUB/package.opf
...
<item id="js4" href="js/script2.js" media-type="text/javascript"/>
```
When I ran calibre with

Code:
calibre ebook2.epub
script2 does not work.
Calling function from script2:
- does not print anything to the terminal.
- the terminal shows an error message:

Code:
ERROR: clbr://internal.sandbox/book/EPUB/js/script2.js:0: Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
How can I load a javascript module file in the .epub?

Thanks,
Avner
avnermoshkovitz is offline   Reply With Quote