npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xitanggg/node-selection

v1.1.0

Published

Get user's current selection text on desktop in Node.js via Node-API native addon

Downloads

96

Readme

🖱️Node Selection

This package provides a simple Node.js util that allows you to retrieve user's current selection text on desktop.

  • Support Windows and Mac
    • Mac: Need to grant accessibility permission to the calling app (Settings -> Privacy & Security -> Accessibility)
  • Require Node.js >= 10

📦Installation

npm i @xitanggg/node-selection

ℹ️Usage

Common Usage

import { getSelectionText } from '@xitanggg/node-selection;

const selectionText = getSelectionText();

Customized Usage

getSelectionText accepts an optional copyWaitTimeMs input argument, which sets how long to wait after performing the copy operation before reading the clipboard text. It defaults to 5ms, which works for most use cases with small selection text. However, a larger value would be needed to support use case for large selection text that takes longer to copy.

import { getSelectionText } from '@xitanggg/node-selection;

const LONG_COPY_WAIT_TIME_MS = 10;
const selectionText = getSelectionText(LONG_COPY_WAIT_TIME_MS);

💡Implementation

Core Logic

The implementation is written in Rust and is ~10 lines of code (see /src/lib.rs for full source code)

The selection text is retrieved in a 3 steps processes:

  1. Save clipboard existing text and clear clipboard
  2. Simulate Ctrl + C (Cmd + C in Mac) keyboard input to copy selection text to clipboard
  3. Read clipboard to retrieve selection text and return it as result (the previous clipboard text is restored before returning to minimize side effects to users)

Dependency

It uses Arboard (Arthur's Clipboard) to perform clipboard operation and enigo to perform keyboard input simulation

Build & Distribution

It uses NAPI-RS to compile the Rust source code into binaries via GitHub actions, package it as a Node-API native addon, and then publish it to npm for distribution and easy use.

One very nice thing about the NAPI-RS tooling is that the binary has been built, so this package just works after installation, i.e. no need to build it yourself. Also, the binary is selectively installed, meaning installation only installs the binary that your system needs, e.g. windows or Mac, to keep the size small instead of including all binaries at once.