-
Notifications
You must be signed in to change notification settings - Fork 3
/
shogakukan.js
1 lines (1 loc) · 12.5 KB
/
shogakukan.js
1
(()=>{"use strict";function e(e=0){return new Promise((n=>setTimeout((()=>n()),e)))}function n(e){return document.querySelector(`#downloader-setting .downloader-helper-${e.replace(/\./g,".downloader-helper-")}`)}function t(e){return new Promise((n=>{const t=new XMLHttpRequest;t.open("GET",e),t.responseType="arraybuffer",t.onload=()=>n(t.response),t.send()}))}const o=[],r=CanvasRenderingContext2D.prototype.drawImage;function a(){return o}new Set;let i=0,s=0;function d(){s=0}function l(){s=0,i++}function c(){i--,i<0&&(i=0),0===i&&d&&d()}const u=new WeakSet,p=[];function h(e){return p.some((n=>n instanceof RegExp?n.test(e):e.startsWith(n)))}!function(){const e=XMLHttpRequest.prototype.send,n=XMLHttpRequest.prototype.open;XMLHttpRequest.prototype.open=function(e,t,...o){return h(t)&&u.add(this),n.call(this,e,t,...o)},XMLHttpRequest.prototype.send=function(...n){return u.has(this)||l(),this.addEventListener("readystatechange",(()=>{4===this.readyState&&(u.has(this)||c())})),e.call(this,...n)}}();const w=new Set;window.addEventListener("message",(e=>{const n=e.data;n&&("request-end"===n.type&&w.has(n.requestId)?(w.delete(n.requestId),c()):"request-start"===n.type&&(w.add(n.requestId),l()))}),!1);const g=window.fetch;window.fetch=async function(e,...n){const t="string"==typeof e?e:e instanceof Request?e.url:e.href,o=g.call(this,e,...n);h(t)||(l(),o.finally((()=>c())));const r=await o;return["blob","arrayBuffer","text","json"].forEach((e=>{const n=Response.prototype[e];r[e]=function(){const e=n.call(this);return l(),e.finally((()=>c())),e}})),r},HTMLCanvasElement.prototype.toDataURL||window.bk_toDataURL;var f=Uint8Array,v=Uint16Array,y=Int32Array,m=new f([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0,0]),b=new f([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,0,0]),x=(new f([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),function(e,n){for(var t=new v(31),o=0;o<31;++o)t[o]=n+=1<<e[o-1];var r=new y(t[30]);for(o=1;o<30;++o)for(var a=t[o];a<t[o+1];++a)r[a]=a-t[o]<<5|o;return{b:t,r}}),E=x(m,2),L=E.b,k=E.r;L[28]=258,k[258]=28;for(var C=x(b,0),q=(C.b,C.r,new v(32768)),R=0;R<32768;++R){var S=(43690&R)>>1|(21845&R)<<1;S=(61680&(S=(52428&S)>>2|(13107&S)<<2))>>4|(3855&S)<<4,q[R]=((65280&S)>>8|(255&S)<<8)>>1}var I=new f(288);for(R=0;R<144;++R)I[R]=8;for(R=144;R<256;++R)I[R]=9;for(R=256;R<280;++R)I[R]=7;for(R=280;R<288;++R)I[R]=8;var A=new f(32);for(R=0;R<32;++R)A[R]=5;var D=["unexpected EOF","invalid block type","invalid length/literal","invalid distance","stream finished","no stream handler",,"no callback","invalid UTF-8 data","extra field too long","date not in range 1980-2099","filename too long","stream finishing","invalid zip data"],T=function(e,n,t){var o=new Error(n||D[e]);if(o.code=e,Error.captureStackTrace&&Error.captureStackTrace(o,T),!t)throw o;return o},M=new f(0),H=function(){for(var e=new Int32Array(256),n=0;n<256;++n){for(var t=n,o=9;--o;)t=(1&t&&-306674912)^t>>>1;e[n]=t}return e}(),U=function(e,n,t){for(;t;++n)e[n]=t,t>>>=8},z="undefined"!=typeof TextEncoder&&new TextEncoder,P="undefined"!=typeof TextDecoder&&new TextDecoder;try{P.decode(M,{stream:!0})}catch(e){}function B(e,n){if(n){for(var t=new f(e.length),o=0;o<e.length;++o)t[o]=e.charCodeAt(o);return t}if(z)return z.encode(e);var r,a,i,s=e.length,d=new f(e.length+(e.length>>1)),l=0,c=function(e){d[l++]=e};for(o=0;o<s;++o){if(l+5>d.length){var u=new f(l+8+(s-o<<1));u.set(d),d=u}var p=e.charCodeAt(o);p<128||n?c(p):p<2048?(c(192|p>>6),c(128|63&p)):p>55295&&p<57344?(c(240|(p=65536+(1047552&p)|1023&e.charCodeAt(++o))>>18),c(128|p>>12&63),c(128|p>>6&63),c(128|63&p)):(c(224|p>>12),c(128|p>>6&63),c(128|63&p))}return r=d,(null==(a=0)||a<0)&&(a=0),(null==(i=l)||i>r.length)&&(i=r.length),new f(r.subarray(a,i))}var X=function(e){var n=0;if(e)for(var t in e){var o=e[t].length;o>65535&&T(9),n+=o+4}return n},$=function(e,n,t,o,r,a,i,s){var d=o.length,l=t.extra,c=s&&s.length,u=X(l);U(e,n,null!=i?33639248:67324752),n+=4,null!=i&&(e[n++]=20,e[n++]=t.os),e[n]=20,n+=2,e[n++]=t.flag<<1|(a<0&&8),e[n++]=r&&8,e[n++]=255&t.compression,e[n++]=t.compression>>8;var p=new Date(null==t.mtime?Date.now():t.mtime),h=p.getFullYear()-1980;if((h<0||h>119)&&T(10),U(e,n,h<<25|p.getMonth()+1<<21|p.getDate()<<16|p.getHours()<<11|p.getMinutes()<<5|p.getSeconds()>>1),n+=4,-1!=a&&(U(e,n,t.crc),U(e,n+4,a<0?-a-2:a),U(e,n+8,t.size)),U(e,n+12,d),U(e,n+14,u),n+=16,null!=i&&(U(e,n,c),U(e,n+6,t.attrs),U(e,n+10,i),n+=14),e.set(o,n),n+=d,u)for(var w in l){var g=l[w],f=g.length;U(e,n,+w),U(e,n+2,f),e.set(g,n+4),n+=4+f}return c&&(e.set(s,n),n+=c),n},_=function(){function e(e){var n;this.filename=e,this.c=(n=-1,{p:function(e){for(var t=n,o=0;o<e.length;++o)t=H[255&t^e[o]]^t>>>8;n=t},d:function(){return~n}}),this.size=0,this.compression=0}return e.prototype.process=function(e,n){this.ondata(null,e,n)},e.prototype.push=function(e,n){this.ondata||T(5),this.c.p(e),this.size+=e.length,n&&(this.crc=this.c.d()),this.process(e,n||!1)},e}(),W=function(){function e(e){this.ondata=e,this.u=[],this.d=1}return e.prototype.add=function(e){var n=this;if(this.ondata||T(5),2&this.d)this.ondata(T(4+8*(1&this.d),0,1),null,!1);else{var t=B(e.filename),o=t.length,r=e.comment,a=r&&B(r),i=o!=e.filename.length||a&&r.length!=a.length,s=o+X(e.extra)+30;o>65535&&this.ondata(T(11,0,1),null,!1);var d=new f(s);$(d,0,e,t,i,-1);var l=[d],c=function(){for(var e=0,t=l;e<t.length;e++){var o=t[e];n.ondata(null,o,!1)}l=[]},u=this.d;this.d=0;var p=this.u.length,h=function(e,n){var t={};for(var o in e)t[o]=e[o];for(var o in n)t[o]=n[o];return t}(e,{f:t,u:i,o:a,t:function(){e.terminate&&e.terminate()},r:function(){if(c(),u){var e=n.u[p+1];e?e.r():n.d=1}u=1}}),w=0;e.ondata=function(t,o,r){if(t)n.ondata(t,o,r),n.terminate();else if(w+=o.length,l.push(o),r){var a=new f(16);U(a,0,134695760),U(a,4,e.crc),U(a,8,w),U(a,12,e.size),l.push(a),h.c=w,h.b=s+w+16,h.crc=e.crc,h.size=e.size,u&&h.r(),u=1}else u&&c()},this.u.push(h)}},e.prototype.end=function(){var e=this;2&this.d?this.ondata(T(4+8*(1&this.d),0,1),null,!0):(this.d?this.e():this.u.push({r:function(){1&e.d&&(e.u.splice(-1,1),e.e())},t:function(){}}),this.d=3)},e.prototype.e=function(){for(var e=0,n=0,t=0,o=0,r=this.u;o<r.length;o++)t+=46+(d=r[o]).f.length+X(d.extra)+(d.o?d.o.length:0);for(var a=new f(t+22),i=0,s=this.u;i<s.length;i++){var d=s[i];$(a,e,d,d.f,d.u,-d.c-2,n,d.o),e+=46+d.f.length+X(d.extra)+(d.o?d.o.length:0),n+=d.b}var l,c,u,p,h;l=a,c=e,u=this.u.length,p=t,h=n,U(l,c,101010256),U(l,c+8,u),U(l,c+10,u),U(l,c+12,p),U(l,c+16,h),this.ondata(null,a,!0),this.d=2},e.prototype.terminate=function(){for(var e=0,n=this.u;e<n.length;e++)n[e].t();this.d=2},e}();function j(e,n){e.addEventListener("mousedown",(e=>e.stopPropagation())),e.addEventListener("click",(t=>{t.stopPropagation(),e.classList.contains("downloader-helper-disabled")||n()}))}"function"==typeof queueMicrotask?queueMicrotask:"function"==typeof setTimeout&&setTimeout,window.Image=class extends window.Image{constructor(){super(),this.crossOrigin="Anonymous"}},window.Image=class extends window.Image{constructor(){super(),l();const e=()=>c();this.addEventListener("error",e,!1),this.addEventListener("abort",e,!1),this.addEventListener("load",e,!1)}},async function(o,r){!function(){if(location.hash.includes("debugCanvas")){const e=CanvasRenderingContext2D.prototype.drawImage;CanvasRenderingContext2D.prototype.drawImage=function(n,...t){return console.log("drawImage",this,n,t),e.call(this,n,...t)}}}(),await new Promise((e=>{"complete"===document.readyState||"interactive"===document.readyState?setTimeout((()=>e()),0):document.addEventListener("DOMContentLoaded",(()=>e()))}));const d=document.createElement("div");d.innerHTML='<div id="downloader-setting">\n <style>\n #downloader-setting {\n position: fixed;\n z-index: 99999;\n top: 0;\n right: 0;\n padding: 16px;\n margin: 12px;\n background: white;\n border-radius: 8px;\n box-shadow: 0 0 5px lightgray;\n color: black;\n }\n #downloader-setting img {\n width: 220px !important;\n }\n #downloader-setting div {\n padding: 6px 0;\n }\n #downloader-setting .downloader-helper-button,\n #downloader-setting .downloader-helper-checkbox {\n cursor: pointer;\n padding: 10px 15px;\n display: inline-block;\n }\n #downloader-setting .downloader-helper-button {\n border: 1px solid lightgray;\n }\n #downloader-setting .downloader-helper-button.downloader-helper-disabled {\n cursor: default;\n background-color: #E7E7E7;\n color: #BBB;\n }\n #downloader-setting .downloader-helper-checkbox > .downloader-helper-box {\n border: 1px solid grey;\n display: inline-block;\n width: 20px;\n height: 20px;\n margin-right: 8px;\n vertical-align: middle;\n }\n </style>\n <div>Captured images: <span class="downloader-helper-captureNum"></span></div>\n <div>Preview:<br /><img class="downloader-helper-preview" /></div>\n <div class="downloader-helper-buttons">\n <div class="downloader-helper-save-container">\n <div>\n <div class="downloader-helper-button downloader-helper-auto">Auto Download All</div>\n </div>\n <div>\n <div class="downloader-helper-button downloader-helper-auto-limit">Auto With Pages Limit</div>\n </div>\n </div>\n <div class="downloader-helper-l2r-container" style="display: none;">\n <div class="downloader-helper-checkbox downloader-helper-l2r">\n <div class="downloader-helper-box"></div>\n Left to Right\n </div>\n </div>\n <div>\n <div class="downloader-helper-button downloader-helper-download">Save</div>\n </div>\n </div>\n <div class="downloader-helper-auto-working" style="display: none">Automatically Downloading...</div>\n <div class="downloader-helper-progress" style="display:none">\n Compress: <span class="downloader-helper-current"></span> / <span class="downloader-helper-total"></span>\n </div>\n</div>\n',document.body.prepend(d);let l=null;const c=[0,0];async function u(t=1/0){n("buttons").style.display="none",n("auto-working").style.display="block",await async function(n,t){for(;;){if(s>=12||a().length>=t)return;if(await e(1e3),0===i)try{await n()&&(s=0),s++}catch(e){}}}(o,t),n("save-container").style.display="none",n("l2r-container").style.display="none",n("buttons").style.display="block",n("button.download").classList.remove("downloader-helper-disabled"),n("auto-working").style.display="none",document.body.style.pointerEvents=""}document.getElementById("downloader-setting").addEventListener("mousedown",(e=>{l=[e.pageX,e.pageY]})),window.addEventListener("mouseup",(()=>{l=null})),window.addEventListener("mousemove",(e=>{if(l){const n=[e.pageX,e.pageY];c[0]+=n[0]-l[0],c[1]+=n[1]-l[1],document.getElementById("downloader-setting").style.transform=`translate(${c[0]}px, ${c[1]}px)`,l=n}})),j(n("auto"),(()=>u())),j(n("auto-limit"),(()=>{const e=window.prompt("How many pages?","10");null!==e&&u(parseInt(e,10))}));const p=n("l2r");j(p,(()=>{const e=p.querySelector(".box");e.style.background=e.style.background?"":"darkblue"})),j(n("download"),(async()=>{n("button.download").classList.add("downloader-helper-disabled"),n("progress").style.display="inline-block",n("progress .total").textContent=a().length.toString(),n("progress .current").textContent="0";const e=await async function(e,o){return new Promise((o=>{const r=[],a=new W(((e,n,t)=>{if(!e&&(r.push(n),t)){const e=window.URL.createObjectURL(new Blob(r));o(e)}})),i=`${e.length}`.length;(async()=>{for(let r=0;r<e.length;++r){const s=new _(`${(r+1).toString().padStart(i,"0")}.jpg`);a.add(s),s.push(new Uint8Array(await t(e[r])),!0),o=`${r+1}`,n("progress .current").textContent=o}var o;a.end()})()}))}(a()),o=document.createElement("a");o.href=e,o.download=document.title+".zip",o.click(),n("progress").style.display="none",r&&r(),n("button.download").classList.remove("downloader-helper-disabled")}))}((async()=>{const e=document.querySelectorAll(".c-viewer__page"),t=Array.from(e).find((e=>{const{left:n,right:t}=e.parentElement?.getBoundingClientRect()||{left:-1,right:-1};return n>=0&&t<=window.innerWidth}));if(!t)return;if(t.querySelector(".c-viewer__last"))return;const a=t.querySelector("canvas");a&&async function(e){if(e.startsWith("data:image/png")){const n=await new Promise((n=>{const t=new Image;t.onload=()=>n(t),t.src=e})),t=document.createElement("canvas");t.width=n.naturalWidth,t.height=n.naturalHeight;const o=t.getContext("2d");r.call(o,n,0,0),e=t.toDataURL("image/jpeg",1)}for(let n=0;n<3;++n)if(o[o.length-1-n]===e)return!1;d(),o.push(e),n("captureNum").textContent=`${o.length}`,n("preview").src=e}(a.toDataURL("image/jpeg",1));const i=document.querySelector(".c-viewer__pager-next");i?.click()}))})();