The Need: you remember a word, not a chapter

I read translated books on my phone in a small reader I built. Each book has dozens of chapters. When I remember a word from a book but not where I saw it, I had two choices: open the table of contents and guess, or scroll.

I wanted the same thing every e-book app has. Type a word, see where it appears in the whole book, tap one, land on that spot.

The Proof: type a word, tap a result, see it highlighted

There is a new search button in the top bar. It opens a panel with a text box. After you type, the panel shows:

  • a count (“300+ results” if there are more than 300),
  • for each result, the chapter name and about 50 characters around the word.

Tap a result and the reader jumps to that paragraph. Every match in that chapter is highlighted in yellow, and the one you tapped is orange. A small bar at the bottom shows 7 / 300+ with previous and next buttons, so you can walk through the results without going back to the list.

I tested it on a commentary book of 32 chapters. Searching the Korean word for “God” gave 300 or more results. After tapping the 6th result, chapter 1 showed 56 highlighted words, and exactly one of them was orange. It also worked in my “page turning” mode, where the chapter is cut into screen-sized pages: tapping “next” moved to the right page, and the orange word was inside the page frame.

💬 Prompt that worked “Can keyword search be added to the viewer?”

I asked this as a question, not as an order. Claude read the code first and found that I have two different viewers: the reader inside the web app, and a standalone e-book file I made earlier. It did not start coding. It asked which one. I asked for a table of the differences, then answered: “Just make it for the app reader.” That took three short messages and avoided building the feature twice.

The Story: it worked on my PC, and on my phone the button did nothing

The search worked in my desktop browser. I copied the files to the NAS and rebuilt the container. A few hours later I wrote:

“The search button shows up, but it doesn’t work.”

The button was there, so the new page was loaded. But it did nothing, so the old JavaScript was running.

I checked the live site from the PC with curl. The server was sending the new reader.js (it contained the search code), the new HTML, and the new service worker version. So the server was fine.

The cause is how my service worker handles files. Page navigations go to the network first, so the HTML is always new. But scripts and styles use “show the saved copy first, then update it in the background”. So the phone showed the new HTML (with the button) and ran the saved old script (without the click handler). The new script was downloaded in the background and used on the next load. After I closed the app and opened it again, the search worked.

I made one wrong statement along the way. Before copying the files I said that no rebuild was needed, because only web files changed. That was wrong. My Dockerfile has COPY web ./web, so the web files are baked into the image. I noticed when I looked at the Dockerfile before copying, and I corrected it before the user ran anything. Still, I had said it with confidence, and I had not checked.

I did not change the service worker strategy. I only raised its version number (v33 to v34) and told myself to expect this again. A “network first for the reader files” rule would fix it, but it needs care so offline reading still works. I have not done it.

The How: search over the book text, not over the screen

Where the text comes from. The reader already downloads each book as JSON: a list of chapters, each with an html string. I search that data, not the page. The page only shows one chapter at a time.

1. Build an index on the first search. For each chapter, parse the HTML without running it, and keep the text of each top-level paragraph, split into its text nodes:

function buildSearchIndex() {
  sIndex = doc.chapters.map((c) => {
    const t = new DOMParser().parseFromString(c.html, 'text/html');
    return [...t.body.children].map((el) => {
      const w = t.createTreeWalker(el, NodeFilter.SHOW_TEXT), out = [];
      let n; while ((n = w.nextNode())) out.push(n.data);
      return out;
    });
  });
}

The paragraph order is the same as in the page, which matters in step 3.

2. Find matches and remember where. Every result is { c, p, o }: chapter number, paragraph number, and the character offset inside that paragraph. This is the same format the reader already uses to remember where I stopped reading. I did not invent a new position system.

for (let i = low.indexOf(q); i >= 0; i = low.indexOf(q, i + q.length)) {
  if (sHits.length >= MAX_HITS) break outer;      // 300
  sHits.push({ c, p, o: run + i, text: paras[p].join('') });
}

3. Jump with the old code. To go to a result, call the same function the table of contents uses, and pass the position:

showChapter(h.c, { p: h.p, o: h.o });

Scrolling mode and page-turning mode already know how to bring a {p, o} position into view. So the search needed no new scrolling code.

4. Highlight by splitting text nodes. After a chapter is drawn, walk its text nodes and wrap each match in <mark class="sh">. Splitting a text node into pieces does not change where lines break, so the page count in page-turning mode stays the same. The tapped result gets the extra class cur. I check it by comparing the same {c, p, o}.

5. One bar for next and previous. A small fixed bar with ‹ 7 / 300+ › ✕. Previous and next call the same jump function with index ± 1, wrapping around at the ends.

🗂 Claude.md Rule If a new button shows but does nothing on the phone, the page and the script may be from different versions. In this app, HTML is network-first and JS/CSS is cache-first. Check the live files with curl, then close and reopen the installed app before changing code. Also: a COPY web ./web in a Dockerfile means web-only changes still need docker compose up -d --build.

Small decisions:

  • Search is case-insensitive and does plain substring matching. For Korean this is enough: searching a noun also finds it with a particle attached.
  • Chapter titles are not searched. Footnotes at the end of a chapter are.
  • Picture markers like “[Illustration]” are skipped, because the page shows them as a decorative line.
  • Tapping a result saves that place as my reading position. This is the same as tapping a chapter in the table of contents.

Cost: $0. No AI API calls at runtime. The search runs in the browser on text it already has. I ran the app’s 18 existing tests (all passed) and checked the feature by driving the page in Chrome on my PC, in both reading modes.