lk-text-select-highlight
v1.0.8
Published
A lightweight JavaScript library for highlighting selected text on web pages
Maintainers
Readme
lk-text-select-highlight
A lightweight JavaScript library for highlighting selected text on web pages.
Installation
npm install lk-text-select-highlightUsage
ES6 Import
import TextHighlighter from 'lk-text-select-highlight';
const highlighter = new TextHighlighter({
className: 'my-highlight',
color: '#ffeb3b'
});
// Highlight selected text
document.addEventListener('mouseup', () => {
highlighter.highlightSelection();
});Browser Script Tag
<script src="https://unpkg.com/lk-text-select-highlight/dist/lk-text-select-highlight.min.js"></script>
<script>
const highlighter = new TextHighlighter({
className: 'my-highlight',
color: '#ffeb3b'
});
// Highlight selected text
document.addEventListener('mouseup', () => {
highlighter.highlightSelection();
});
</script>API
Constructor
new TextHighlighter(options)
Creates a new TextHighlighter instance.
Options
className: CSS class name for highlighted elements (default:"highlighted-text")color: Background color for highlights (default:"#ffff00")container: DOM element to restrict text selection/highlighting (default:document.body)strictMode: Prevents highlighting selections that contain elements with the target className (default:false)caseSensitive: Whether search is case-sensitive (default:true)
Methods
highlightSelection(): Highlights the currently selected textremoveAllHighlights(): Removes all highlightsremoveHighlight(highlight): Removes a specific highlightgetHighlights(): Gets all highlight objectssetHighlights(highlights): Sets highlight data from an array of highlight objects
Testing
To run the tests:
npm testThe library includes comprehensive unit tests covering all major functionality including initialization, custom options, strict mode handling, and highlight management.
License
MIT
