javascriptepub.js

How to bind events to the rendition in epub.js


I want to register a right click listener on the page in epub.js but don't know how to do it. There is also passEvents method of rendition object but couldn't find any help on that either. This is my last try:

rendition.on("rendered", () => {
    const contents = rendition.getContents()
    contents.document.addEventListener('contextmenu', showContextMenu, false);
});

Solution

  • Based on what you asked and I hope I got it right, you want a contextmenu event at the book itself, right?

    If that's the case I used the following JS:

    rendition.on("rendered", (e,i) => {;
      i.document.documentElement.addEventListener('contextmenu', (cfiRange, contents) => {
          console.log('hey');
      })
    });
    

    This code simply returns hey when I right click at the book. But as you can see there are two parameters (cfiRange, contents) which contains what you need.

    In any case, I created a fiddle.

    Another solution would be use document as the element receiving the event, but in my tests it gets everything but the book.