Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Viewer

Notices

Reply
 
Thread Tools Search this Thread
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
Old 06-27-2021, 12:07 PM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,289
Karma: 27111238
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
what version of calibre, what OS? As far as I know, calibre does send mimetypes for js files. It's likely a bug in Qt WebEngine's custom URL scheme handling.
kovidgoyal is online now   Reply With Quote
Advert
Old 06-27-2021, 12:13 PM   #3
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,289
Karma: 27111238
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
And I just tried it on the current calibre release, it works fine.
kovidgoyal is online now   Reply With Quote
Old 06-27-2021, 04:09 PM   #4
avnermoshkovitz
Member
avnermoshkovitz began at the beginning.
 
Posts: 22
Karma: 10
Join Date: Jun 2021
Device: Any ereader for Android or iOS
@kovidgoyal, thanks for replying
The Calibre version is 4.99.4. The OS is Ubuntu 20.04

Can you share a LINK TO Nn example with a call to javascript module?
Maybe I'm doing something incorrectly.

Thanks,
Avner

Code:
calibre --version
calibre (calibre 4.99.4)

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.2 LTS
Release:        20.04
Codename:       focal
avnermoshkovitz is offline   Reply With Quote
Old 06-27-2021, 06:13 PM   #5
BetterRed
null operator (he/him)
BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.
 
Posts: 21,685
Karma: 29711016
Join Date: Mar 2012
Location: Sydney Australia
Device: none
Quote:
Originally Posted by avnermoshkovitz View Post
@kovidgoyal, thanks for replying
The Calibre version is 4.99.4. The OS is Ubuntu 20.04

Can you share a LINK TO Nn example with a call to javascript module?
Maybe I'm doing something incorrectly.
Version 4.99.4 is one of a series of Calibre 5 beta test versions from Q3 2020.

Suggest you download and install the latest version from the calibre site ==>> https://calibre-ebook.com/

BR
BetterRed is offline   Reply With Quote
Advert
Old 06-27-2021, 08:12 PM   #6
avnermoshkovitz
Member
avnermoshkovitz began at the beginning.
 
Posts: 22
Karma: 10
Join Date: Jun 2021
Device: Any ereader for Android or iOS
I installed the latest Calibre version (5.22.1) from here

After install, I was able to load module javascript files, and see messages from console.log() in the terminal where I launched Calibre from.

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="module"/>

When I ran calibre with
Code:
calibre ebook2.epub
script2 works ok.
I can see messages from Console.log() in the terminal.

kovidgoyal, thanks for your help.
Avner
avnermoshkovitz is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript in ePub. Vijay ePub 22 12-28-2023 09:43 AM
Aura {A1} epub 3.0 renderer javascript support? pluma Kobo Reader 1 01-13-2017 03:53 PM
Hints for javascript in epub ? fxp33 Kobo Developer's Corner 6 08-09-2014 01:18 PM
Javascript in ePub Techno4 ePub 9 11-05-2012 12:50 PM
epub et javascript zelda_pinwheel Software 9 12-09-2010 04:56 PM


All times are GMT -4. The time now is 06:14 AM.


MobileRead.com is a privately owned, operated and funded community.