How Legado Reader Works and Why an Android App Needs a Built-in JS Engine
Personally, I rarely pay attention to mobile e-readers. Usually they're either wrappers around system text rendering or closed apps packed with ads and subscriptions. But the Legado repository caught my attention. Under the hood of this Android reader lies an architecture you'd more likely expect to find in a server-side data harvesting powerhouse than in a leisure-time book reading utility.
The app is written in Kotlin and distributed under the GPL-3.0 license. Essentially, it's a web page scraper, a user script execution engine, and a local web server bundled into a single mobile interface.
What's Inside a Mobile Scraper
If you look at the dependency list in the README, it becomes clear how the authors approached the design. Instead of being tightly coupled to specific formats or websites, they created a flexible tool for extracting content from anywhere.
Here's how this system works:
- HTML and JSON parsing. The project uses Jsoup, JsoupXpath, and json-path. Chapter text, titles, and tables of contents are extracted from web pages using XPath and JSONPath queries.
- JavaScript execution. To handle dynamic pages, the app embeds rhino-android (a Mozilla Rhino JS engine port). If a site delivers content via client-side scripts or uses non-trivial markup, Legado executes custom JS right on the device.
- Local web server. Nanohttpd and nanohttpd-websocket are used. The app spins up an HTTP and WebSocket server right on the smartphone. You can enter the phone's IP address in your computer's browser to read books on a big screen or manage parsing rules.
- Code editor right in the UI. The io.github.rosemoe:editor library adds a code editor with syntax highlighting. You can tweak a text extraction rule directly from the phone screen.
How the Source Concept Works
Legado's main feature is the separability of the data source from the reader itself. The app doesn't contain a built-in book database. Instead, the user imports configuration files in JSON format.
Each such file describes the structure of the target site. It specifies search rules for books, paths to chapters, and selectors for extracting text. If a site is protected by simple scripts, JS code gets embedded into the configurator. The result is that the reader turns into a flexible content cleaner: it removes banners, formats paragraphs, and assembles a ready EPUB on the fly.
For working with linguistic material, the authors integrated HanLP — a natural language processing library. This helps with Chinese texts, proper word wrapping, and character conversion.
Potential Challenges You Might Face
The documentation in the repository itself is extremely sparse. The README is limited to listing the libraries used and a couple of links. Most of the community and ready-made parsing rules are concentrated in the Chinese internet, so out of the box you won't find much in English or Russian.
To set up book extraction from a specific Russian-language resource, you'll need to understand Legado's JSON source format and write the rules manually. On the other hand, the built-in editor with highlighting makes this easier if you're familiar with XPath.
Why Study This Project's Source Code
Even if you don't plan to read web novels, the Legado codebase is interesting as a technical case study.
- Background processing architecture. The code demonstrates how to organize parallel page loading and parsing without UI degradation.
- Embedding server components. Using NanoHTTPD for phone-to-PC communication provides a practical example of building local hybrid interfaces.
- Safe JS execution on Android. The project clearly shows the Kotlin and Rhino pairing for handling user code.
Legado is an interesting example of how enthusiasts can turn a familiar mobile app into an advanced platform for working with data. The project is worth cloning and studying for Android developers looking for practical examples of integrating web technologies, scripting engines, and background services in Kotlin.
Related projects