Coder Social home page Coder Social logo

lolipop99 / clipboard.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lgarron/clipboard-polyfill

0.0 2.0 0.0 26 KB

:clipboard: Sane copying on the web.

Home Page: https://garron.net/code/clipboard.js/

License: Other

Makefile 4.23% JavaScript 71.58% HTML 24.20%

clipboard.js's Introduction

clipboard.js

Make copying on the web as easy as:

clipboard.copy("This text is plain.");

Note: in most browsers, copying is only allowed if clipboard.copy() is triggered in direct response to a user gesture like a click or a key press.

Copy rich text

clipboard.copy({
  "text/plain": "Markup text. Paste me into a rich text editor.",
  "text/html": "<i>Markup</i> <b>text</b>. Paste me into a rich text editor."
});

Copy a DOM node as markup

clipboard.copy(document.body);

(Uses XMLSerializer.)

Use the copy outcome as a Promise (optional):

clipboard.copy("test").then(
  function(){console.log("success");},
  function(err){console.log("failure", err);}
);

Paste

Pasting plain strings currently works in IE.

clipboard.paste().then(
  function(result) {console.log(result);},
  function(err) {console.log("failure", err);}
);

Usage

Get the source using one of the following:

  • clipboard.js or clipboard.min.js
  • npm install clipboard-js
  • bower install clipboard.js

Load the script:

<script src="clipboard.js"></script>

Then copy a string or an object (mapping data types to values) as above.

This project is half a year older. :-P
I created it partially to test the clipboard API while reviewing it for Chrome (I work on Chrome security), and partially to use in my own project.

I wouldn't have created this project if zenorocha/clipboard.js had already existed, but both projects have different uses right now. The fundamental difference is that this project hijacks the copy event, while zenorocha/clipboard.js uses fake element selection. Some details (as of November 2015):

This project zenorocha/clipboard.js
Supports plain strings, text/html, and DOM nodes Only supports plain strings
โ‰ˆ100 lines โ‰ˆ700 lines
1.5KB minimized + gzipped 2.9KB minimized + gzipped
Doesn't change document selection Clears document selection
Only an imperative API (clipboard.copy()) Declarative DOM-based API
Uses Promises -
Supports paste (in IE) -
  •                                              | Offers a fallback prompt (`Press Ctrl+C to copy`)
    

This is way too complicated!

Try this gist for a simpler solution.

  • Chrome 42+
  • Firefox 41+
  • Opera 29+
  • Internet Explorer 9+

clipboard.js's People

Contributors

lgarron avatar anton-ryzhov avatar danchr avatar khankuan avatar

Watchers

James Cloos avatar bear001 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.