55
Hacking iBooks and EPUB with JavaScript! Jim McKeeth [email protected] @JimMcKeeth

Hacking iBooks and ePub3 with JavaScript!

Embed Size (px)

DESCRIPTION

[Updated for 2013] How to embed JavaScript in Interactive Widgets in iBooks Author 2 for use on the iPad. Also looks at embedded JavaScript in the page when creating ePub files from scratch. Uses DashCode, Tumult Hype 2, or your favorite text editor. Visit http://blogs.remobjects.com/?p=3862 for more information. From my presentation at 360iDev 2013 in Denver.

Citation preview

Page 1: Hacking iBooks and ePub3 with JavaScript!

Hacking iBooks and EPUB with JavaScript!

Jim [email protected]

@JimMcKeeth

Page 2: Hacking iBooks and ePub3 with JavaScript!

What 400 Level Means

• Assumed understanding of JavaScript and HTML5 web technologies.

• More examples and less explanation.

• Please ask questions as we go.

Page 3: Hacking iBooks and ePub3 with JavaScript!

About Jim McKeeth

• Lead Developer Evangelist for Embarcadero Technologies

• Delphi XE5 creates native iOS & Android apps from same code

• Improvisational performer with ComedySportz Boise

• Inventor of pattern and swipe to unlock

• US Patent #s 6,766,456 & 8,352,745 . . . (First patented in 2000)

• Used on iOS, Android and Windows 8

Page 4: Hacking iBooks and ePub3 with JavaScript!

Why Put JavaScript in a Book?

• Provide updated data to go along with book topic

• Allow real time collaboration between readers / students

• Create interactive story books

• Customize story to the reader

• Collect quiz responses

• Interesting animation effects

• Add games & bonuses

• Engage your readers

• Track readers

• Gather feedback

• Sky’s the limit!

Page 5: Hacking iBooks and ePub3 with JavaScript!

Overview

• JavaScript Interactive Widgets

• Dashcode

• Tumult Hype

• Creating Widgets from Scratch

• Creating ePub from Scratch

• Cracking ePub

Page 6: Hacking iBooks and ePub3 with JavaScript!

More Information

• 4 Part Series on iBooks JavaScript Widgets

• http://sn.im/ibooksjs

Page 7: Hacking iBooks and ePub3 with JavaScript!

iBooks Author 2

• Free in the Mac App Store

• Extension on ePub 3 standard (non-compatible)

• DRM and license only for iBooks on the iPad

• Nice powerful and easy interface for building books

• Be aware of orientations

Page 8: Hacking iBooks and ePub3 with JavaScript!

iBooks / ePub Technical Details

• Zip file of HTML5 content adhering to specific spec.

• 2 GB technical book size limit

• 200 MB recommended size

• iBooks IBA files encrypt the page content

• All resources must be included!

Page 9: Hacking iBooks and ePub3 with JavaScript!

3D Models

• 20,000 to 50,000 moderately textured polygons

• Textures <= 1024x1024

• Use COLLADA (.dae) file format

• Specify a camera

• Simple lighting

• http://support.apple.com/kb/ht5093

Page 10: Hacking iBooks and ePub3 with JavaScript!

3D Models• Trimble SketchUp Make

• www.sketchup.com

• Free version creates .dae 3D models that work in iBooks Author

• http://www.sketchup.com/products/3D-warehouse

Page 11: Hacking iBooks and ePub3 with JavaScript!

3D Model Demo

Page 12: Hacking iBooks and ePub3 with JavaScript!

Dashcode

Page 13: Hacking iBooks and ePub3 with JavaScript!

Dashcode

• JavaScript, HTML and CSS Editor from Apple

• For creating Dashboard widgets

• Part of Xcode until Xcode 4.3

• Now a separate download: http://bit.ly/Dashcode Latest: “Late July” for Aug 7, 2012 - 3.0.5

Page 14: Hacking iBooks and ePub3 with JavaScript!

Demo Dashcode

