// ==UserScript== // @name bilibili-roll-history // @namespace Violentmonkey Scripts // @match https://www.bilibili.com/ // @grant GM_addStyle // @version 1.0 // @author mesimpler // @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@2 // @description 2024/3/22 21:28:21 // @license MIT // @downloadURL none // ==/UserScript== const feedHistory = []; const maxHistory = 10; let feedHistoryIndex = 0; const feedRollBackBtn = ` `; const feedRollNextBtn = ` `; const disconnect = VM.observe(document.body, () => { let feedRollBtn = document.getElementsByClassName("roll-btn")[0]; if (feedRollBtn) { // 处理返回上一页feed的历史内容 let backBtn = document.createElement("button"); feedRollBtn.parentNode.appendChild(backBtn); backBtn.outerHTML = feedRollBackBtn; document .getElementById("feed-roll-back-btn") .addEventListener("click", () => { let feedCards = document.getElementsByClassName("feed-card"); if (feedHistoryIndex == feedHistory.length) { feedHistory.push(listInnerHTMLOfFeedCard(feedCards)); } for (let fc_i = 0; fc_i < feedCards.length; fc_i++) { feedCards[fc_i].innerHTML = feedHistory[feedHistoryIndex - 1][fc_i]; } feedHistoryIndex = feedHistoryIndex - 1; if (feedHistoryIndex == 0) { disableElementById("feed-roll-back-btn", true); } disableElementById("feed-roll-next-btn", false); }); // 处理返回下一页feed的历史内容 let nextBtn = document.createElement("div"); feedRollBtn.parentNode.appendChild(nextBtn); nextBtn.outerHTML = feedRollNextBtn; document .getElementById("feed-roll-next-btn") .addEventListener("click", () => { let feedCards = document.getElementsByClassName("feed-card"); for (let fc_i = 0; fc_i < feedCards.length; fc_i++) { feedCards[fc_i].innerHTML = feedHistory[feedHistoryIndex + 1][fc_i]; } feedHistoryIndex = feedHistoryIndex + 1; if (feedHistoryIndex == feedHistory.length - 1) { disableElementById("feed-roll-next-btn", true); } disableElementById("feed-roll-back-btn", false); }); // 处理点击换一换事件 feedRollBtn.id = "feed-roll-btn"; feedRollBtn.addEventListener("click", () => { if (feedHistoryIndex == feedHistory.length) { let feedCards = listInnerHTMLOfFeedCard( document.getElementsByClassName("feed-card") ); feedHistory.push(feedCards); } if (feedHistory.length > maxHistory) { feedHistory.shift(); } feedHistoryIndex = feedHistory.length; console.log(feedHistory); disableElementById("feed-roll-back-btn", false); disableElementById("feed-roll-next-btn", true); }); return true; } }); function disableElementById(id, bool) { if (bool) { document.getElementById(id).classList.add("biliplus-disabled"); } else { document.getElementById(id).classList.remove("biliplus-disabled"); } } function listInnerHTMLOfFeedCard(feedCardElements) { let feedCardInnerHTMLs = []; for (let fc of feedCardElements) { feedCardInnerHTMLs.push(fc.innerHTML); } return feedCardInnerHTMLs; } GM_addStyle(` .feed-roll-back-btn { flex-direction: column; margin-left: 0 !important; height: 40px !important; width: 40px; padding: 9px; margin-top: 6px; } .feed-roll-back-btn svg { margin-right: 0; margin-bottom: 0px; } .feed-roll-next-btn { flex-direction: column; margin-left: 0 !important; height: 40px !important; width: 40px; padding: 9px; margin-top: 6px; } .feed-roll-next-btn svg { margin-right: 0; margin-bottom: 0px; } .biliplus-disabled{ opacity: 0.5; cursor: not-allowed; pointer-events: none; } `);