├── package.json ├── index.html ├── cheval.min.js ├── README.md ├── cheval.js └── LICENSE /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cheval", 3 | "version": "2.0.0", 4 | "description": "Copy to the clipboard using JavaScript without writing JS.", 5 | "main": "cheval.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/ryanpcmcquen/cheval.git" 12 | }, 13 | "keywords": [ 14 | "clipboard" 15 | ], 16 | "author": "Ryan McQuen", 17 | "license": "MPL-2.0", 18 | "bugs": { 19 | "url": "https://github.com/ryanpcmcquen/cheval/issues" 20 | }, 21 | "homepage": "https://github.com/ryanpcmcquen/cheval#readme" 22 | } 23 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | 7 |
14 | 17 | 18 |
19 | 20 | 21 |22 | 23 |
24 | 25 | 26 |27 | 28 |
29 | 30 | 31 |32 | 33 |
34 | 35 | 36 |37 | 38 |
39 | 40 |textarea, for example, this is a div!42 | 43 |
44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /cheval.min.js: -------------------------------------------------------------------------------- 1 | // @license magnet:?xt=urn:btih:3877d6d54b3accd4bc32f8a48bf32ebc0901502a&dn=mpl-2.0.txt MPL-v2 2 | /*! cheval v2.0.0 by ryanpcmcquen */ 3 | !function(){"use strict";var afterCopyText_desktop="Copy again",afterCopyText_iPad="Now tap the text, then 'Copy'",afterCopyText_iPhoneOriPod="Now tap 'Copy'",afterCopyText_oldSafari="Press Command + C to copy",afterCopyText_notSupported="Please copy manually",sets={},regexBuilder=function(prefix){return new RegExp(prefix+"\\S*")};window.addEventListener("DOMContentLoaded",function(){var texts=Array.prototype.slice.call(document.querySelectorAll("[class*=text-to-copy]")),buttons=Array.prototype.slice.call(document.querySelectorAll("[class*=js-copy-btn]")),classNameFinder=function(arr,regex,namePrefix){return arr.map(function(item){return!!item.className.match(regex)&&item.className.match(regex)[0].replace(namePrefix,"")}).sort()};sets.texts=classNameFinder(texts,regexBuilder("text-to-copy"),"text-to-copy"),sets.buttons=classNameFinder(buttons,regexBuilder("js-copy-btn"),"js-copy-btn");var matches=sets.texts.map(function(ignore,index){return sets.texts[index].match(sets.buttons[index])}),throwErr=function(err){throw new Error(err)},iPhoneORiPod=!1,iPad=!1,oldSafari=!1,navAgent=window.navigator.userAgent;/^((?!chrome).)*safari/i.test(navAgent)&&!/^((?!chrome).)*[0-9][0-9](\.[0-9][0-9]?)?\ssafari/i.test(navAgent)&&(oldSafari=!0),navAgent.match(/iPhone|iPod/i)?iPhoneORiPod=!0:navAgent.match(/iPad/i)&&(iPad=!0);var cheval=function(btn,text){var copyBtn=document.querySelector(btn),setCopyBtnText=function(textToSet){copyBtn.textContent=textToSet};(iPhoneORiPod||iPad)&&oldSafari&&setCopyBtnText("Select text"),copyBtn?copyBtn.addEventListener("click",function(){var oldPosX=window.scrollX,oldPosY=window.scrollY,originalCopyItem=document.querySelector(text),dollyTheSheep=originalCopyItem.cloneNode(!0),copyItem=document.createElement("textarea");copyItem.style.opacity=0,copyItem.style.position="absolute";var copyValue=dollyTheSheep.value||dollyTheSheep.textContent;if(copyItem.value=copyValue,document.body.appendChild(copyItem),copyItem){copyItem.focus(),copyItem.selectionStart=0,copyItem.selectionEnd=copyValue.length;try{document.execCommand("copy"),copyItem.setAttribute("disabled",!0),oldSafari?setCopyBtnText(iPhoneORiPod?afterCopyText_iPhoneOriPod:iPad?afterCopyText_iPad:afterCopyText_oldSafari):(document.activeElement.blur(),setCopyBtnText(afterCopyText_desktop))}catch(ignore){setCopyBtnText(afterCopyText_notSupported)}originalCopyItem.focus(),window.scrollTo(oldPosX,oldPosY),originalCopyItem.selectionStart=0,originalCopyItem.selectionEnd=copyValue.length,copyItem.remove()}else throwErr("You don't have an element with the class: 'text-to-copy'. Please check the cheval README.")}):throwErr("You don't have a