iwanami

edown

Apr 13th, 2021
1,966
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Ebookjapan hacker
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  try to take over the world!
  6. // @author       You
  7. // @match        https://ebookjapan.yahoo.co.jp/bviewer/purchased/*
  8. // @match        https://ebookjapan.yahoo.co.jp/bviewer/trial/*
  9. // @grant        none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13.   const showTips = {
  14.     info (msg) {
  15.       console.log('%c' + msg, 'color:blue');
  16.     },
  17.     success (msg) {
  18.       console.log('%c' + msg, 'color:green');
  19.     },
  20.     error (msg) {
  21.       console.log('%c' + msg, 'color:red');
  22.     }
  23.   }
  24.  
  25.   const delayExec = function(fn, delay) {
  26.     let timer = null
  27.     let context
  28.  
  29.     return function(...args) {
  30.       context = this
  31.       if (timer) {
  32.         clearTimeout(timer)
  33.       }
  34.  
  35.       timer = setTimeout(() => {
  36.         fn.apply(context, args)
  37.         timer = null
  38.       }, delay)
  39.     }
  40.   }
  41.  
  42.   const rename = num => num > 99 ? num : num > 9 ? '0' + num : '00' + num;
  43.  
  44.   const downloadPage = (resolve, index) => {
  45.     return function(blob) {
  46.       const a = document.createElement('a');
  47.       a.download = rename(index);
  48.       a.href = window.URL.createObjectURL(blob);
  49.       a.click();
  50.       window.URL.revokeObjectURL(a.href);
  51.       resolve();
  52.     }
  53.   }
  54.  
  55.   let toBlob; // 取回canvas元素转换为二进制文件的API
  56.   let getImageData;
  57.   let viewerCanvas;
  58.  
  59.   {
  60.     const iframe = document.createElement('iframe');
  61.     iframe.style.display = "none";
  62.  
  63.     iframe.onload = e => {
  64.       toBlob = iframe.contentWindow.HTMLCanvasElement.prototype.toBlob;
  65.       getImageData = iframe.contentWindow.CanvasRenderingContext2D.prototype.getImageData;
  66.  
  67.  
  68.       window.HTMLCanvasElement.prototype.toBlob = toBlob;
  69.       window.CanvasRenderingContext2D.prototype.getImageData = getImageData;
  70.  
  71.       showTips.success('iframe load success')
  72.     }
  73.  
  74.     document.body.appendChild(iframe);
  75.   }
  76.  
  77.   const getCanvas = function(whiteBorder = false, index) {
  78.     console.log(index)
  79.     const newCanvas = document.createElement('canvas')
  80.     newCanvas.width = viewerCanvas.width - (whiteBorder ? 1 : 0)
  81.     newCanvas.height = viewerCanvas.height
  82.  
  83.     const capture = getImageData.apply(viewerCanvas.getContext('2d'), [
  84.       whiteBorder ? (index % 2 === 0 ? 1 : 0) : 0,
  85.       0,
  86.       viewerCanvas.width - (whiteBorder ? 1 : 0),
  87.       viewerCanvas.height
  88.     ]);
  89.  
  90.     newCanvas.getContext('2d').putImageData(capture, 0, 0);
  91.  
  92.     return newCanvas;
  93.   }
  94.  
  95.   const drawed = delayExec(async function(whiteBorder, pageIndex) {
  96.     const mimeType = 'image/png';
  97.     const canvas = getCanvas(whiteBorder, pageIndex)
  98.     toBlob.apply(canvas, [downloadPage(() => {}, pageIndex), mimeType]);
  99.   }, 1500)
  100.  
  101.   window.startDownload = function (whiteBorder, pageIndex = 0) {
  102.     const __drawImage = window.CanvasRenderingContext2D.prototype.drawImage;
  103.  
  104.     // 获取阅读器的canvas元素
  105.     viewerCanvas = document.querySelector('.viewer > canvas');
  106.  
  107.     window.CanvasRenderingContext2D.prototype.drawImage = function(...args) {
  108.       if (args.length !== 5) {
  109.         return __drawImage.apply(this, args);
  110.       }
  111.  
  112.       __drawImage.apply(this, args);
  113.       drawed(whiteBorder, pageIndex++);
  114.     }
  115.  
  116.     drawed(whiteBorder, pageIndex++);
  117.   }
  118. })();
  119.  
  120. // ==========说明==========
  121. // 代码放到油猴去执行。
  122. // 在调整到单页模式后(缩小浏览器窗口就能出现),每次在阅读器页面的第一页,
  123. // 直接运行 window.startDownload() 就行了,然后缓慢手动翻页
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×