Page 15: Hacking iBooks and ePub3 with JavaScript!

Things to Remember

1. Backwards compatibility on, or parts disappear.

2. The back of the widget never gets displayed.

3. Text doesn’t show up in the default image unless you change the option in the inspector.

4. Dashcode text parts layout odd on the iPad.

5. Java and other plugins don’t work on the iPad.

Page 16: Hacking iBooks and ePub3 with JavaScript!

Posting to Web Services

• External web services must implement CORS

• (Cross-Origin Resource Sharing) headers.

• enable-cors.org

• www.w3.org/TR/cors

Page 17: Hacking iBooks and ePub3 with JavaScript!

Tumult Hype 2

• HTML5 & JavaScript Multimedia Studio

• Create JavaScript Powered Animations and Interactions

• Creates Widgets Directly

• Compatible with iBooks Author & the iPad

• $59.99 in Mac App Store (on sale $29.99 through Sept 10th)

Page 18: Hacking iBooks and ePub3 with JavaScript!

Tumult Hype Demo

Page 19: Hacking iBooks and ePub3 with JavaScript!

Creating from Scratch

• Required files:

• Main.html - Main HTML file (any name will work).

• Default.png – Preview image and place holder.

• Info.plist – Property list describing widget. References main HTML.

http://support.apple.com/kb/HT5068

Page 20: Hacking iBooks and ePub3 with JavaScript!

Required Properties

• CFBundleIdentifier - Unique identifier

• CFBundleName - Friendly name

• MainHTML - The main HTML file

Page 21: Hacking iBooks and ePub3 with JavaScript!

Info.plist

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict>! <key>CFBundleIdentifier</key>! <string>com.360idev.gallery</string>! <key>CFBundleName</key>! <string>Gallery</string>! <key>MainHTML</key>! <string>index.html</string></dict></plist>

Page 22: Hacking iBooks and ePub3 with JavaScript!

Making a Package

• Put all files in a directory - Subdirectories are OK (use relative paths for references).

• Rename root directory .wdgt

• Don’t Install Package! It gets deleted. Use Duplicate to test.

• Context / Show Package Contents to edit.

Page 23: Hacking iBooks and ePub3 with JavaScript!

Supported HTML5 Features

• localStorage (per book - between widgets)

• Geolocation (requests permission)

• Canvas

• CSS Animations

• Others . . .

Page 24: Hacking iBooks and ePub3 with JavaScript!

Prevent Flicker

1. Add a IBNotifiesOnReady item set to true in the Dashboard widget's info.plist file, so iBooks knows this Dashboard widget will tell it when to switch to displaying the running HTML widget.

2. Set your Dashboard widget to load the Apple javascript library.

3. In javascript call widget.notifyContentIsReady() to notify iBooks it can switch from showing the Default.png file to displaying the running HTML widget.

http://support.apple.com/kb/HT5068

Page 25: Hacking iBooks and ePub3 with JavaScript!

From Scratch Demos

Page 26: Hacking iBooks and ePub3 with JavaScript!

Things to Remember

1. Double clicking a widget on desktop deletes it (installs it into the library).

2. The aspect ratio of your Default.png determine the aspect ratio of your widget.

3. All JavaScript libraries must be referenced from the Main HTML file, and in the package.

Page 27: Hacking iBooks and ePub3 with JavaScript!

bookry.com

• Custom Sliding Puzzle

• Live Twitter Feed

• Interactive Timeline Images

• YouTube Video

• Vimeo Video

• Google Maps

• Live Polling

• More to come . . .

• Auto-generated customized widgets ready to add to iBooks (or look at the code to figure out how they did it.)

• Free license includes “branding”

See also ibooksgenerator.com

Page 28: Hacking iBooks and ePub3 with JavaScript!

Live Twitter feed in iBooks

Page 29: Hacking iBooks and ePub3 with JavaScript!

Live Google Maps in iBooks

Page 30: Hacking iBooks and ePub3 with JavaScript!

