# ClipboardAsyncJS **Repository Path**: w3csss/clipboard-async-js ## Basic Information - **Project Name**: ClipboardAsyncJS - **Description**: ClipboardJS支持同步拷贝接口数据逻辑 - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-08-09 - **Last Updated**: 2023-06-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # clipboard.js v2.0.11 基础上扩展 ## 扩展Promise回传如下: ```js var clipboardTarget = new ClipboardJS('.btnText', { target: function (target) { const res = new Promise((resolve,reject)=>{ // 异步请求数据 setTimeout(()=>{ document.querySelector('div').innerHTML = '异步数据: target
'+new Date() resolve(document.querySelector('div')) },2000) }) return res; }, }); clipboard.on('success', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); clipboard.on('error', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); var clipboardText = new ClipboardJS('.btn', { text: function (target) { const res = new Promise((resolve,reject)=>{ // 异步请求数据 setTimeout(()=>{ document.querySelector('div').innerHTML = '异步数据'+new Date() resolve('异步数据'+new Date()) },2000) }) return res; }, }); clipboard.on('success', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); clipboard.on('error', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); ``` ![Build Status](https://github.com/zenorocha/clipboard.js/workflows/build/badge.svg) ![Killing Flash](https://img.shields.io/badge/killing-flash-brightgreen.svg?style=flat) > Modern copy to clipboard. No Flash. Just 3kb gzipped. Demo ## Why Copying text to the clipboard shouldn't be hard. It shouldn't require dozens of steps to configure or hundreds of KBs to load. But most of all, it shouldn't depend on Flash or any bloated framework. That's why clipboard.js exists. ## Install You can get it on npm. ``` npm install clipboard-async --save ``` Or if you're not into package management, just [download a ZIP](https://github.com/zenorocha/clipboard.js/archive/master.zip) file. ## Setup First, include the script located on the `dist` folder or load it from [a third-party CDN provider](https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers). ```html ``` Now, you need to instantiate it by [passing a DOM selector](https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-selector.html#L18), [HTML element](https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-node.html#L16-L17), or [list of HTML elements](https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-nodelist.html#L18-L19). ```js new ClipboardJS('.btn'); ``` Internally, we need to fetch all elements that matches with your selector and attach event listeners for each one. But guess what? If you have hundreds of matches, this operation can consume a lot of memory. For this reason we use [event delegation](https://stackoverflow.com/questions/1687296/what-is-dom-event-delegation) which replaces multiple event listeners with just a single listener. After all, [#perfmatters](https://twitter.com/hashtag/perfmatters). # Usage We're living a _declarative renaissance_, that's why we decided to take advantage of [HTML5 data attributes](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes) for better usability. ### Copy text from another element A pretty common use case is to copy content from another element. You can do that by adding a `data-clipboard-target` attribute in your trigger element. The value you include on this attribute needs to match another's element selector. example-2 ```html ``` ### Cut text from another element Additionally, you can define a `data-clipboard-action` attribute to specify if you want to either `copy` or `cut` content. If you omit this attribute, `copy` will be used by default. example-3 ```html ``` As you may expect, the `cut` action only works on `` or `