// ==UserScript== // @name 云展网下载PDF // @namespace http://tampermonkey.net/yunzhanpDF // @version 1.1 // @description 云展网下载高清PDF! // @author ZouYS // @match https://*.yunzhan365.com/* // @icon https://book.yunzhan365.com/web/images/nav_logo_big.jpg // @require https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js // @require https://cdn.jsdelivr.net/npm/sweetalert2@11 // @require https://fastly.jsdelivr.net/npm/sweetalert2@11 // @grant GM_addStyle // @grant GM_addElement // @grant unsafeWindow // @run-at document-end // @downloadURL none // ==/UserScript== (function() { 'use strict'; window.onload = function() { /*if(!window.htmlConfig){ console.error('error!') return 1; }*/ let css = `.mydiv1{ position: absolute; z-index: 9999999999; left: 100px; top: 300px; display: none; } .mybtn1 { z-index: 9999999999; position: absolute; top: 300px; font-size: inherit; font-family: inherit; color: white; padding: 0.5em 1em; outline: none; border: none; background-color: hsl(236, 32%, 26%); overflow: hidden; transition: color 0.4s ease-in-out; } .mybtn2 { z-index: 9999999999; position: absolute; top: 350px; font-size: inherit; font-family: inherit; color: white; padding: 0.5em 1em; outline: none; border: none; background-color: hsl(236, 32%, 26%); overflow: hidden; transition: color 0.4s ease-in-out; } .mybtn2::before,.mybtn1::before { content: ''; z-index: -1; position: absolute; top: 100%; right: 100%; width: 1em; height: 1em; border-radius: 50%; background-color: #3cefff; transform-origin: center; transform: translate3d(50%, -50%, 0) scale3d(0, 0, 0); transition: transform 0.45s ease-in-out; } .mybtn2:hover,.mybtn1:hover { cursor: pointer; color: #161616; } .mybtn2:hover::before,.mybtn1:hover::before { transform: translate3d(50%, -50%, 0) scale3d(15, 15, 15); }`; GM_addStyle(css); const button = document.createElement('button') button.classList.add('mybtn1') button.innerText = '高清下载PDF' document.body.append(button) button.onclick = ()=>{ // console.log(unsafeWindow) let newUrl=document.getElementsByTagName('iframe')[0] && document.getElementsByTagName('iframe')[0].src if(newUrl){ unsafeWindow.location.href=newUrl } if (Swal) { Swal.fire({ position: 'top-end', //定位 左上角 type: 'success', title: '↑↑↑下载中,请留意浏览器右上角↑↑↑弹窗~', showConfirmButton: false, timer: 1500 }) } /*if(!unsafeWindow.htmlConfig.fliphtml5_pages){ GM_addElement('script', { src: douc, type: 'text/javascript' }); }*/ let meta=unsafeWindow.htmlConfig || console.error('no meta data!') let imgUrls= meta.fliphtml5_pages let basicUrl=meta.meta.url basicUrl=basicUrl.slice(0,basicUrl.lastIndexOf('/')+1) addImageToPDF(meta,imgUrls,basicUrl).then(pdf => { pdf.save(`${meta.meta.title}.pdf`); }); } async function addImageToPDF(meta,imageUrls,basicUrl) { let pdf; for (let i = 0; i < imageUrls.length; i++) { try { let url=imageUrls[i].n[0].split('?')[0] url+=`?x-oss-process=image/sharpen,100` if(url.includes('files/large/')){ url=url.replace('../','') }else { url='files/large/'+url } url=basicUrl+url; console.log(`第${i+1}页 url:`,url) const img = await loadImage(url); const imgWidth = img.width; const imgHeight = img.height; if(!pdf){ pdf=new jspdf.jsPDF({ orientation: imgWidth/imgHeight>1?'l':'p', unit: 'px', format: [imgWidth, imgHeight], compress: true }); } if(i!==0){ pdf.addPage({ format: [imgWidth, imgHeight], }); } /*const pageWidth = pdf.internal.pageSize.width; const pageHeight = pdf.internal.pageSize.height; // 计算缩放比例 const ratio = Math.min(pageWidth / imgWidth, pageHeight / imgHeight); const newWidth = imgWidth * ratio; const newHeight = imgHeight * ratio;*/ // 计算居中位置 const x = 0; const y = 0; // 确保坐标和尺寸有效 pdf.addImage(img, 'WEBP', x, y, imgWidth, imgHeight); /*if (newWidth > 0 && newHeight > 0 && x >= 0 && y >= 0) { pdf.addImage(img, 'WEBP', x, y, newWidth, newHeight); } else { console.error('Invalid dimensions or coordinates:', { newWidth, newHeight, x, y }); }*/ /*if(i!==imageUrls.length-1){ pdf.addPage() }*/ if (Swal && i>10) { Swal.fire({ position: 'top-end', //定位 左上角 icon: 'success', title: `加载中,第${i+1}页`, showConfirmButton: false, }) } }catch (e) { if (Swal) { Swal.fire({ icon: 'error', title: `下载失败,${e.message}`, showConfirmButton: true, }) } } } return pdf; } function loadImage(url) { return new Promise((resolve, reject) => { const img = new Image(); img.crossOrigin = 'anonymous'; // 处理跨域问题 img.src = url; img.onload = () => { /*const canvas = document.createElement('canvas'); canvas.width = img.width; canvas.height = img.height; const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0);*/ // resolve(canvas.toDataURL('image/webp')); resolve(img); }; img.onerror = reject; }); } } // Your code here... })();