@acrool/js-utils
Version:
Common javascript utils methods for project development
2 lines (1 loc) • 1.96 kB
JavaScript
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function c(e,o){return!!e.className.match(new RegExp(`(\\s|^)${o}(\\s|$)`))}function d(e,o){c(e,o)||e.classList.add(o)}function a(e,o){e.classList.remove(o)}function s(e,o,t){if(document.getElementById(e)===null){const n=document.createElement("iframe");n.id=e,n.src=o,n.scrolling="no",n.frameborder="0",n.width=0,n.height=0,n.onload=()=>{t&&t(n)},document.body.appendChild(n)}}function m(e,o){if(document.getElementById(e)===null){const t=document.createElement("script");t.id=e,t.append(o),document.head.appendChild(t)}}function p(e,o,t){if(document.getElementById(e)===null){const n=document.createElement("script");n.id=e,n.src=o,t&&(n.onload=t),document.head.appendChild(n)}return!1}const u=async(e,o)=>{if(navigator.clipboard&&window.ClipboardItem)try{const t=new Blob([e],{type:"text/plain"}),n=new ClipboardItem({"text/plain":t});await navigator.clipboard.write([n]),console.log("Text copied using Clipboard API");return}catch{console.warn("Clipboard API failed, falling back to execCommand")}return new Promise((t,n)=>{const i=document.createElement("textarea");i.style.position="fixed",i.style.opacity="0",i.inputMode="none",o?i.value=e:i.innerText=e,document.body.appendChild(i),i.select();try{document.execCommand("copy")?(console.log("Text copied using execCommand"),t()):n(new Error("execCommand failed"))}catch(r){n(r)}finally{i.remove()}})},b=(e,o)=>{l(window.URL.createObjectURL(e),o)},l=(e,o)=>{const t=document.createElement("a");t.href=e,t.download=o,t.click()},f=e=>{const t=e.getBoundingClientRect().top;return window.innerHeight-t>t?"bottom":"top"},C=()=>{document.activeElement.blur()};exports.activeElementBlur=C;exports.addClass=d;exports.copyToClipboard=u;exports.downloadBlob=b;exports.downloadUrl=l;exports.getVisiblePosition=f;exports.hasClass=c;exports.insertIFrame=s;exports.insertScriptContent=m;exports.insertScriptSrc=p;exports.removeClass=a;
;