function getImages(target) { var photoSwipeBox = '
' var photoSwipeBoxDiv = document.createElement('div'); photoSwipeBoxDiv.innerHTML = photoSwipeBox; document.body.appendChild(photoSwipeBoxDiv); var imgs = []; var currentIndex = 0 var targetEle = document.querySelector(target); if(!targetEle) { var iframeList = document.querySelectorAll('iframe') for(var i =0; i < iframeList.length; i++) { if(iframeList[i].contentWindow.document.querySelector(target)) { targetEle = iframeList[i].contentWindow.document.querySelector(target); break; } } } var images = targetEle.querySelectorAll('img'); var imgIndex = 0; for(var index =0; index < images.length; index++) { var img = images[index]; var orisrc = img.getAttribute('orisrc') if(orisrc && (orisrc.indexOf("virtual_attach_file.vsb") > -1 || orisrc.indexOf("virtual_resource_file.jsp") > -1)) { imgs.push({ src: orisrc, w: img.naturalWidth, h: img.naturalHeight }); img.setAttribute('index',imgIndex); img.addEventListener('click', function (e) { currentIndex = this.getAttribute('index') * 1; previewImage(); }); imgIndex++; } } var gallery = null; function previewImage() { if(isIE()) { var imgURL = imgs[currentIndex].src window.open(imgURL,'_blank'); }else { var pswpElement = document.querySelectorAll('.pswp')[0]; // index 当前图片的下标 timeToIdle 鼠标悬浮多久后,隐藏界面上的按钮,设置为0,则不隐藏 gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, imgs, {index:currentIndex, timeToIdle: 0}); gallery.init() } } var downloadBtn = document.querySelector('.pswp__button--download') downloadBtn.addEventListener('click', function () { if(isWeChatWorkBrowser()) { window.alert("可在浏览器打开此网页来下载文件") }else { var imgsrc = gallery.currItem.src var fileSuffix = getQueryParam(imgsrc, 'e') || "" let filename = new Date().getTime() + fileSuffix if (navigator.msSaveBlob) { var xhr = new XMLHttpRequest(); xhr.open('GET', imgsrc, true); xhr.responseType = 'blob'; xhr.onload = function () { if (xhr.status === 200) { navigator.msSaveBlob(xhr.response, filename); } }; xhr.send(); } else { var a = document.createElement('a') a.href = imgsrc a.download = filename a.click() } } }) function getQueryParam(url, paramName) { var paramRegex = new RegExp('[?&]' + paramName + '=([^]*)'); var match = paramRegex.exec(url); return match ? decodeURIComponent(match[1]) : null; } function isIE() { var userAgent = navigator.userAgent; var isIE = userAgent.indexOf('MSIE') > -1 || userAgent.indexOf('Trident/') > -1; var is360 = userAgent.indexOf('360EE') > -1 || userAgent.indexOf('360SE') > -1; return isIE || is360; } // 企业微信提示:可在浏览器打开此网页来下载文件 function isWeChatWorkBrowser() { const userAgent = navigator.userAgent; return userAgent.indexOf("wxwork") > -1; } }