-
Notifications
You must be signed in to change notification settings - Fork 14
/
cheval.min.js
4 lines (4 loc) · 2.95 KB
/
cheval.min.js
1
2
3
4
// @license magnet:?xt=urn:btih:3877d6d54b3accd4bc32f8a48bf32ebc0901502a&dn=mpl-2.0.txt MPL-v2
/*! cheval v2.0.0 by ryanpcmcquen */
!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 <button> with the class: 'js-copy-btn'. Please check the cheval README.")};matches.forEach(function(i){return cheval(".js-copy-btn"+i,".text-to-copy"+i)});try{window.cheval=cheval,module.exports=cheval}catch(ignore){}})}();
// @license-end