Skip to content

Playground

Annotate the article, change the options and watch the data change. The Code tab shows the annotate() call for the current options. JSON is what instance.export() returns, W3C is the same data as W3C Web Annotations, and Events logs everything the instance emits.

The Highlight API

Most annotation tools wrap highlighted text in extra elements. That works until the page re-renders: a framework that owns the DOM throws the wrappers away, or trips over nodes it didn't create. penciled leaves the page alone and paints highlights with the CSS Custom Highlight API instead.

The browser keeps a registry of named highlights, each a set of ranges. A stylesheet then styles them with the ::highlight() pseudo-element, much like it styles a selection. Nothing is added to the document, so nothing can be thrown away.

Finding the text again after a reload is the harder part. Each annotation stores the quoted text, some context on either side and its position. When the page changes, penciled looks for the quote, compares the context, and only highlights a match it is confident about.

Select text, click highlights, then change the options below.Annotations here are saved under the document id docs-playground.

Appearance
Toolbar actions
Behavior
Placement

The instance is also available as window.penciled, so you can call its methods from the browser console.