Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Ebookjapan hacker
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description try to take over the world!
- // @author You
- // @match https://ebookjapan.yahoo.co.jp/bviewer/purchased/*
- // @match https://ebookjapan.yahoo.co.jp/bviewer/trial/*
- // @grant none
- // ==/UserScript==
- (function() {
- const showTips = {
- info (msg) {
- console.log('%c' + msg, 'color:blue');
- },
- success (msg) {
- console.log('%c' + msg, 'color:green');
- },
- error (msg) {
- console.log('%c' + msg, 'color:red');
- }
- }
- const delayExec = function(fn, delay) {
- let timer = null
- let context
- return function(...args) {
- context = this
- if (timer) {
- clearTimeout(timer)
- }
- timer = setTimeout(() => {
- fn.apply(context, args)
- timer = null
- }, delay)
- }
- }
- const rename = num => num > 99 ? num : num > 9 ? '0' + num : '00' + num;
- const downloadPage = (resolve, index) => {
- return function(blob) {
- const a = document.createElement('a');
- a.download = rename(index);
- a.href = window.URL.createObjectURL(blob);
- a.click();
- window.URL.revokeObjectURL(a.href);
- resolve();
- }
- }
- let toBlob; // 取回canvas元素转换为二进制文件的API
- let getImageData;
- let viewerCanvas;
- {
- const iframe = document.createElement('iframe');
- iframe.style.display = "none";
- iframe.onload = e => {
- toBlob = iframe.contentWindow.HTMLCanvasElement.prototype.toBlob;
- getImageData = iframe.contentWindow.CanvasRenderingContext2D.prototype.getImageData;
- window.HTMLCanvasElement.prototype.toBlob = toBlob;
- window.CanvasRenderingContext2D.prototype.getImageData = getImageData;
- showTips.success('iframe load success')
- }
- document.body.appendChild(iframe);
- }
- const getCanvas = function(whiteBorder = false, index) {
- console.log(index)
- const newCanvas = document.createElement('canvas')
- newCanvas.width = viewerCanvas.width - (whiteBorder ? 1 : 0)
- newCanvas.height = viewerCanvas.height
- const capture = getImageData.apply(viewerCanvas.getContext('2d'), [
- whiteBorder ? (index % 2 === 0 ? 1 : 0) : 0,
- 0,
- viewerCanvas.width - (whiteBorder ? 1 : 0),
- viewerCanvas.height
- ]);
- newCanvas.getContext('2d').putImageData(capture, 0, 0);
- return newCanvas;
- }
- const drawed = delayExec(async function(whiteBorder, pageIndex) {
- const mimeType = 'image/png';
- const canvas = getCanvas(whiteBorder, pageIndex)
- toBlob.apply(canvas, [downloadPage(() => {}, pageIndex), mimeType]);
- }, 1500)
- window.startDownload = function (whiteBorder, pageIndex = 0) {
- const __drawImage = window.CanvasRenderingContext2D.prototype.drawImage;
- // 获取阅读器的canvas元素
- viewerCanvas = document.querySelector('.viewer > canvas');
- window.CanvasRenderingContext2D.prototype.drawImage = function(...args) {
- if (args.length !== 5) {
- return __drawImage.apply(this, args);
- }
- __drawImage.apply(this, args);
- drawed(whiteBorder, pageIndex++);
- }
- drawed(whiteBorder, pageIndex++);
- }
- })();
- // ==========说明==========
- // 代码放到油猴去执行。
- // 在调整到单页模式后(缩小浏览器窗口就能出现),每次在阅读器页面的第一页,
- // 直接运行 window.startDownload() 就行了,然后缓慢手动翻页
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement