UNPKG

mu-tooljs

Version:

Web前端开发,常用JavaScript工具库

1 lines 3.5 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.mu=t():e.mu=t()}(self,(function(){return function(){"use strict";var e={};return function(){var t=e;Object.defineProperty(t,"__esModule",{value:!0}),t.blobToBase64=t.base64ToBlob=t.imgURLToBase64=t.downloadByData=t.download2=t.download=t.cookie=t.print=void 0;const o=e=>{try{return-1!=e.indexOf("#")?document.querySelector(e):document.getElementById(e)}catch(e){return console.error("对不起:请检查传入元素的ID值是否存在!"),!1}};t.print=(e="")=>{const t=e&&o(e)?o(e):document.body;if(t){const e=document.createElement("IFRAME");e.hidden=!0,document.body.appendChild(e);const o=e.contentWindow,n=o.document;n.open(),n.write(t.innerHTML),n.close(),setTimeout((()=>{o.focus(),o.print(),document.body.removeChild(e)}),300)}else console.error("对不起:请先传入要打印的区域元素的ID值!")},t.cookie={get:function(e){const t=e+"=",o=document.cookie.split(";");for(let e=0;e<o.length;e++){let n=o[e].trim();if(0==n.indexOf(t))return n.substring(t.length,n.length)}return this},set(e,t,o=0,n="",r=""){const d=o?`expires=${new Date(+new Date+60*(24*o+8)*60*1e3).toUTCString()};`:"",i=n?`path=${n};`:"",c=r?`domain=${r};`:"";return document.cookie=`${e}=${t};${d}${i}${c}`},del:function(e){return this.set(e,"",-1)}},t.download=function(e,t,o="_blank"){const n=window.navigator.userAgent.toLowerCase().indexOf("chrome")>-1,r=window.navigator.userAgent.toLowerCase().indexOf("safari")>-1;if(/(iP)/g.test(window.navigator.userAgent))return console.error("Your browser does not support download!"),!1;if(n||r){const n=document.createElement("a");if(n.href=e,n.target=o,void 0!==n.download&&(n.download=t||e.substring(e.lastIndexOf("/")+1,e.length)),document.createEvent){const e=document.createEvent("MouseEvents");return e.initEvent("click",!0,!0),n.dispatchEvent(e),!0}}return-1===e.indexOf("?")&&(e+="?download"),function(e,t){const{target:o="__blank",noopener:n=!0,noreferrer:r=!0}=t||{},d=[];n&&d.push("noopener=yes"),r&&d.push("noreferrer=yes"),window.open(e,o,d.join(","))}(e,{target:o}),!0},t.download2=(e,t)=>{if(e){const o=document.createElement("A");return o.href=e,o.download=t||Date.now(),document.body.append(o),o.click(),document.body.removeChild(o),!0}return console.error("对不起:请先传入要下载的文件地址!"),!1},t.downloadByData=function(e,t,o,n){const r=new Blob(void 0!==n?[n,e]:[e],{type:o||"application/octet-stream"}),d=window.URL.createObjectURL(r),i=document.createElement("a");i.style.display="none",i.href=d,i.setAttribute("download",t),void 0===i.download&&i.setAttribute("target","_blank"),document.body.appendChild(i),i.click(),document.body.removeChild(i),window.URL.revokeObjectURL(d)},t.imgURLToBase64=function(e,t){return new Promise(((o,n)=>{let r=document.createElement("CANVAS");const d=r.getContext("2d"),i=new Image;i.crossOrigin="",i.onload=function(){if(!r||!d)return n();r.height=i.height,r.width=i.width,d.drawImage(i,0,0);const e=r.toDataURL(t||"image/png");r=null,o(e)},i.src=e}))},t.base64ToBlob=function(e){const t=e.split(","),o=t[0].match(/:(.*?);/)[1],n=window.atob(t[1]);let r=n.length;const d=new Uint8Array(r);for(;r--;)d[r]=n.charCodeAt(r);return new Blob([d],{type:o})},t.blobToBase64=function(e,t){const o=new FileReader;o.onload=function(e){var o;t(null===(o=e.target)||void 0===o?void 0:o.result)},o.readAsDataURL(e)}}(),e}()}));