Selection.js
A tiny (4kb) JavaScript cursor selection library for all browsers.
Install / Use
/learn @tcr/Selection.jsREADME
selection.js
A simple (4kb) library for manipulating cursor selections across all browsers:
- Modern browsers, using DOM Selection/Ranges
- IE5—8, using TextRanges
This library adds a window.Selection object with static methods for retrieving,
setting, and clearing a user selection. By manipulating the DOM, you can replicate
most DOM Range functionality (like extract/clone/deleteContents, etc.) or just
implement what your application actually needs.
Usage
Include "selection.js" in your project. The global window.Selection object is
has the following static methods. As a shorthand, new Selection(window) instantiates
an object with the same methods, but the window argument is no longer necessary.
- Selection.supported -- This property is
trueif the browser supports the Selection object. - Selection.hasSelection(window) -- Returns true if anything is currently selected.
- Selection.getOrigin(window) -- Returns an array [anchorNode, anchorOffset] of the current selection's starting anchor (in IE, returns leftmost anchor)
- Selection.getFocus(window) -- Returns an array [focusNode, focusOffset] of the current selection's focus anchor (in IE, returns rightmost anchor)
- Selection.getStart(window) -- Returns an array [node, offset] for the leftmost anchor.
- Selection.getEnd(window) -- Returns an array [node, offset] for the rightmost anchor.
- Selection.setSelection(window, originNode, originOffset, focusNode, focusOffset) -- Sets the selection to include the new origin anchor and focus anchor (in IE, origin will be leftmost anchor, focus rightmost)
- Selection.clearSelection(window) -- Deselects all content.
License
Released under the MIT license.
Related Skills
node-connect
345.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
106.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
345.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
345.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
