// ==UserScript==
// @name 微博超话图片下载
// @namespace http://tampermonkey.net/
// @version 0.9
// @description 提供超话内原图下载,优化超话浏览体验
// @author 乃木流架
// @match https://weibo.com/p/*
// @match https://weibo.com/u/*
// @match https://weibo.com/*
// @icon https://i.jpg.dog/26e8e3a48d8a079e3bca9bae1a96434b.png
// @require https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js
// @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=";
const chaohuaImpl =
"https://weibo.com/ajax/profile/topicContent?tabid=231093_-_chaohua";
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/ThAXNeDAugJZgCEua4BWfvA2iG2XGvT7F8JRi+NMVhAAoxzTjrq6C4jPcogl/Sg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DE+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-top:-2px;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;
// console.log(picInfos);
const picNum = response.pic_num;
const userName = response.user.screen_name;
const text = response.text_raw;
console.log("🌈微博用户:" + userName + "\n📝微博文案:\n" + text);
let downloadList = [];
if (picInfos) {
console.log("💝开始下载图片");
console.log("📦共计 " + picNum + " 张图片");
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("💖" + name + " 下载完毕");
// 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 handleChaohua() {
// console.log("handleChaohua...");
let tab = document.getElementsByClassName("wbpro-tab2")[0];
let searchb = document.querySelector(".Search_btn_3zZx3.Search_btn_3zZx3");
// searchb.style.marginRight = "10px";
tab.childNodes[5].remove();
tab.childNodes[5].remove();
// console.log(tab.lastElementChild);
let img = document.createElement("img");
img.className = "icon-link wbpro-textcut nogiruka";
img.setAttribute(
"style",
"padding:5px;width: 22px;height: 22px;margin-top: 7px;color: black;cursor: pointer;"
);
img.src =
"https://h5.sinaimg.cn/upload/100/959/2020/05/09/timeline_card_small_super_default.png";
let imgdiv = document.createElement("imgdiv");
imgdiv.className = "woo-box-item-inlineBlock";
imgdiv.appendChild(img);
let div = document.createElement("div");
let divin = document.createElement("div");
let d = document.createElement("div");
div.className = "woo-box-item-inlineBlock";
divin.className = "woo-box-flex woo-box-alignCenter woo-box-justifyCenter";
d.className = "wbpro-textcut";
d.innerHTML = "我的超话";
divin.appendChild(d);
div.appendChild(divin);
tab.children[3].insertAdjacentElement("beforebegin", imgdiv);
tab.children[3].insertAdjacentElement("beforebegin", div);
//获取超话列表
let data = sendAjax("GET", chaohuaImpl).data;
let res = data.list;
// console.log(data);
let length = res.length;
let chaohuaList = [];
if (res) {
let index = 0;
for (const [id, l] of Object.entries(res)) {
index += 1;
let link = l.link;
let pic = l.pic;
let topic_name = l.topic_name;
let content1 = l.content1;
let content2 = l.content2;
chaohuaList.push({
index: index,
link: link,
pic: pic,
topic_name: topic_name,
content1: content1,
content2: content2,
});
}
// console.log(chaohuaList);
}
imgdiv.addEventListener("click", function () {
window.location.href = chaohuaAll;
});
div.addEventListener("click", function () {
console.log("💝开始处理超话信息。。。");
console.log("🔢超话条数:" + length);
for (let index = 0; index < tab.children.length; index++) {
tab.children[index].className = "woo-box-item-inlineBlock";
}
div.className = "woo-box-item-inlineBlock cur";
wrapper = document.getElementsByClassName(
"vue-recycle-scroller__item-wrapper"
)[0];
wrapper.firstElementChild.insertAdjacentElement(
"beforebegin",
wrapperDiv
);
// console.log(e);
while (wrapper.hasChildNodes()) {
//当table下还存在子节点时 循环继续
wrapper.removeChild(wrapper.firstChild);
}
// console.log("wrapper: ", wrapper);
handleChaohuaList(chaohuaList, length);
// $("a").each(function () {
// if (
// $(this).attr("href").indexOf("javascript") < 0 &&
// $(this).attr("href").indexOf("#") < 0
// ) {
// $(this).attr("target", "_blank");
// }
// });
});
}
function handleChaohuaList(chaohuaList, length) {
// console.log("handleChaohuaList...");
for (const item of chaohuaList) {
// console.log("处理超话列表。。。");
// console.log(item);
chaohuaWrapper(
item.index,
item.link,
item.pic,
item.topic_name,
item.content1,
item.content2
);
}
// console.log("wrapperDiv.length: " + wrapperDiv.children.length);
// 添加查看全部超话按钮
if (wrapperDiv.children.length == length) {
// console.log("添加查看全部超话按钮。。。");
let all = document.createElement("div");
let bt = document.createElement("a");
all.className = "W_pages";
all.setAttribute(
"style",
"background-color: white;display: grid;padding: 20px;transform: translateY(" +
length * 92 +
"px);"
);
// a.className = "page next S_txt1 S_line1";
bt.innerHTML = "查看全部超话";
// bt.className = "nogiruka";
// bt.className = "page next S_txt1 S_line1";
bt.href = chaohuaAll;
bt.setAttribute(
"style",
"padding: 4px 0;text-align: center;font-weight: 400;background-color: transparent;cursor: pointer;font-size: 15px;display: block;border: 1px solid transparent;color:#fa7d3c"
);
all.appendChild(bt);
wrapperDiv.appendChild(all);
console.log("💖超话信息处理完毕。。。");
// console.log(wrapperDiv);
} else {
alert("❌出错了,请刷新");
location.reload();
return;
}
wrapper.appendChild(wrapperDiv);
}
let autoplaySigns = localStorage.getItem("autoplaySigns");
let userId = autoplaySigns.split('"')[1];
console.log("🆔登录用户ID为:" + userId);
const chaohuaAll =
"https://weibo.com/u/page/follow/" + userId + "/231093_-_chaohua";
//
let wrapper = "";
let wrapperDiv = document.createElement("div");
wrapperDiv.className = "wrapperDiv";
function chaohuaWrapper(index, link, pic, topic_name, content1, content2) {
//
// console.log("chaohuaWrapper...");
let tr = (index - 1) * 92;
// console.log(index);
// console.log(tr);
let st = "background-color: white;transform: translateY(" + tr + "px)";
// console.log(st);
let view = document.createElement("div");
view.className = "vue-recycle-scroller__item-view";
view.setAttribute("style", st);
view.innerHTML =
'<div data-index="0" data-active="true" class="wbpro-scroller-item"><div class="woo-box-flex TopicFeedCard_topicFeedCard_159d4"><div class="woo-box-flex woo-box-alignCenter TopicFeedCard_item_1Ikoi"><a class="ALink_none_1w6rm TopicFeedCard_left_EgdGf" href="' +
link +
"/super_index" +
'"><div class="woo-picture-main woo-picture-square woo-picture-hover TopicFeedCard_pic_1ilsA" style="width: 3.125rem;"><!----><img alt="等比图" src="' +
pic +
'" class="woo-picture-img"><div class="woo-picture-cover"></div><div class="woo-picture-hoverMask"></div><!----></div><div class="woo-box-item-flex TopicFeedCard_con_294Gq TopicFeedCard_f12_avvOy" style="align-self: center;"><div class="TopicFeedCard_cla_1pQ6t TopicFeedCard_fb_1aJkn TopicFeedCard_cut_3akoX">' +
topic_name +
'</div><div class="TopicFeedCard_clb_3j52M TopicFeedCard_cut_3akoX"> ' +
content1 +
' </div><div class="TopicFeedCard_clb_3j52M">' +
content2 +
'</div></div></a><div class="TopicFeedCard_right_1ygKg"><button class="woo-button-main woo-button-line woo-button-default woo-button-s woo-button-round FollowBtn_s_3J5Ve"><span class="woo-button-wrap"><!----><!----><!----><span class="woo-button-content"> 已关注 </span></span></button></div></div></div></div>';
let view1 = view;
wrapperDiv.appendChild(view1);
// while (view.hasChildNodes()) {
// //当table下还存在子节点时 循环继续
// view.removeChild(view.firstChild);
// }
// console.log(wrapper);
}
function handleBack() {
let backMain = document.getElementsByClassName(
"woo-picture-main ProfileHeader_pic_2Coeq"
)[0];
let back = document.getElementsByClassName("woo-picture-img")[0];
if (backMain != null && 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 backMain = document.getElementsByClassName(
"woo-picture-main ProfileHeader_pic_2Coeq"
)[0];
let back = document.getElementsByClassName("woo-picture-img")[0];
jumpLink = window.location.href;
//监听网页链接变化,刷新页面
if (
currentLink != jumpLink &&
/weibo.com\/u/.test(window.location.href) &&
backMain != null
) {
console.log("currentLink: " + currentLink);
console.log("jumpLink: " + jumpLink);
if (back.src == urlDefault1 || back.src == urlDefault2) {
window.location.href = jumpLink;
}
}
// if (currentLink != jumpLink) {
// handleAllChaohua();
// }
//伪超话内搜索
handleQ();
//去除扫描二维码进入手机超话
let qr = document.getElementById("Pl_Core_PicText__265");
if (qr) {
qr.remove();
}
//背景图片监视停止
if (go1) {
if (!backMain) {
go1 = false;
clearInterval(ba);
console.log("❎不是用户主页");
}
if (/weibo.com\/u/.test(window.location.href) && backMain != null) {
if (back.src != urlDefault1 && back.src != urlDefault2) {
go1 = false;
clearInterval(ba);
console.log("✅背景图片已修改");
}
}
}
//超话按钮
let home = document.getElementsByClassName("Search_outer_3k7Aq")[0];
let chaohuaimg = document.getElementsByClassName(
"icon-link wbpro-textcut nogiruka"
)[0];
if (home != null) {
if (chaohuaimg != null) {
return;
} else {
console.log("🌼生成超话按钮");
handleChaohua();
}
}
//翻页
let nextPage = document.getElementsByClassName(
"page next S_txt1 S_line1"
)[0];
// console.log(nextPage);
if (nextPage != null && 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);
function resourceInject() {
let script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.src = "https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js";
document.documentElement.appendChild(script);
}
// function handleAllChaohua() {
// let ch = document.getElementsByClassName(
// "ALink_none_1w6rm TopicFeedCard_left_EgdGf"
// );
// if (ch) {
// for (let index = 0; index < ch.length; index++) {
// console.log(ch[index].href);
// ch[index].href = ch[index].href + "/super_index";
// console.log(ch[index].href);
// }
// }
// }
window.onload = function () {
//图片下载按钮生成
setTimeout(() => {
resourceInject();
// handleAllChaohua();
if (/weibo.com\/p/.test(window.location.href)) {
addBtn();
}
// let home = document.getElementsByClassName("Search_outer_3k7Aq")[0];
// if (home != null) {
// console.log("🌼生成超话按钮");
// handleChaohua();
// }
}, 2000);
// addBtn();
};
// Your code here...
})();