window.onload = () => {
insertJQuery('https://stuk.github.io/jszip/vendor/FileSaver.js')
insertJQuery('https://stuk.github.io/jszip/dist/jszip.js')
insertJQuery('https://stuk.github.io/jszip-utils/dist/jszip-utils.js')
const props = getData()
setStyle()
getProductImage(props)
getProductInfoPage(props)
}
function insertJQuery(url) {
let script = document.createElement('script');
script.src = url;
document.head.appendChild(script);
return script;
}
function getData() {
const props = {}
const params = location.search.substr(1).split('&')
for (let i in params) {
props[params[i].split('=')[0]] = unescape(params[i].split('=')[1])
}
return props
}
function getPicture(url) {
return new Promise((resolve, reject) => {
fetch(url, {
method: 'get',
responseType: 'arraybuffer'
}).then(data => {
return data.blob()
}).then(data => {
resolve(data)
}).catch(error => {
reject(error)
})
})
}
function download(images, folderName) {
const zip = new JSZip()
const folder = zip.folder(folderName)
const promises = images.map(image => {
return getPicture(image).then(data => {
const name = image.split('/')[image.split('/').length - 1]
folder.file(name, data, { binary: true })
})
})
Promise.all(promises).then(() => {
zip.generateAsync({ type: 'blob' }).then(content => {
saveAs(content, folderName + '.zip')
})
})
}
function getProductImage(props) {
let imgList = document.getElementById('J_UlThumb')
const downBox = document.createElement('div')
document.getElementsByClassName('tb-gallery')[0].appendChild(downBox)
const title = document.createElement('h2')
title.innerHTML = '下载主图'
title.style = 'text-align: center; margin-bottom: 20px; margin-top: 20px'
downBox.appendChild(title)
const downList = document.createElement('ul')
downList.className = 'tb-thumb tb-clearfix thumb-ul'
downBox.appendChild(downList)
const imgArr = []
for (let k = 0; k < imgList.getElementsByTagName('li').length; k++) {
let src
const xpath = document.evaluate(
`//li[${k + 1}]/div/a/img`,
imgList,
null,
XPathResult.ANY_TYPE,
null
)
let img = xpath.iterateNext()
if (k === 0) {
const arr = imgList
.getElementsByTagName('li')[0]
?.getElementsByTagName('span')
if (!(arr ?.length >= 1)) {
src = img ?.src
}
} else {
src = img ?.src
}
if (src) {
src = src.substr(0, src.length - 16)
imgArr.push(src)
const downButton = document.createElement('li')
downButton.style =
'display:flex; flexflow: row; align-items: center; justify-content: center'
const downImg = document.createElement('img')
downImg.src = src
downImg.style.cursor = 'pointer'
downImg.title = (k + 1).toString()
downImg.onmouseover = () => {
imgList.getElementsByTagName('li')[k].className = 'tb-selected'
}
downImg.onmouseout = () => {
imgList.getElementsByTagName('li')[k].className = 'tb-thumb tm-clear'
}
downImg.onclick = () => {
GM_download(src, `${props.id}_主图_${k >= 9 ? k + 1 : '0' + (k + 1)}`)
}
downList.appendChild(downButton)
downButton.appendChild(downImg)
}
}
const downAll = document.createElement('li')
downAll.style =
'display:flex; flexflow: row; align-items: center; justify-content: center'
const downAllSpan = document.createElement('a')
downAllSpan.style.cursor = 'pointer'
downAllSpan.onclick = () => {
GM_notification('共下载 ' + imgArr.length + ' 张图片', '下载主图')
download(imgArr, '主图')
}
downAllSpan.innerHTML = '全部'
downList.appendChild(downAll)
downAll.appendChild(downAllSpan)
return imgArr
}
function getProductInfoPage(props) {
console.log('创建详情页图片列表***')
const imgArr = []
const editForm = document.getElementById('J_TabBar')
const downButton = document.createElement('li')
downButton.style =
'display:flex; flexflow: row; align-items: center; justify-content: center'
const downA = document.createElement('a')
downA.innerHTML = '下载详情页'
downA.style = 'color: red; font-weight: bold;'
downA.title =
'请注意,下载详情图之前请先将所有详情图片显示完毕再点击此按钮下载,否则会出现下载不完整等问题'
downA.onclick = () => {
downButton.className = 'tm-selected'
const imgList = document.getElementsByClassName('ke-post')[0].childNodes
const imgArr = []
const mapChild = list => {
list.forEach(i => {
if (i ?.src) {
imgArr.push(i.src)
}
if (i.childNodes.length !== 0) {
mapChild(i.childNodes)
}
})
}
mapChild(imgList)
GM_notification('共下载 ' + imgArr.length + ' 张图片', '下载详情页')
download(imgArr, '详情页')
}
editForm.insertBefore(
downButton,
document.getElementsByClassName('tm-qrcode-icon')[0]
)
downButton.appendChild(downA)
}
function setStyle() {
GM_addStyle(`
.thumb-ul{
font-family:Arial;
font-weight:bold;
}
.thumb-ul li{
border-style:solid;
border-color:#FE4403!important;
font-family:Arial;
font-weight:bold;
font-size:16px;
cursor:pointer;
}
.cat-ul li{
cursor:pointer;
font-size:14px;
font-family:Arial;
}
.detail-li,.border-li{
width:40px!important;
padding:0px!important;
}
.tb-tabbar>li{
min-width:80px!important;
}
`)
}