// ==UserScript== // @name bilibili-roll-history // @namespace Violentmonkey Scripts // @match https://www.bilibili.com/ // @run-at document-idle // @grant GM_addStyle // @version 1.2.2 // @author mesimpler // @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@2 // @description 为你的b站首页添加换一换回溯历史功能。(add roll history btn in bilibili home page.) // @license MIT // @downloadURL https://update.greasyfork.icu/scripts/490584/bilibili-roll-history.user.js // @updateURL https://update.greasyfork.icu/scripts/490584/bilibili-roll-history.meta.js // ==/UserScript== const feedHistory = []; const maxHistory = 10; let feedHistoryIndex = 0; const nextBtn = MS_createElement(` `); const backBtn = MS_createElement(` `); const targetNode = document.querySelector(".recommended-container_floor-aside"); const disconnect = VM.observe(targetNode, () => { const feedRollBtn = document.querySelector(".roll-btn"); if (feedRollBtn) { // 处理返回上一页feed的历史内容 feedRollBtn.parentNode.appendChild(backBtn); backBtn.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; updateBtnStatus(); }); // 处理返回下一页feed的历史内容 feedRollBtn.parentNode.appendChild(nextBtn); nextBtn.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; updateBtnStatus(); }); // 处理点击换一换事件 feedRollBtn.addEventListener("click", () => { // 等待网络请求加载完毕 setTimeout(() => { if (feedHistoryIndex == feedHistory.length) { let feedCards = listInnerHTMLOfFeedCard( document.getElementsByClassName("feed-card") ); feedHistory.push(feedCards); } if (feedHistory.length > maxHistory) { feedHistory.shift(); } feedHistoryIndex = feedHistory.length; updateBtnStatus(); }); }); // disconnect observer return true; } }); GM_addStyle(` .feed-roll-back-btn { flex-direction: column; margin-left: 0 !important; height: 40px !important; width: 40px; padding: 11px; 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: 11px; margin-top: 6px; } .feed-roll-next-btn svg { margin-right: 0; margin-bottom: 0px; } .btn-disabled{ opacity: 0.5; cursor: not-allowed; pointer-events: none; } `); function updateBtnStatus() { if (feedHistoryIndex <= 0) { backBtn.classList.add("btn-disabled"); } else { backBtn.classList.remove("btn-disabled"); } if (feedHistoryIndex >= feedHistory.length - 1) { nextBtn.classList.add("btn-disabled"); } else { nextBtn.classList.remove("btn-disabled"); } } function listInnerHTMLOfFeedCard(feedCardElements) { let feedCardInnerHTMLs = []; for (let fc of feedCardElements) { feedCardInnerHTMLs.push(fc.innerHTML); } return feedCardInnerHTMLs; } function MS_createElement(htmlString) { const element = document.createElement("div"); element.innerHTML = htmlString; return element.firstElementChild; }