// ==UserScript== // @name 微博图片批量下载 // @author Shawlj // @namespace http://tampermonkey.net/ // @description 本程序支持新版微博图片批量下载功能(多图下载、动图、转发图下载等) // @version v1.0 // @match https://weibo.com/* // @match https://d.weibo.com/* // @match https://s.weibo.com/* // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.0/jszip.min.js // @require https://cdn.staticfile.org/mustache.js/3.1.0/mustache.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.10/dayjs.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js // @resource xinfo https://cdn.jsdelivr.net/gh/Mr-Po/weibo-resource-download/out/media/info.png // @resource xsuccess https://cdn.jsdelivr.net/gh/Mr-Po/weibo-resource-download/out/media/success.png // @resource xerror https://cdn.jsdelivr.net/gh/Mr-Po/weibo-resource-download/out/media/error.png // @connect sinaimg.cn // @connect weibo.com // @compatible chrome // @compatible firefox // @license MIT // @grant GM_xmlhttpRequest // @grant GM_getResourceURL // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @grant GM_notification // @grant GM_setClipboard // @grant GM_setValue // @grant GM_getValue // @grant GM_addStyle // @grant GM_addElement // @grant GM_deleteValue // @grant GM_openInTab // @grant GM_download // @grant window.onurlchange // @downloadURL none // ==/UserScript== "use strict"; function _classCallCheck(e, t) { if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function"); } function _toConsumableArray(e) { if (Array.isArray(e)) { for (var t = 0, n = Array(e.length); t < e.length; t++) n[t] = e[t]; return n; } return Array.from(e); } var _typeof2 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) { return typeof e; } : function(e) { return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e; }, _createClass = function() { function e(e, t) { for (var n = 0; n < t.length; n++) { var r = t[n]; r.enumerable = r.enumerable || !1, r.configurable = !0, "value" in r && (r.writable = !0), Object.defineProperty(e, r.key, r); } } return function(t, n, r) { return n && e(t.prototype, n), r && e(t, r), t; }; }(), _typeof = "function" == typeof Symbol && "symbol" == _typeof2(Symbol.iterator) ? function(e) { return void 0 === e ? "undefined" : _typeof2(e); } : function(e) { return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : void 0 === e ? "undefined" : _typeof2(e); }; !function() { function e() { if (v.xvalid(48)) { var e = new Set(), t = new MutationObserver(function(t, n) { var r = !0, u = !1, a = void 0; try { for (var c, l = t[Symbol.iterator](); !(r = (c = l.next()).done); r = !0) if ("childList" === c.value.type) { var s = t[0].target, f = s._prevClass; f && (e.has(s) ? e.delete(s) : (e.add(s), new RegExp(i.prv + "|" + i.rep).test(f) && o(s, "xwb_btn"))); } } catch (e) { u = !0, a = e; } finally { try { !r && l.return && l.return(); } finally { if (u) throw a; } } }), r = document.querySelectorAll(i.sup + ', [class^="' + i.sub + '"]'), u = !0, a = !1, c = void 0; try { for (var l, s = r[Symbol.iterator](); !(u = (l = s.next()).done); u = !0) { var f = l.value; t.observe(f, n); } } catch (e) { a = !0, c = e; } finally { try { !u && s.return && s.return(); } finally { if (a) throw c; } } p("微博图片批量下载程序准备就绪"); } } var t = { 0: "xerror", 1: "xsuccess", 2: "xinfo" }, n = { attributes: !1, childList: !0, subtree: !0 }, r = { wurl: "https://weibo.com/", purl: "https://wx{0}.sinaimg.cn", href: "批量下载({0})" }, i = { sup: ".vue-recycle-scroller__item-wrapper", sub: "Feed_body", prv: "Feed_pictureViewer", rep: "Feed_retweet", box: "picture-viewer_previewList", ico: "woo-font woo-font--imgSave", msk: "picture-viewer_mask", img: "picture-viewer_listContentAnimate", pic: "picture-viewer_imgWrap", wut: "a[usercard]>span,a[class^=head-info_time]", clr: "#eb7340" }; $(document).ready(e), window.addEventListener("urlchange", function() { setTimeout(function() { return e(); }, 2e3); }); var o = function(e, t) { var n = e.children[1], o = n.children[1]; if (o && c($(o.lastChild).attr(t))) { var a = $(n).find('div[class*="' + i.msk + '"]').length, l = $(n).find('[class*="' + (a > 0 ? i.img : i.pic) + '"] img').toArray(), s = new Set(l.map(function(e, t) { return e.src.split("/").pop(); })), f = $(o.lastChild).clone(); $(o).append(o.children[1].cloneNode(!0)).append(f); var p = $(o.lastChild).css("color", i.clr).attr(t, t), d = /[@\-\:\s]/g, m = $(e).closest("article").find(i.wut).toArray(); m.length && (m = m.map(function(e) { return (e.title || e.textContent).replace(d, ""); })), $(p).empty().append($(y(r.href, s.size)).click(function() { return u(m, s); })); } }, u = function(e, t) { var n = a(e), i = encodeURIComponent(r.wurl), o = [].concat(_toConsumableArray(t)).map(function(e, t) { return new Promise(function(o, u) { fetch(y(r.purl, m(4)) + "/large/" + e + "?referer=" + i).then(function(e) { return e.blob(); }).then(function(e) { saveAs(e, n + "-" + String(t + 1).padStart(2, 0)), o(1); }).catch(function(e) { return u(e); }); }); }); Promise.all(o).then(function(e) { return d("全部下载完成", 1); }).catch(function(e) { return d(e.message, 0); }); }, a = function(e) { var t = new Array(), n = v.xvalid(54); return Object.values(v.menu_list).forEach(function(r, i) { if (i > 1 && f(r[0])) { var o = e[i - 2]; o && i < 6 && (i % 2 != 0 ? t.push(n ? o : o.slice(0, 8)) : t.push(o)); } }), v.xvalid(55) && t.push(dayjs().format("YYYYMMDDHHmmssSSS")), t.join("-"); }, c = function(e) { return void 0 == e || "undefined" == e || void 0 === e || null == e || e.length < 1 || "" === e || "object" == (void 0 === e ? "undefined" : _typeof(e)) && 0 == Object.keys(e).length; }, l = function(e, t, n) { return c(f(e)) && s(e, t), f(e); }, s = function(e, t) { return GM_setValue(e, t), t; }, f = function(e) { return GM_getValue(e); }, p = function() { var e; return (e = console).log.apply(e, arguments); }, d = function(e, n, r) { v.xvalid(49) && GM_notification({ title: r, text: e, image: GM_getResourceURL(t[n]), timeout: 3e3 }); }, m = function(e) { return Math.floor(Math.random() * e) + 1; }, y = function() { if (0 == arguments.length) return null; for (var e = arguments[0], t = 1; t < arguments.length; t++) { var n = new RegExp("\\{" + (t - 1) + "\\}", "gm"); e = e.replace(n, arguments[t]); } return e; }, h = function() { function e() { _classCallCheck(this, e), this.menu_ids = new Array(9), this.menu_list = { 48: [ "$xwbp-xdn", "脚本启用", !0, "(Alt+0)" ], 49: [ "$xwbn-xms", "消息通知", !0, "(Alt+1)" ], 50: [ "$xwbf-xn1", "文件名:昵称 (原)", !0, "(Alt+2)" ], 51: [ "$xwbf-xt1", "文件名:时间 (原)", !0, "(Alt+3)" ], 52: [ "$xwbf-xn2", "文件名:昵称 (转)", !1, "(Alt+4)" ], 53: [ "$xwbf-xt2", "文件名:时间 (转)", !1, "(Alt+5)" ], 54: [ "$xwbf-sfm", "文件名:时分秒", !0, "(Alt+6)" ], 55: [ "$xwbf-stm", "文件名:时间戳", !0, "(Alt+7)" ] }; } return _createClass(e, [ { key: "xregister", value: function() { var e = this; return this.menu_ids.forEach(function(e) { return GM_unregisterMenuCommand(e); }), Object.values(this.menu_list).forEach(function(t, n) { t.splice(2, 1, l(t[0], t[2])), e.menu_ids[n] = GM_registerMenuCommand((t[2] ? "✅" : "❌") + " " + t[1] + " " + t[3], function() { return e.xswitch(t); }), n > 6 && e.menu_ids.push(GM_registerMenuCommand("💬 反馈 & 建议", function() { return GM_openInTab(l("xurl")); })); }), this; } }, { key: "xswitch", value: function(e) { e && this.xregister(s(e[0], !e[2])); } }, { key: "xkeyboard", value: function() { var e = this; return $(document).keydown(function(t) { var n = t || window.event, r = n.keyCode || n.which; return n.altKey && r && e.xswitch(e.menu_list[r]), !0; }), this; } }, { key: "xvalid", value: function(e) { return f(this.menu_list[e][0]); } } ]), e; }(), v = new h().xregister().xkeyboard(); }();