微博相册图片地址批量获取工具

批量获取微博相册图片地址

当前为 2019-05-28 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        WeiboPhotoUrlBatchGet
// @name:zh-CN  微博相册图片地址批量获取工具
// @namespace   http://www.mapaler.com/
// @description BatchGetWeiboPhotoURL
// @description:zh-CN 批量获取微博相册图片地址
// @include     *://weibo.com/*home*
// @include     *://weibo.com/*friends*
// @include     *://weibo.com/*mygroups*
// @include     *://photo.weibo.com/*albums/detail/album_id/*
// @include     *://photo.weibo.com/*talbum/index*
// @include     *://photo.weibo.com/*photos*
// @version     2.0.3
// @grant       none
// @copyright   2016+, Mapaler <[email protected]>
// @icon        data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACkAAAAuCAYAAAC1ZTBOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAK6wAACusBgosNWgAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNui8sowAAASASURBVFiFzZk/bNtGFIc/Cl4KFKjTQSDQIQyaSYvpJWMjg0Az1u7ELrU8tUCH0gjQTYgDrSlMD2l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gxohDjrKSj27TZwD9gBdvSa0gJoRsM6Yg1Xtddyq4X4kScM8+6DImBnloQxqQLQcqt9Q5vpNBarFx4iCpBkxj5ClgAEpZ5sRsNX5CuQpOVW7036sjyN/Ux+Tw2PLNFOpm0h5oUMAP2w6KDlVtvLdqL0oZ6CqAcPo5ZbnX6CIV9wobUnwHbLrc48VDK+QXj3A/LnRJO0UxJ2+e1DMxrqu7cMNDuIH2ntd5X7OsupFFLfd/cpFqUOxf+5rEJJWYNeT5pWg1UqQZ7smvZVmQq7xZIh/z9gEkTWvwGisqE1yXTMcsbiwxtJmMsMbB6YMpkgQ8TcZzqTjBgn0A2IhEmWhVlIzWjorKVjTf8BGAN+rkqYYl0AAAAASUVORK5CYII=
// @noframes
// ==/UserScript==

