Javascript API

From openZIM
Jump to: navigation, search

Overview

Zim files can be seem as subset and snapshot of a website. Webpages in the zim file could have totally different structure and layout.

The problem arises when interaction with the webpage is needed, since it is impossible to perform those interactions when there's little information regarding if and how an interaction should be performed.

Some kind of indication should be provided to allow zim readers to turn interactions on or off once a page is loaded. Further, implementations to perform those interactions needs to be included in the zim file itself.

Motivation

A basic function of zim readers is to provide a table of content of an article. But it is impossible come up with one javascript function to extract table of content in all sorts of zim files. It would make sense for each webpage to come with its own `getTableOfContent()` and `scrollToSection()` function. Some webpages, such as phet or main page, it might make little sense to provide table of content. A way to check `hasTableOfContent` could also be provided.

Proposed Solution

Add javascript with indication and implementation of capabilities to each offliner and load the javascript when page loads. A standard interface should be defined and documented.

Potential capabilities to include:

  • title
  • table of content
    • extraction
    • scrolling position indicating
    • scroll to section
  • font
    • size
    • line spacing
    • justification
  • night mode
  • readability
  • snippet
  • page thumb image
  • in page search

Capabilities

Table of content

 1 function TableOfContents () {
 2     this.getSections = function() {
 3         /*
 4         - return an array of section objects
 5         - in case there there is no table of content, return an empty array
 6         */
 7     }
 8 
 9     this.scrollToSection = function(index: number) {
10         /*
11         - scroll to a section with index in array returned by this.getSections
12         - perform change to the page so that the section become available for reading, such as expanding the collapsed section  
13         */
14     }
15 
16     // OPTIONAL
17     this.visibleSectionIndexes = function() {
18         /*
19         - return an array of visible section's indexes
20         - in case there there is no table of content, return an empty array
21         */
22     }
23 
24     // OPTIONAL
25     this.startVisibleSectionCallBack = function () {
26         /*
27         - start page visible section call back
28         - using this call back, the reader app could know which section is currently visible to the user and highlighting the location in table of content
29         - implementation to be discussed. It might not be a good idea to modify window.onscroll directly
30         */
31         var handleScroll = function() {
32             var visible = tableOfContents.visibleSectionIndexes();
33             if (visible.length > 0) {
34                 window.location = 'pagescroll:scrollEnded?start=' + visible[0] + '&length=' + visible.length;
35             }
36         }
37         window.onscroll = handleScroll;
38         handleScroll();
39     }
40 
41     // OPTIONAL
42     this.stopVisibleSectionCallBack = function () {
43         /*
44         - stop page visible section call back
45         */
46         window.onscroll = undefined;
47     }
48     
49 }
50 
51 function Section(title: string, level: number) {}