Socket
Socket
Sign inDemoInstall

copy-to-clipboard

Package Overview
Dependencies
1
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    copy-to-clipboard

Copy stuff into clipboard using JS with fallbacks


Version published
Weekly downloads
4.4M
decreased by-17.08%
Maintainers
1
Install size
18.0 kB
Created
Weekly downloads
 

Package description

What is copy-to-clipboard?

The copy-to-clipboard package is a simple and versatile tool for copying text to the clipboard in web applications. It provides an easy-to-use API that works across different browsers and platforms, making it a popular choice for developers looking to implement copy functionality without dealing with the complexities of the Clipboard API directly.

What are copy-to-clipboard's main functionalities?

Copy text to clipboard

This feature allows you to copy a specified string of text to the user's clipboard. It's useful for copying content like URLs, codes, or any text content that users might want to use outside the web application.

copy('Text to be copied');

Copy with options

This feature enhances the basic copy functionality by allowing you to pass an options object. The 'debug' option enables console output for debugging, and the 'message' option allows you to customize the instruction message shown to users on how to copy when automatic copying fails (useful for older browsers).

copy('Text to be copied', { debug: true, message: 'Press #{key} to copy' });

Other packages similar to copy-to-clipboard

Readme

Source

Copy to clipboard Build Status

Simple module exposing copy function that will try to use execCommand with fallback to IE-specific clipboardData interface and finally, resort to usual prompt with proper text content and message.

If you are building using Electron, use their API.

Example

import copy from 'copy-to-clipboard';

copy('Text');

// Copy with options
copy('Text', {
  debug: true,
  message: 'Press #{key} to copy',
});

API

copy(text: string, options: object): boolean — tries to copy text to clipboard. Returns true if no additional keystrokes were required from user (so, execCommand, IE's clipboardData worked) or false.

ValueDefaultNotes
options.debugfalseBoolean. Optional. Enable output to console.
options.messageCopy to clipboard: #{key}, EnterString. Optional. Prompt message. *
options.format"text/html"String. Optional. Set the MIME type of what you want to copy as. Use text/html to copy as HTML, text/plain to avoid inherited styles showing when pasted into rich text editor.
options.onCopynullfunction onCopy(clipboardData: object): void. Optional. Receives the clipboardData element for adding custom behavior such as additional formats

* all occurrences of #{key} are replaced with ⌘+C for macOS/iOS users, and Ctrl+C otherwise.

Browser support

Works everywhere where prompt* is available. Works best (i.e. without additional keystrokes) in Chrome, FF, Safari 10+, and, supposedly, IE/Edge.

Note: does not work on some older iOS devices.
* – even though Safari 8 has prompt, you cannot specify prefilled content for prompt modal – thus it doesn't work as expected.

Installation

  • Can be used as npm package and then leveraged using commonjs bundler/loader:
npm i --save copy-to-clipboard
  • Can be utilized using wzrd.in. Add following script to your page:
<script src="https://wzrd.in/standalone/copy-to-clipboard@latest" async></script>

You will have window.copyToClipboard exposed for you to use.

UI components based on this package:

See also:

Running Tests

This project has some automated tests, that will run using nightwatch on top of selenium.

npm i
npm test

Typescript

This library has built-in Typescript definitions.

import * as copy from 'copy-to-clipboard';

Keywords

FAQs

Last updated on 13 Nov 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc