What iBooks Does Behind the Scenes

  • Sumo

With the new iBooks Desktop Web Inspector, we now have a little insight into how certain parts of the iBooks application works. If you have ever wondered why something looks a little funky or wanted to dig into using javascript in iBooks there are some very interesting files for you to take a look at.

How to Access the iBooks Application Javascript Files

Before I got through each of these, here’s how you can find them for yourself. (I’ve got links to GitHub Gists of these files later on as well.)

  1. Open iBooks, then open a non-DRM book
  2. Open the Web Inspector
  3. In the top bar of the Web Inspector, click on Resources
  4. in the lefthand sidebar, click on the folder titled “Extra Scripts.” These are the scripts that come packaged with the application.

Just what are these files?

As I mentioned before, these scripts are packaged with the iBooks application and are either used to process each title, or are used to manage the application interface itself. iBooks is partly made of html (the rendering of the EPUB pages) and other device specific code (like the Objective-C language in iOS). These two languages need to communicate with each other and that is partly handled by javascript.

Here’s a breakdown of each file included in that “Extra Scripts” folder and what it does. If you’re familiar with javascript you can probably dig into each file and understand a lot more about the internal workings of iBooks.

BKContentCleanup.js

See the GitHub Gist here. This file will probably be of the most interest to most EPUB Creators. This file processes the EPUB file and bends it to all the rules of the iBooks application. Ever added a <textarea> to an EPUB file and found you couldn’t enter text in that field in iBooks? That was because your file was manipulated through this file. There’s a lot to go through in here, but this might the file that is causing problems if you try to make more advanced EPUB files for use in iBooks.

BKContentTheme.js

See the GitHub Gist here. This file applies the iBooks theme (White, Sepia, and Night) and its styles to your document. This is helpful if you ever want to override particular theme styles.

BKReadingSystem.js

See the GitHub Gist here. This file applies the the EPUB3 JavaScript epubReadingSystem Object spec. Not much if you’re a regular EPUB user, but it you get into some crazy javascript stuff you’ll be happy to see this exists. Or if you’re just an EPUB spec geek.

One thing you won’t be happy about in this file, though, is this comment:

Screenshot 2014-07-24 12.23.48

Let’s hope they fix this in iOS 8 iBooks.

CFI.js

See the GitHub Gist here. This one is for the real EPUB spec geeks. This is Apple’s implementation of the Canonocal Fragment Identifier EPUB (EPUBcfi) spec. CFIs allow a reading system to store arbitrary pieces of text. CFIs are used in a lot of ways—for highlights and page positions.

There you have it. If you dig around through these files and find anything interesting be sure to leave a comment.

4 Responses to “What iBooks Does Behind the Scenes”

  1. Ever thought of writing an ebook on all these iBooks secrets? I used ID-CC-2014 to create the files, but it’d be great to understand what is happening.

  2. Barry O'Neill says:

    I would love to know of a tool that automates the indexing of EPUB’s using the CFI method, so that there’s clickable links to all the indexed words to the exact spot in the book. I’ve been looking high and low for days…no luck…

  3. I have problem with spin. I don’t want to put all page in spin.(I have 10pages but I need to show in spin only 8 pages).
    If I put linear=no in spin on ibook Opf. it is working in Mac ibooks but it is not working with ipad ibook.

    If I remove item in spin on opf. it is working in ipad ibooks, but in Mac ibook will button done when we hover on page.