(function() {
	var scriptVersion = "LocalDebug"; //本程序的版本
	var scriptName = "WeiboPhotoUrlBatchGet"; //本程序的名称
	if (typeof(GM_info)!="undefined")
	{
		scriptVersion = GM_info.script.version.replace(/(^\s*)|(\s*$)/g, "");
		if (GM_info.script.name_i18n)
		{
			var i18n = (navigator.language||navigator.userLanguage).replace("-","_"); //获取浏览器语言
			scriptName = GM_info.script.name_i18n[i18n]; //支持Tampermonkey
		}
		else
		{
			scriptName = GM_info.script.localizedName || //支持Greasemonkey 油猴子 3.x
						GM_info.script.name; //支持Violentmonkey 暴力猴
		}
	}
	var imgs = { img: [] , count:0};
	function imgObj()
	{
		var obj = {
			addFormUrl: function (url)
			{
				var regSrc = /(https?:\/\/[^\/]+)\/.+\/([\d\w]+)\.([\d\w]+)/ig;
				var result = regSrc.exec(url);
				if (result == null) return this;
				this.host = result[1];
				this.pid = result[2];
				this.extention = result[3];
				return this;
			},
			add: function (host, pid, extention)
			{
				if (extention == undefined) extention = "jpg";
				this.host = host;
				this.pid = pid;
				this.extention = extention;
				return this;
			},
			get: function (size)
			{
				if (size == undefined) size = "large";
				var src = [
					this.host,
					"/",
					size,
					"/",
					this.pid,
					".",
					this.extention
				];
				return src.join("");
			},
			host: "",
			pid: "",
			extention: "",
			//width: 0,
			//height: 0,
			//large: "",	mw690: "",	mw600: "",	bmiddle: "",	small: "",	square: "",	sq612: "",	orj480: "",	smsq612all: "",	thumb300: "",	thumb180: "",	thumb150: "",
		}
		return obj;
	}
	
	
	//仿GM_xmlhttpRequest函数v1.3
	if (typeof(GM_xmlhttpRequest) == "undefined") {
		var GM_xmlhttpRequest = function(GM_param) {
	
			var xhr = new XMLHttpRequest(); //创建XMLHttpRequest对象
			xhr.open(GM_param.method, GM_param.url, true);
			if (GM_param.responseType) xhr.responseType = GM_param.responseType;
			if (GM_param.overrideMimeType) xhr.overrideMimeType(GM_param.overrideMimeType);
			xhr.onreadystatechange = function() //设置回调函数
				{
					if (xhr.readyState === xhr.DONE) {
						if (xhr.status === 200 && GM_param.onload)
							GM_param.onload(xhr);
						if (xhr.status !== 200 && GM_param.onerror)
							GM_param.onerror(xhr);
					}
				}
	
			for (var header in GM_param.headers) {
				xhr.setRequestHeader(header, GM_param.headers[header]);
			}
	
			xhr.send(GM_param.data ? GM_param.data : null);
		}
	}
	//仿GM_getValue函数v1.0
	if (typeof(GM_getValue) == "undefined") {
		var GM_getValue = function(name, type) {
			var value = localStorage.getItem(name);
			if (value == undefined) return value;
			if ((/^(?:true|false)$/i.test(value) && type == undefined) || type == "boolean") {
				if (/^true$/i.test(value))
					return true;
				else if (/^false$/i.test(value))
					return false;
				else
					return Boolean(value);
			} else if ((/^\-?[\d\.]+$/i.test(value) && type == undefined) || type == "number")
				return Number(value);
			else
				return value;
		}
	}
	//仿GM_setValue函数v1.0
	if (typeof(GM_setValue) == "undefined") {
		var GM_setValue = function(name, value) {
			localStorage.setItem(name, value);
		}
	}
	
	
	var win = document.body.appendChild(document.createElement('div'));
	win.id = "WeiboPhotoUrlBatchGet"
	win.className = "WPUBG_win"
	win.style.display = "none";
	
	var style = win.appendChild(document.createElement("style"));
	style.type = "text/css";
	style.appendChild(document.createTextNode(`
	.WPUBG_win {
		box-shadow:0 0 10px #333;
		position:fixed;
		top:0;
		right:0;
		z-index:1000000;
		font-family:arial,sans-serif;
		padding:5px;
		margin:0;
		border-radius: 0 0 0 5px;
		background:#F5F8FA
	}
	.WPUBG_box {
		width:180px
	}
	.WPUBG_tra {
		width:180px;
		height:180px
	}
	.WPUBG_cls {
		width:40px;
		box-shadow:0 0 2px #333;
		position:absolute;
		top:0;
		left:-40px;
		line-height:25px;
		padding:0;
		margin:0;
		border-radius:0;
		border:none;
		background:#515151;
		z-index:99999;
		text-align:center;
		color:#aaa;
		cursor:pointer
	}
	.WPUBG_ipt {
		width:100px
	}
	.WPUBG_rate {
		float:right
	}
	`));
	
	var box = win.appendChild(document.createElement('div'));
	box.className = "WPUBG_box";
	
	var title = box.appendChild(document.createElement('div'));
	title.className = "WPUBG_title";
	title.appendChild(document.createTextNode(scriptName + " v" + scriptVersion));
	
	var tra = box.appendChild(document.createElement('textarea'));
	tra.className = "WPUBG_tra";
	tra.wrap = "off";
	tra.placeholder = "获取数据中...";
	
	var lbl = box.appendChild(document.createElement('label'));
	lbl.className = "WPUBG_lbl";
	lbl.appendChild(document.createTextNode("Size:"));
	lbl.title = `常见尺寸:
	large
	mw690
	mw600
	bmiddle
	small
	square
	sq612
	orj480
	smsq612all
	thumb300
	thumb180
	thumb150`;
	
	var ipt = box.appendChild(document.createElement('input'));
	ipt.className = "WPUBG_ipt";
	ipt.type = "text";
	ipt.placeholder = "large";
	ipt.name = "WPUBG_size";
	ipt.title = lbl.title;
	ipt.value = GM_getValue("WPUBG_size");
	ipt.onblur = function ()
	{
		GM_setValue("WPUBG_size", this.value);
		reCreatList();
	}
	
	var rate = box.appendChild(document.createElement('span'));
	rate.className = "WPUBG_rate";
	
	var cls = win.appendChild(document.createElement('div'));
	cls.className = "WPUBG_cls";
	cls.innerHTML = '关闭';//关闭
	cls.onclick = function (){win.style.display = "none";}
	
	var insertPlace = document.body;
	if (document.location.host == "photo.weibo.com")
	{
		var album = typeof($GLOBAL_DETAIL) != "undefined"; //无 $GLOBAL_DETAIL 则不是专辑
		insertPlace = document.querySelector(".m_share_like") || document.querySelector(".m_user_album");
		var btnGetAll = document.createElement('button');
		btnGetAll.className = "M_btn_h";
		btnGetAll.innerHTML = "获取本专辑全部图片地址";
		btnGetAll.onclick = function () { win.style.display = "block";getAll(album); }
		insertPlace.insertBefore(btnGetAll, insertPlace.firstChild);
	} else
	{
		insertPlace = document.querySelector("#plc_top");
		var btnGetUp = insertPlace.appendChild(document.createElement('button'));
		btnGetUp.className = "W_btn_a";
		btnGetUp.innerHTML = "获得上传的图地址";
		btnGetUp.style.cssFloat = "left";
		btnGetUp.onclick = function () { win.style.display = "block";getUp(); }
	}
	
	function getUp()
	{
		var drag_pic_list = document.getElementsByClassName("drag_pic_list")[0];
		var pics = drag_pic_list.getElementsByClassName("pic");
		for (var pi = 0, len=pics.length; pi < len; pi++)
		{
			var pdiv = pics[pi].getElementsByTagName("div")[0];
			var img = new imgObj;
			imgs.img.push(img.addFormUrl(pdiv.style.backgroundImage))
			tra.value += img.get() + "\r\n";
		}
	}
	function getAll(isAlbum)
	{
		if (imgs.img.length > 0)
			reCreatList();  //重新生成列表,不重复获取
		else
		{
			var type = (isAlbum && $GLOBAL_DETAIL.type == 3) ? 3 : 1 ; //当type为3时(访问他人相册),无法一次性获取所有图像,只能每次30个。
	
			GM_xmlhttpRequest({
				method: "GET",
				url: "http://photo.weibo.com/photos/get_all?uid=" + $CONFIG.owner_uid + (isAlbum?"&album_id=" + $GLOBAL_DETAIL.album_info.album_id:"") + "&count=1&type=" + type + "&__rnd=" + new Date().getTime(),
				onload: function(response) {
					dellFirstJSON(response, $CONFIG.owner_uid, (isAlbum?$GLOBAL_DETAIL.album_info.album_id:isAlbum), type)
				}
			});
		}
	}
	
	function dellFirstJSON(response, uid, aid, type)
	{
		var info = JSON.parse(response.response);
		imgs.count = info.data.total; //添加图片总数
	
		//当前进度
		rate.innerHTML = imgs.img.length + "/" + imgs.count;
		if (imgs.count<1){alert("图片总数为0,可能没有图片。");return;}
	
		if(type == 3)
		{
			var imgCountInPage = 30;
			for(var pi=1, len=Math.ceil(imgs.count/imgCountInPage); pi<=len; pi++)
			{
				var thePi = pi;
				GM_xmlhttpRequest({
					method: "GET",
					url: "http://photo.weibo.com/photos/get_all?uid=" + uid + (aid?"&album_id=" + aid:"") + "&count=" + imgCountInPage + "&page=" + thePi + "&type=" + type + "&__rnd=" + new Date().getTime(),
					onload: function(response) {
						dellAllJSON(response, thePi);
					}
				});
			}
		}else
		{
			GM_xmlhttpRequest({
				method: "GET",
				url: "http://photo.weibo.com/photos/get_all?uid=" + uid + (aid?"&album_id=" + aid:"") + "&count=" + info.data.total + "&type=" + type + "&__rnd=" + new Date().getTime(),
				onload: function(response) {
					dellAllJSON(response);
				}
			});
		}
	}
	
	function dellAllJSON(response, pageIndex)
	{
		var info = JSON.parse(response.response);
		var plist = info.data.photo_list;
		if (plist.length<1){
			alert((pageIndex?pageIndex + "页" :"") + "没有获取到图片数据,可能是有些图被渣浪和谐了,也可能不支持本页面或API有变化需要更新脚本。");
			console.log(response)
			return;
		}
		for (var pi = plist.length - 1; pi >= 0; pi--)
		{
			var img = new imgObj;
			var regFn = /([\d\w]+)\.([\d\w]+)/ig;
			var resultFn = regFn.exec(plist[pi].pic_name);
			img.add(plist[pi].pic_host, plist[pi].pic_pid, resultFn[2]);
			imgs.img.push(img);
		}
		reCreatList();
	}
	
	function reCreatList(size) //重新生成列表
	{
		if (size == undefined) size = (GM_getValue("WPUBG_size") && GM_getValue("WPUBG_size").length) > 0 ? GM_getValue("WPUBG_size") : "large";
	
		//用了ES5的map,将当前所有图像生成链接写入
		var links = imgs.img.map(function (img) {return img.get(size);});
		tra.value = links.join("\n");
	
		//当前进度
		rate.innerHTML = imgs.img.length + "/" + imgs.count;
	}
	
	})();