Projects / Libraries / Tokan.js

Tokan ("特観", "とくかん") is a Japanese compound word where "特" (toku) means "special" and "観" (kan) relates to "view" or "observation." This term aptly reflects the purpose of the Tokan library, which serves as a specialized tool for observing changes in the Document Object Model (DOM) of web applications.

Tokan is a library designed to simplify and enhance the functionality of the native MutationObserver API. The native API, while powerful, can be complex and cumbersome to use, especially for developers who need to track various types of changes in the DOM. Tokan addresses these challenges by providing a more user-friendly interface that abstracts away some of the complexities associated with the native API.

Key Features:


Types

TokanMutationFilterCallback

TokanMutationFilter

TokanMutationKindOptions

TokanEventCallback

TokanObserverDescriptor

Constants

TokanMutationKinds

TokanMutationEvents

Class: Tokan

Static Properties

Tokan.MutationKinds

ConstantTypeValueDescription
Attrstring"attributes"Represents changes to the attributes of elements.
CharDatastring"characterData"Represents changes to the text content of nodes.
Nodesstring"nodes"Represents additions or removals of child nodes.

Tokan.MutationEvents

ConstantTypeValueDescription
Addedstring"added"Triggered when nodes are added.
AttrChangedstring"attributeChanged"Triggered when attributes of elements change.
CharDataChangedstring"characterDataChanged"Triggered when the text content of nodes changes.
Removedstring"removed"Triggered when nodes are removed.

Constructor

MethodParametersDescription
constructortarget: string | NodeInitializes a new instance of the Tokan class with a target node or a CSS selector string.

Methods

MethodParametersReturnsDescription
observerRoutermutationList: MutationRecord[], observer: MutationObservervoidRoutes mutation records to the appropriate listeners based on the mutation type and filters.
watchmutationKind: TokanMutationKind, options?: TokanMutationKindOptionsnumber | undefinedSets up a new observer for a specific type of mutation.
unwatchid?: numberbooleanStops and removes an observer or all observers.
onevent: TokanMutationEvent, callback: TokanEventCallbackTokanAdds an event listener for a specific mutation event.
startid?: numbervoidStarts observing with a specific observer or all observers.
stopid?: numbervoidStops observing with a specific observer or all observers.

Constructor

constructor(target: string | Node)

Private Properties

target: Node

observerId: number

observers: Set<TokanObserverDescriptor>

listeners: { [key in TokanMutationEvent]: TokanEventCallback[] }

Methods

observerRouter(mutationList: MutationRecord[], observer: MutationObserver): void

watch(mutationKind: TokanMutationKind, options?: TokanMutationKindOptions): number | undefined

unwatch(id?: number): boolean

on(event: TokanMutationEvent, callback: TokanEventCallback): Tokan

start(id?: number): void

stop(id?: number): void

Example Usage

Setting Up an Observer

const tokan = new Tokan("#myElement");

const filterCallback: TokanMutationFilterCallback = (node) =>
  node.nodeName === "A";

const observerAttrsId = tokan.watch(Tokan.MutationKinds.Attr, {
  oldValue: true,
  subtree: true,
  filters: ["id", "class", "disabled"],
});

const observerNodesID = tokan.watch(Tokan.MutationKinds.Nodes, {
  subtree: true,
  filters: [filterCallback],
});

tokan.on(Tokan.MutationEvents.Added, (node) => {
  console.log("Node added:", node);
});

tokan.on(Tokan.MutationEvents.AttrChanged, (node, data) => {
  console.log("Attribute changed:", node, data);
});

tokan.start();

Stopping an Observer

tokan.stop();
tokan.unwatch();

Listening for Multiple Events

tokan.on(Tokan.MutationEvents.Removed, (node) => {
  console.log("Node removed:", node);
});

tokan.on(Tokan.MutationEvents.CharDataChanged, (node, data) => {
  console.log("Character data changed:", node, data);
});

tokan.start();