Loading...
const html = p.innerHTML;
p.innerHTML = html.replaceAll(term, `<mark>${term}</mark>`);const range = new Range();
range.setStart(textNode, start);
range.setEnd(textNode, start + term.length);
CSS.highlights.set('search', new Highlight(range));Highlights paint ranges without changing the DOM, so no HTML is rebuilt and injection through innerHTML is avoided.
CSS.highlights differs from the one in ::highlight().CSS.highlights.set(name, new Highlight(range)), and the name matches ::highlight(name).color, background-color and text-decoration are used; others are ignored.Yes. A Range can start and end in different text nodes, and the highlight paints across them.
Call CSS.highlights.delete('search') to remove one, or CSS.highlights.clear() to remove all of them.