- // ==UserScript==
- // @name 微博超话图片下载
- // @namespace http://tampermonkey.net/
- // @version 0.5
- // @description 提供超话内原图下载,优化超话浏览体验
- // @author 乃木流架
- // @match https://weibo.com/p/*
- // @match https://weibo.com/u/*
- // @icon https://i.jpg.dog/26e8e3a48d8a079e3bca9bae1a96434b.png
- // @grant GM_download
- // @run-at document-start
- // @license GPL-3.0 License
- // ==/UserScript==
-
- (function () {
- ("use strict");
-
- console.log("🏳️🌈[微博超话图片下载]脚本开始执行");
-
- //自定义用户主页背景图片
- //图床:https://jpg.dog/
- const urlNew = "https://i.jpg.dog/f5651b662ec09801fdd4a54285038ee1.jpeg";
-
- //默认主页背景图片
- const urlDefault1 =
- "https://ww1.sinaimg.cn/mw2000/70ace9b7ly1ggzusnypoej20yi0yiaop.jpg";
- const urlDefault2 =
- "https://wx1.sinaimg.cn/mw2000/001WLsZ7ly1gs69906b4pj60u00u0wiu02.jpg";
-
- /**
- * @desc 属性改变监听,属性被set时出发watch的方法,类似vue的watch
- * @author Jason
- * @study https://www.jianshu.com/p/00502d10ea95
- * @data 2018-04-27
- * @constructor
- * @param {object} opts - 构造参数. @default {data:{},watch:{}};
- * @argument {object} data - 要绑定的属性
- * @argument {object} watch - 要监听的属性的回调
- * watch @callback (newVal,oldVal) - 新值与旧值
- */
-
- class watcher {
- constructor(opts) {
- this.$data = this.getBaseType(opts.data) === "Object" ? opts.data : {};
- this.$watch = this.getBaseType(opts.watch) === "Object" ? opts.watch : {};
- for (let key in opts.data) {
- this.setData(key);
- }
- }
-
- getBaseType(target) {
- const typeStr = Object.prototype.toString.apply(target);
-
- return typeStr.slice(8, -1);
- }
-
- setData(_key) {
- Object.defineProperty(this, _key, {
- get: function () {
- return this.$data[_key];
- },
- set: function (val) {
- const oldVal = this.$data[_key];
- if (oldVal === val) return val;
- this.$data[_key] = val;
- this.$watch[_key] &&
- typeof this.$watch[_key] === "function" &&
- this.$watch[_key].call(this, val, oldVal);
- return val;
- },
- });
- }
- }
-
- // export default watcher;
-
- const picImpl = "https://weibo.com/ajax/statuses/show?id=";
- let length = 15;
- let wm = new watcher({
- data: {
- len: length,
- },
- watch: {
- len(newVal, oldVal) {
- // console.log("length: ", length);
- console.log("🔢新微博条数:" + newVal);
- console.log("🔢旧微博条数:" + oldVal);
- if (newVal > length) {
- let faces = document.getElementsByClassName("WB_info");
- let i = length;
- length = faces.length;
- while (i < length) {
- let btn = initBtn();
- faces[i].appendChild(btn);
- // console.log(i);
- // console.log(faces[i]);
- handleBtn(btn);
- i++;
- }
- }
- },
- },
- });
-
- function sendAjax(type, url) {
- let xhr = new XMLHttpRequest();
- // 拼接所需要的的值
- // 所要拼接的值 + 里面填获取值的参数+ "&或者的意思" 最后拼接的直接+获取值的参数
- xhr.open(type, url, false);
- xhr.send();
- // xhr.onreadystatechange = function () {
- // if (xhr.readyState == 4 && xhr.readyState == 200) {
- // // 这个是转化为字符串的形式
- // let data = JSON.parse(xhr.responseText);
- // callback(data);
- // }
- // };
- return JSON.parse(xhr.responseText);
- }
-
- function initBtn() {
- let div = document.createElement("div");
- let img = document.createElement("img");
- let a = document.createElement("a");
- // img.src = "https://i.jpg.dog/72dbffd3545cb15b148682beaf0fb64a.png";
- // img.src = "https://i.jpg.dog/d5380c9048e6ee303f188da9ec574399.png";
- img.src =
- "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEMAAABDCAYAAADHyrhzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAFMElEQVR4nO2baYgcRRTHKzGJqFlFvDDRL16RCB6sG3f7vdnxQ+KBB3gEL4R8cVFUZN0se8x7U4giqIj4TZB4bEAJKIpCUBA0+SCaLwoKUYOKmCBoNEYYp6vGWFJzrLs7PTO93V3dk539Q8F8merXv36vjveqhFhWd8iMDp5kZP7MssQLygXvwpL01hm56VSxlGWkWKmkd6UmHNUEM4rhc0VwRDOaoKYYfM34tWZ8WxE+WSngZgtOHM8AKgxbai+Ph1u9eNhmASmCjxTltplxr08cDzITm0/ThAXNeDAugJZgCEua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JRtoUxElW/jmPBomh7iwiPqbUTElSp6dzkwLBCIQxC7YoNoSDE+7RqIi9Coh8dnC2eoWDL2EAvjDjdA4BPN8Gb1RkDS/RPuj3zcsfOACjMugDhphPv/LuTOF65kbOWN4JnMX7Sjt+G+wHSeC+kCPtS9d05gZyNRnJoUDV+lCQ90jzdAWRM8JrKSkfm1ivA5e2kuUxCMH9o1kegGKYLLFMEHqYMgPOAT3im6UdomlQnedX51i/ArRXCvzduKbldpenC9jV/N+GWCY8IRO7VX78andTsxaZXl4EV2X6AJ3tCM34QeXwh+roYewZSt2jm/jZiFzEj/al/iJTYHWSG8uXr/rYj322S0/er2elamt5eXtSwxV/8B8CHKT9TokA8AAAAASUVORK5CYII=";
- img.setAttribute(
- "style",
- "width:12px;height:12px;margin-top:5px;margin-right:2px;"
- );
- a.innerHTML = "图片下载";
- a.setAttribute(
- "style",
- "padding:0;font-size: 12px;margin-top:1px;color:#333333;margin-top: 3px;"
- );
- div.appendChild(img);
- div.appendChild(a);
- div.setAttribute("class", "nogiruka-button");
- div.setAttribute(
- "style",
- "display: inline-flex;position:absolute;box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.1);border:1px solid #d9d9d9;height:21px;width:80px;background-color:#ffffff;font-color:#333333;font-size: 12px;text-align: center;border-radius: 2px;cursor: pointer;margin-left:5px;margin-bottom:5px;justify-content: center;"
- );
- return div;
- }
-
- function addBtn() {
- let faces = document.getElementsByClassName("WB_info");
- let i = 0;
- length = faces.length;
- console.log("🔢初始微博条数: " + length);
- while (i < length) {
- let btn = initBtn();
- faces[i].appendChild(btn);
- // console.log(i);
- // console.log(faces[i]);
- handleBtn(btn);
- i++;
- }
- }
-
- function handleBtn(btn) {
- btn.addEventListener("click", function (e) {
- let detail = e.target.parentNode.parentNode;
- let from = detail.nextElementSibling;
- let href = from.firstElementChild.href;
- // console.log(detail);
- // console.log(from);
- // console.log(href);
- let mblogid = href.split("/")[href.split("/").length - 1];
- let url = picImpl + mblogid;
- // console.log(mblogid);
- // console.log(url);
- let response = sendAjax("GET", url);
- // console.log(response);
- const picInfos = response.pic_infos;
- const userName = response.user.screen_name;
- const text = response.text_raw;
- console.log("🌈微博用户:" + userName + "\n📝微博文案:\n" + text);
- let downloadList = [];
- if (picInfos) {
- console.log("💝开始下载图片。。。");
- let index = 0;
- for (const [id, pic] of Object.entries(picInfos)) {
- index += 1;
- let largePicUrl = pic.largest.url;
- let picName = largePicUrl
- .split("/")
- [largePicUrl.split("/").length - 1].split("?")[0];
- let ext = picName.split(".")[1];
- let dlName = userName + "-" + mblogid + "-" + index + "." + ext;
- downloadList.push({
- url: largePicUrl,
- name: dlName,
- headerFlag: true,
- });
- }
- // console.log(downloadList);
- handleDownloadList(downloadList);
- }
- });
- }
-
- function handleDownloadList(downloadList) {
- for (const item of downloadList) {
- downloadWrapper(item.url, item.name, item.headerFlag);
- }
- }
-
- const pro = {
- a: "🌚", //0-25
- b: "🌘", //25-50
- c: "🌓", //50
- d: "🌒", //50-75
- e: "🌝", //100
- };
- function downloadWrapper(url, name, headerFlag) {
- let textContent = name + " [0%]";
- let percent = 0;
- const download = GM_download({
- url,
- name,
- headers: headerFlag
- ? {
- Referer: "https://weibo.com/",
- Origin: "https://weibo.com/",
- }
- : null,
- // saveAs: false,
- onprogress: (e) => {
- // e = { int done, finalUrl, bool lengthComputable, int loaded, int position, int readyState, response, str responseHeaders, responseText, responseXML, int status, statusText, int total, int totalSize }
- percent = (e.done / e.total) * 100;
- percent = percent.toFixed(0);
- textContent = name + " [" + percent + "%]";
- if (percent < 25) {
- console.log(pro.a + textContent);
- } else if (percent >= 25 && percent < 50) {
- console.log(pro.b + textContent);
- } else if (percent >= 50 && percent < 75) {
- console.log(pro.c + textContent);
- } else if (percent >= 75 && percent < 100) {
- console.log(pro.d + textContent);
- } else {
- console.log(pro.e + textContent);
- }
-
- // console.log(textContent);
- },
- onload: ({ status, response }) => {
- console.log("💖图片下载完毕。。。");
- },
- onerror: (e) => {
- console.log(e);
- },
- ontimeout: (e) => {
- console.log(e);
- },
- });
- }
-
- function handleQ() {
- //搜索处理
- if (document.getElementsByClassName("username")[0]) {
- let username = document.getElementsByClassName("username")[0].innerText;
- let input = document.getElementsByClassName("W_input")[0];
- let q = username + "超话 ";
- let ph = document.getElementsByClassName("placeholder")[0];
- if (ph != null) {
- ph.remove();
- }
- // console.log(input.value);
- if (input.value != q) {
- input.value = q;
- }
- }
- }
-
- function handleBack() {
- let back = document.getElementsByClassName("woo-picture-img")[0];
- if (back != null) {
- let url = back.src;
- // console.log(url != null);
- // console.log(url == urlDefault);
- // console.log(url);
- // console.log(urlDefault);
- if (url != null) {
- if (url == urlDefault1 || url == urlDefault2) {
- back.src = urlNew;
- }
- }
- }
- }
-
- let ba = setInterval(() => {
- //更换用户主页背景图片
- handleBack();
- // console.log("更改用户主页背景图片。。。");
- }, 50);
- let go1 = true;
- let go2 = true;
- let currentLink = window.location.href;
- let jumpLink = "";
-
- //全局循环器
- setInterval(() => {
- let back = document.getElementsByClassName("woo-picture-img")[0];
- jumpLink = window.location.href;
- //监听网页链接变化,刷新页面
- if (currentLink != jumpLink && /weibo.com\/u/.test(window.location.href)) {
- if (
- (back != null && back.src == urlDefault1) ||
- back.src == urlDefault2
- ) {
- window.location.href = jumpLink;
- }
- }
-
- //伪超话内搜索
- handleQ();
-
- //去除扫描二维码进入手机超话
- let qr = document.getElementById("Pl_Core_PicText__265");
- if (qr) {
- qr.remove();
- }
-
- //背景图片监视停止
- if (go1) {
- if (!/weibo.com\/u/.test(window.location.href)) {
- go1 = false;
- clearInterval(ba);
- console.log("❎不是用户主页。。。");
- }
- if (/weibo.com\/u/.test(window.location.href) && back != null) {
- if (back.src != urlDefault1 && back.src != urlDefault2) {
- go1 = false;
- clearInterval(ba);
- console.log("✅背景图片已修改。。。");
- }
- }
- }
-
- //翻页
- let nextPage = document.getElementsByClassName(
- "page next S_txt1 S_line1"
- )[0];
- // console.log(nextPage);
- if (nextPage != undefined && go2) {
- go2 = false;
- hr = nextPage.href;
- console.log("🚌下一页出现,地址为:" + hr);
- nextPage.addEventListener("click", function () {
- window.location.href = hr;
- });
- }
-
- //微博条数监控
- let temp = document.getElementsByClassName("WB_info").length;
- if (temp != length && temp > length) {
- wm.len = temp;
- }
- }, 1000);
-
- window.onload = function () {
- //图片下载按钮生成
- setTimeout(() => {
- if (/weibo.com\/p/.test(window.location.href)) {
- addBtn();
- }
- }, 1000);
-
- // addBtn();
- };
- // Your code here...
- })();