// ==UserScript==
// @name 花瓣网下载
// @namespace https://www.saintic.com/
// @version 0.1
// @description 花瓣网(huaban.com)用户画板图片批量下载到本地
// @author staugur
// @match http*://huaban.com/boards/*
// @require https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.min.js
// @grant GM_setClipboard
// @license MIT
// @date 2018-05-25
// @modified none
// @github https://github.com/staugur/grab_huaban_board
// @supportURL https://github.com/staugur/grab_huaban_board/issues
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
/*
公共接口
*/
//字符串是否包含子串
function isContains(str, substr) {
//str是否包含substr
return str.indexOf(substr) >= 0;
}
//下载保存图片
function saveImage(imgUrl, imgName) {
//imgUrl: 图片地址; imgName: 保存的文件名
try {
const xhr = new XMLHttpRequest();
xhr.open('GET', imgUrl, true);
xhr.responseType = 'blob';
xhr.onload = () => {
if (xhr.status === 200) {
//将图片文件用浏览器中下载
saveAs(xhr.response, imgName);
}
};
xhr.send();
} catch (e) {
console.log(e);
}
}
//加载css文件
function addCSS(href) {
var link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = href;
document.getElementsByTagName("head")[0].appendChild(link);
}
//加载js文件
function addJS(src, cb) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
script.onload = typeof cb === "function" ? cb : function() {};
}
//由于@require方式引入jquery时layer使用异常,故引用cdn中jquery v1.10.1;加载完成后引用又拍云中layer v3.1.1
addJS("https://cdn.bootcss.com/jquery/1.10.1/jquery.min.js", function() {
addJS("https://static.saintic.com/cdn/layer/3.1.1/layer.js");
});
var board_id = window.location.pathname.split('/')[1] === "boards" ? window.location.pathname.split('/')[2] : "";
var protocol = window.location.protocol;
/*
下载图片
*/
//交互确定下载方式
function interactive(pins) {
var msg = [
'当前画板共抓取' + pins.length + '张图片!提示: 只有登录后才可以抓取几乎所有图片哦。
',
'请选择以下三种下载方式:
',
'1. 文本:
即所有图片地址按行显示,提供复制,粘贴至迅雷、QQ旋风等下载工具批量下载即可,推荐使用此方法。
',
'2. 本地:
即所有图片直接保存到硬盘中,由于是批量下载,所以浏览器设置中请关闭"下载前询问每个文件的保存位置",并且允许浏览器下载多个文件的授权申请,以保证可以自动批量保存,否则每次保存时会弹出询问,对您造成困扰。
',
'3. 远程:
即所有图片将由第三方服务器下载并压缩,提供压缩文件链接,直接下载此链接解压即可。
',
'
寻求帮助?请点击我!
' ].join(''); layer.confirm(msg, { title: "选择画板图片下载方式", closeBtn: false, shadeClose: false, shade: 0, btn: ['文本', '本地', '远程'], btnAlign: 'c', btn3: function(index, layero) { downloadPicRemotely(pins); } }, function(index, layero) { downloadPicText(pins); }, function(index) { downloadPicLocally(pins); }); } //本地下载 function downloadPicLocally(pins) { for (var i = 0, len = pins.length; i < len; i++) { saveImage(pins[i].imgUrl, pins[i].imgName); } } //远端下载 function downloadPicRemotely(pins) { $.ajax({ url: "https://www.saintic.com/CrawlHuaban/", type: "POST", data: { board_id: board_id, pins: JSON.stringify(pins) }, success: function(res) { if (res.success === true) { var msg = "下载任务已经提交!