Register Guidelines E-Books Today's Posts Search

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

Notices

Reply
 
Thread Tools Search this Thread
Old 05-21-2023, 07:23 PM   #1
gonzalo_diaz
Junior Member
gonzalo_diaz began at the beginning.
 
Posts: 5
Karma: 10
Join Date: May 2023
Device: Kindle
Detecting tags in header

Hello,

When converting an EPUB to PDF, in the custom "Header Template" section, I would like to use JavaScript in order to show the closest text of a given tag type, say, whatever is the value o the closest <h2>.

In order to do that, I tried to enhance the JavaScript example provided in the documentation, for example:

<header>
...
<div></div>

<script>
var thisDiv = document.currentScript.parentNode.querySelector("d iv");
var first = document.getElementsByTagName('h2').firstChild[0];

thisDiv.innerHTML = "" + first.innerHTML

</script>
...
</header>

But the problem with that code, is that the DOM has not been evaluated at the time where I try to grab the H2 content. And the normal JavaScript tricks to overcome this problem (adding "defer" or "module" to the <script> tag, or putting it all inside the document's load event), also don't seem to work.

So, my question is: Only the predefined variables such as _SECTION_ or _TOP_LEVEL_SECTION_ are available in the header script? Is it not possible to grab the value of the "closest()" text of a specific tag type?
gonzalo_diaz is offline   Reply With Quote
Old 05-21-2023, 09:37 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,345
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Headers and footers are evaluated in dummy HTML files they dont have access to book content. If you want to get title information use _SECTION_
kovidgoyal is online now   Reply With Quote
Advert
Old 05-22-2023, 09:22 PM   #3
gonzalo_diaz
Junior Member
gonzalo_diaz began at the beginning.
 
Posts: 5
Karma: 10
Join Date: May 2023
Device: Kindle
OK, I understand.
Thanks for the answer.
gonzalo_diaz is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Are too many single-use tags (in tags-like field(s)) bad for Calibre's performance? 01100001 Library Management 10 09-21-2022 09:39 AM
In epub converting calibre auto split file in header. Why not create toc from header. The_book Conversion 7 11-06-2020 09:09 AM
Parsing tags from a bibliographic entry and/or getting tags from Library of Congress kirk8677 Library Management 2 05-27-2020 06:48 PM
How to sync MacOs Finder Tags with Calibre Tags EmilThelonius Calibre 13 09-11-2019 12:53 PM
Amazon Tags - Popular tags vs Unique tags. chrisanthropic Writers' Corner 6 09-19-2011 11:18 PM


All times are GMT -4. The time now is 10:58 PM.


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