// ==UserScript==
// @name 快看漫画一键保存
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 单击保存即可下载漫画
// @author You
// @icon https://www.google.com/s2/favicons?sz=64&domain=kuaikanmanhua.com
// @grant none
// @license MIT
// @include https://www.kuaikanmanhua.com/web/comic/*
// @downloadURL none
// ==/UserScript==
(function () {
'use strict';
async function AsyncErgodic(
data,
callBack
) {
const ps = new Set()
if ('forEach' in data) {
data.forEach((a, b, c) => {
ps.add(callBack(a, b, c))
})
} else {
for (const k in data) {
ps.add(callBack(data[k], k, data))
}
}
return await Promise.all(ps)
}
function run() {
document.body.insertAdjacentHTML('afterbegin', ``)
// 顶部下载
document.querySelector('.titleBox>.tab>div').insertAdjacentHTML('beforebegin', `
`)
// 下载进度条
document.querySelector('.bodyContent').insertAdjacentHTML('afterbegin', `
`)
const fc_sop = document.querySelector('.fc_sop')
/**@type {HTMLDivElement} */
const fc_sop_line_l = document.querySelector('.fc_sop_line_l')
const fc_sop_cancel = document.querySelector('.fc_sop_cancel')
function cancel() {
fc_sop.classList.remove('fc_show')
fc_sop_line_l.style.width = '0%'
}
// 防重锁
let lock = false
async function download() {
if(lock) return
lock = true
fc_sop.classList.add('fc_show')
const imgEles = [...document.querySelectorAll('.imgList .img-box .img[data-src]')]
const imgUrls = imgEles.map(e => e.getAttribute('data-src'))
fc_sop_cancel.onclick = function () {
a = null
cancel()
}
let a = document.createElement('a')
/** @type {HTMLImageElement[]} */
const imgObjs = await AsyncErgodic(imgUrls, (e, i) => new Promise(n => {
const el = document.createElement('img')
el.onload = () => {
const b = (i + 1) / imgUrls.length * 100
fc_sop_line_l.style.width = b + '%'
n(el)
}
el.src = e
el.setAttribute("crossOrigin", 'Anonymous')
}))
if (!imgObjs.length) {
console.log('异常:图片列表为空')
}
const width = imgObjs[0].width
const height = imgObjs.reduce((v, e) => v + e.height, 0)
const cvs = document.createElement('canvas')
cvs.width = width
cvs.height = height
const ctx = cvs.getContext('2d')
let th = 0
imgObjs.forEach(e => {
ctx.drawImage(e, 0, th)
th += e.height
})
if (!a) return
a.href = cvs.toDataURL('image/png')
a.download = document.title.replace('漫画全集在线观看-快看', '') + '.png'
a.click()
cancel()
lock = false
}
document.querySelectorAll('.fc_dl').forEach(e => e.addEventListener('click', download))
}
setTimeout(run, 100)
})();