Creating an ePub from ScratchNo iBooks Author, no Dashcode, just text editing!

Page 31: Hacking iBooks and ePub3 with JavaScript!

Disclaimer

• So far I have been unable to access online content without using iBooks Author.

Page 32: Hacking iBooks and ePub3 with JavaScript!

International DigitalPublishing Forum

• Develops and maintains the EPUB content publication standard

• idpf.org/epub

• ePub 3.0 is current standard

• October 2011

• Official support for JavaScript

Page 33: Hacking iBooks and ePub3 with JavaScript!

Progressive Enhancements& Fallbacks

Page 35: Hacking iBooks and ePub3 with JavaScript!

2.4.2 Content Conformance

• A container-constrained script must not contain instructions to:

• modify the DOM of the parent Content Document

• modify other contents in the Publication

• manipulate the size of its containing rectangle

• Container-constrained scripts

Page 36: Hacking iBooks and ePub3 with JavaScript!

2.4.2 Content Conformance

• EPUB Content Documents that include spine-level scripting must utilize the progressive enhancement technique:

• When rendered without scripting support document must retain its integrity, remaining consumable by the User without any information loss or other significant deterioration.

• Use fallbacks!

• iBooks doesn’t seem to allow network communications at spine-level

• Spine-level scripts

Page 37: Hacking iBooks and ePub3 with JavaScript!

The JavaScript epubReadingSystem Object

• Syntax:

• ReadingSystem = navigator.epubReadingSystem;

• Properties: name, version, layoutStyle

• Method: hasFeature(feature)

• dom-manipulation, layout-changes, touch-events, mouse-events, keyboard-events, spine-scripting

Appendix B 

Page 38: Hacking iBooks and ePub3 with JavaScript!

preventDefault

• Use to “override” the default behavior of the reading system.

• If you have a touch zone near the edge of the screen, this gives you the touch event instead of turning the page.

• Turn off for whole document:

• document.addEventListener('touchmove', function (e) {e.preventDefault();}, false);

• In individual event handler :

• event.preventDefault();

Page 40: Hacking iBooks and ePub3 with JavaScript!

ePub Format

• A zip file with epub extension

• mimetype must be the first file in the zip

• Container.xml references the opf file (which can be named anything)

• All content files must be listed in the opf file

• mimetype

• iTunesMetadata.plist

• META-INF

• container.xml

• OPS

• content.opf

• content files

Automaticallyadded by iTunesbut not needed.

Page 41: Hacking iBooks and ePub3 with JavaScript!

mimetype

• First file in zip - uncompressed

• No Unicode signature or byte order mark

• Only contain

• application/epub+zip

Page 42: Hacking iBooks and ePub3 with JavaScript!

container.xml

<?xml version="1.0" encoding="UTF-8"?><container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0"> <rootfiles> <rootfile

full-path="OPS/content.opf" media-type="application/oebps-package+xml"/>

</rootfiles></container>

Page 43: Hacking iBooks and ePub3 with JavaScript!

Content OPF

<?xml version="1.0" encoding="UTF-8"?><package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="uid" prefix="rendition: http://www.idpf.org/vocab/rendition/# cc: http://creativecommons.org/ns#"> <metadata xmlns:dc="http://purl.org/dc/elements/1.1/"> <dc:xxx>. . .</dc:xxx> </metadata> <manifest> <item>. . .</item> </manifest> <spine> <itemref ... /> </spine></package>

Page 44: Hacking iBooks and ePub3 with JavaScript!

Content OPFmetadata

•The metadata section describes the book

<metadata xmlns:dc="http://purl.org/dc/elements/1.1/"> <dc:title id="title">JavaScript Test Book</dc:title> <dc:description>Test of spine-level scripting</dc:description> <dc:creator>Jim McKeeth</dc:creator> <dc:identifier id="uid">8181AEF2-CD21-4FE6-9AE8-9A83572FF4D</dc:identifier> <dc:language>en-US</dc:language> <meta property="dcterms:modified">2012-09-11T12:47:00Z</meta> <dc:rights>

Licensed under a Creative Commons Attribution 3.0 License. </dc:rights>

<link rel="cc:license" href="http://creativecommons.org/licenses/by-sa/3.0/"/>

</metadata>

Page 45: Hacking iBooks and ePub3 with JavaScript!

Content OPFmanifest

•The manifest section must list all other files in the ePub.•There must be exactly one with property “nav” and up to one “cover-image”•The “scripted” property indicates the page executes JavaScript.

<manifest> <item id="i" href="main.xhtml" media-type="application/xhtml+xml"

properties="scripted"/> <item id="nav" href="nav.xhtml" media-type="application/xhtml+xml"

properties="nav"/> <item id="prt" href="default.png" media-type="image/png"

properties="cover-image"/> <item id="first" href="first.jpg" media-type="image/jpeg"/> <item id="css" href="ro.css" media-type="text/css"/> <item id="scrpt" href="main.js" media-type="application/javascript"/> <item id="jq" href="jQuery.js" media-type="application/javascript"/> </manifest>

Page 46: Hacking iBooks and ePub3 with JavaScript!

Content OPFspine

•Must list all “sections” on the spine. Includes chapters, preface, glossary, etc. •The idref value is defined in the manifest section

<spine> <itemref idref="i" properties="rendition:layout-reflowable rendition:spread-auto"/> </spine>

Page 47: Hacking iBooks and ePub3 with JavaScript!

Zipping an ePub

• zip -X ../epubfile.epub mimetype

• zip ../epubfile.epub META-INF/*

• zip -r ../epubfile.epub OPS/*

• unzip -l ../epubfile.epub

http://idpf.org/epub/30/spec/epub30-ocf.html

mimetype must be the first file (uncompressed).

Any folder(s) with “content”

Lists the contents for verification

Page 48: Hacking iBooks and ePub3 with JavaScript!

ePub Validator

• EpubCheck

• validator.idpf.org (web based)

• code.google.com/p/epubcheck/ (download - Java)

Download and include call in your script to zip your ePub!

Page 49: Hacking iBooks and ePub3 with JavaScript!

iBooks Fixed Layout Format

• Use a separate XHTML document for each Fixed Layout page.

• Use CSS relative and/or absolute positioning to place text and images on a page.

• Include the height and width of the page in a viewport meta tag in the head of each XHTML document. (used for aspect ratio)<head>          <title>Fixed Layout Example</title>        <meta content="text/html; charset=UTF-8"/>        <link href="css/stylesheet.css" type="text/css" rel="stylesheet"/>        <meta name="viewport" content="width=575, height=432"/></head>

Page 50: Hacking iBooks and ePub3 with JavaScript!

From Scratch ePub Demo

Page 51: Hacking iBooks and ePub3 with JavaScript!

Cracking ePub

Page 52: Hacking iBooks and ePub3 with JavaScript!

iBooks DRM Decrypt

• iTunes 10.5.3.3 x64 (as close as possible)

• http://www.oldversion.com/windows/itunes/

• Requiem 3.3.6

• https://tag3ulp55xczs3pn.onion.to/ (via Tor)

Must be these versions working together.

Run it in an XP Virtual Machine.

Page 53: Hacking iBooks and ePub3 with JavaScript!

Other ePub Books

• Calibre

• http://calibre-ebook.com/ (eBook conversion and management)

• DeDRM plugins

• http://apprenticealf.wordpress.com/ 6.05 is latest

Page 54: Hacking iBooks and ePub3 with JavaScript!

Other Possible Resources

• Threepress Consulting Blog

• http://blog.threepress.org/

• ePub Straight to the Point by Elizabeth Castro

• http://www.elizabethcastro.com/epub/Have not read, but was recommended.

Page 55: Hacking iBooks and ePub3 with JavaScript!

More Information

• 4 Part Series on iBooks JavaScript Widgets

• http://sn.im/ibooksjs

• Might become an iBook

[email protected] or @JimMcKeeth