JavaScript常用工具函数( 二 )

  1. base64转blob
/** * @desc base64转blob * @param {String} data base64 * @return {Blob} blob */function dataURLtoBlob(data) {const dataAtob = atob(data)let dataLength = dataAtob.lengthconst u8arr = new Uint8Array(dataLength)while (dataLength--) {u8arr[dataLength] = dataAtob.charCodeAt(dataLength)}return new Blob([u8arr])}
  1. base64数据导出文件 。文件下载
function downloadFile(filename, data) {let DownloadLink = document.createElement('a')if (DownloadLink) {document.body.appendChild(DownloadLink)DownloadLink.style = 'display: none'DownloadLink.download = filenameDownloadLink.href = https://www.huyubaike.com/biancheng/dataif (document.createEvent) {let DownloadEvt = document.createEvent('MouseEvents')DownloadEvt.initEvent('click', true, false)DownloadLink.dispatchEvent(DownloadEvt)} else if (document.createEventObject) DownloadLink.fireEvent('onclick')else if (typeof DownloadLink.onclick == 'function') DownloadLink.onclick()document.body.removeChild(DownloadLink)}}
  1. 下载文件
/** * @desc 下载文件 * @param {String} data 二进制流文件 * @param {String} fileName 文件名 * @param {boolean} transformBlob 是否是Blob,默认不传无需转换 */export function download(data, fileName, transformBlob) {const blob = !transformBlob ? data : dataURLtoBlob(data)const blobUrl = window.URL.createObjectURL(blob)// desc:兼容IE浏览器blob文件下载if (navigator.msSaveOrOpenBlob) {navigator.msSaveOrOpenBlob(blob, fileName)} else {const a = document.createElement('a')a.style.display = 'none'a.href = https://www.huyubaike.com/biancheng/blobUrla.download = fileNamedocument.body.appendChild(a)a.click()document.body.removeChild(a)window.URL.revokeObjectURL(blobUrl)}}
  1. 页面全屏
function toFullScreen() {let el = document.documentElementlet rfs =el.requestFullScreen ||el.webkitRequestFullScreen ||el.mozRequestFullScreen ||el.msRequestFullScreen//typeof rfs != "undefined" && rfsif (rfs) {rfs.call(el)} else if (typeof window.ActiveXObject !== 'undefined') {//for IE,这里其实就是模拟了按下键盘的F11,使浏览器全屏let wscript = new ActiveXObject('WScript.Shell')if (wscript != null) {wscript.SendKeys('{F11}')}} else {alert('浏览器不支持全屏')}}
  1. 退出全屏
function exitFullscreen() {let el = parent.documentlet cfs =el.cancelFullScreen ||el.webkitCancelFullScreen ||el.mozCancelFullScreen ||el.exitFullScreen//typeof cfs != "undefined" && cfsif (cfs) {cfs.call(el)} else if (typeof window.ActiveXObject !== 'undefined') {//for IE,这里和fullScreen相同,模拟按下F11键退出全屏let wscript = new ActiveXObject('WScript.Shell')if (wscript != null) {wscript.SendKeys('{F11}')}} else {alert('切换失败,可尝试Esc退出')}}
  1. 利用performance.timing进行性能分析
window.onload = function () {setTimeout(function () {let t = performance.timingconsole.log('DNS查询耗时 :' + (t.domainLookupEnd - t.domainLookupStart).toFixed(0))console.log('TCP链接耗时 :' + (t.connectEnd - t.connectStart).toFixed(0))console.log('request请求耗时 :' + (t.responseEnd - t.responseStart).toFixed(0))console.log('解析dom树耗时 :' + (t.domComplete - t.domInteractive).toFixed(0))console.log('白屏时间 :' + (t.responseStart - t.navigationStart).toFixed(0))console.log('domready时间 :' +(t.domContentLoadedEventEnd - t.navigationStart).toFixed(0))console.log('onload时间 :' + (t.loadEventEnd - t.navigationStart).toFixed(0))if ((t = performance.memory)) {console.log('js内存使用占比 :' +((t.usedJSHeapSize / t.totalJSHeapSize) * 100).toFixed(2) +'%')}})}
  1. 禁止某些键盘事件
document.addEventListener('keydown', function (event) {return (!((112 == event.code || //F1123 == event.code || //F12(event.ctrlKey && 82 == event.code) || //ctrl + R(event.ctrlKey && 78 == event.code) || //ctrl + N(event.shiftKey && 121 == event.code) || //shift + F10(event.altKey && 115 == event.code) || //alt + F4('A' == event.srcElement.tagName && event.shiftKey)) //shift + 点击a标签) || (event.returnValue = https://www.huyubaike.com/biancheng/false))})
  1. 禁止右键、选择、赋值
['contextmenu', 'selectstart', 'copy'].forEach(function (ev) {document.addEventListener(ev, function (event) {return (event.returnValue = https://www.huyubaike.com/biancheng/false)})})常用js工具函数 , 长期维护更新

推荐阅读