|
| 1 | +// ==UserScript== |
| 2 | +// @name 屏蔽B站营销视频和推广视频 |
| 3 | +// @name:zh-CN 屏蔽B站营销视频和推广视频 |
| 4 | +// @name:zh-TW 屏蔽B站营销视频和推广视频 |
| 5 | +// @name:en Block Bilibili's marketing videos and promotional videos |
| 6 | +// @namespace http://tampermonkey.net/ |
| 7 | +// @version 2.8 |
| 8 | +// @description 屏蔽部分B站(bilibili)主页推荐的视频卡片,屏蔽up主粉丝少于一定数量的,屏蔽直播与右侧推广,屏蔽带广告标签的 |
| 9 | +// @description:zh-CN 屏蔽部分B站(bilibili)主页推荐的视频卡片,屏蔽up主粉丝少于一定数量的,屏蔽直播与右侧推广,屏蔽带广告标签的 |
| 10 | +// @description:zh-TW 遮罩部分B站(bilibili)主頁推薦的視頻卡片,遮罩up主粉絲少於一定數量的,遮罩直播與右側推廣,遮罩帶廣告標籤的 |
| 11 | +// @description:en Block some video cards recommended on the homepage of Bilibili. The rules are to block those from creators with a certain number of small fans, block live streams and right-hand promotion, and block those with advertising tags. |
| 12 | +// @author anonymous |
| 13 | +// @license GNU General Public License v3.0 |
| 14 | +// @icon https://www.bilibili.com/favicon.ico |
| 15 | +// @match https://www.bilibili.com/ |
| 16 | +// @match https://www.bilibili.com/?spm_id_from=* |
| 17 | +// @grant none |
| 18 | +// @downloadURL https://update.greasyfork.org/scripts/467384/%F0%9F%9B%A0%EF%B8%8F%E5%B1%8F%E8%94%BDB%E7%AB%99%E8%90%A5%E9%94%80%E8%A7%86%E9%A2%91.user.js |
| 19 | +// @updateURL https://update.greasyfork.org/scripts/467384/%F0%9F%9B%A0%EF%B8%8F%E5%B1%8F%E8%94%BDB%E7%AB%99%E8%90%A5%E9%94%80%E8%A7%86%E9%A2%91.meta.js |
| 20 | +// ==/UserScript== |
| 21 | + |
| 22 | +"use strict"; |
| 23 | +(() => { |
| 24 | + var __defProp = Object.defineProperty; |
| 25 | + var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); |
| 26 | + |
| 27 | + // src/bilibili-block/index.ts |
| 28 | + var FILTER_CLASSES = [".bili-feed-card"]; |
| 29 | + var FILTER_BLOCK_CLASSES = [".floor-single-card"]; |
| 30 | + var FILTER_BLOCK_UIDS = [113560378]; |
| 31 | + var MIN_FOLLOWER = 2e3; |
| 32 | + var API_USERDATA = "https://api.bilibili.com/x/relation/stat?vmid="; |
| 33 | + var processedCards = 0; |
| 34 | + function getUid(card) { |
| 35 | + const ownerLink = card.querySelector( |
| 36 | + ".bili-video-card__info--owner" |
| 37 | + ); |
| 38 | + if (ownerLink) { |
| 39 | + const uid = ownerLink.href.split("/").pop(); |
| 40 | + if (uid && uid.match(/^\d+$/)) { |
| 41 | + return Number(uid); |
| 42 | + } else { |
| 43 | + logMessages += `\u{1F7E2}remove becouse can't get uid: ${processedCards}, uid: ${uid} |
| 44 | +`; |
| 45 | + return -1; |
| 46 | + } |
| 47 | + } |
| 48 | + logMessages += `\u{1F7E2}remove becouse can't get ownerLink, processedCards: ${processedCards}, ownerLink: ${ownerLink} |
| 49 | +`; |
| 50 | + return -1; |
| 51 | + } |
| 52 | + __name(getUid, "getUid"); |
| 53 | + async function getFollower(uid) { |
| 54 | + const response = await fetch(`${API_USERDATA}${uid}`); |
| 55 | + logMessages += `\u{1F7E2}getFollower, uid: ${uid} |
| 56 | +`; |
| 57 | + const data = await response.json(); |
| 58 | + if (data.code === 0) { |
| 59 | + return data.data.follower; |
| 60 | + } else { |
| 61 | + logMessages += `\u{1F534}getFollower error, uid: ${uid}, message: ${data.message} |
| 62 | +`; |
| 63 | + return -1; |
| 64 | + } |
| 65 | + } |
| 66 | + __name(getFollower, "getFollower"); |
| 67 | + async function editCards(card) { |
| 68 | + processedCards++; |
| 69 | + const uid = getUid(card); |
| 70 | + if (uid === -1) { |
| 71 | + logMessages += `\u{1F7E2}remove because getUid error, uid: ${uid} |
| 72 | +`; |
| 73 | + removeCard(card); |
| 74 | + return; |
| 75 | + } |
| 76 | + if (FILTER_BLOCK_UIDS.includes(uid)) { |
| 77 | + logMessages += `\u{1F7E2}remove because uid in FILTER_BLOCK_UIDS, uid: ${uid} |
| 78 | +`; |
| 79 | + removeCard(card); |
| 80 | + return; |
| 81 | + } |
| 82 | + const follower = await getFollower(uid); |
| 83 | + if (follower === -1) { |
| 84 | + console.log(`\u{1F534}keep because getFollower error, uid: ${uid}`); |
| 85 | + return; |
| 86 | + } |
| 87 | + if (follower < MIN_FOLLOWER) { |
| 88 | + logMessages += `\u{1F7E2}remove because follower < ${MIN_FOLLOWER}, uid: ${uid}, follower: ${follower} |
| 89 | +`; |
| 90 | + removeCard(card); |
| 91 | + return; |
| 92 | + } |
| 93 | + } |
| 94 | + __name(editCards, "editCards"); |
| 95 | + function removeCard(card) { |
| 96 | + card.remove(); |
| 97 | + } |
| 98 | + __name(removeCard, "removeCard"); |
| 99 | + function removeIfBlockByADBlocker(card) { |
| 100 | + const cardContent = card.querySelector(".bili-video-card.is-rcmd"); |
| 101 | + if (!cardContent || cardContent.innerHTML.match( |
| 102 | + /<!----><div class=".+?"><\/div><!---->/ |
| 103 | + )) { |
| 104 | + removeCard(card); |
| 105 | + return true; |
| 106 | + } |
| 107 | + return false; |
| 108 | + } |
| 109 | + __name(removeIfBlockByADBlocker, "removeIfBlockByADBlocker"); |
| 110 | + var isProcessing = false; |
| 111 | + var observer = new IntersectionObserver( |
| 112 | + (entries, obs) => { |
| 113 | + entries.forEach((entry) => { |
| 114 | + if (entry.isIntersecting) { |
| 115 | + editCards(entry.target); |
| 116 | + obs.unobserve(entry.target); |
| 117 | + } |
| 118 | + }); |
| 119 | + }, |
| 120 | + { rootMargin: "0px", threshold: 0.2 } |
| 121 | + ); |
| 122 | + function observeNewCards() { |
| 123 | + const blockCards = document.querySelectorAll( |
| 124 | + FILTER_BLOCK_CLASSES.join(", ") |
| 125 | + ); |
| 126 | + blockCards.forEach((card) => { |
| 127 | + removeCard(card); |
| 128 | + }); |
| 129 | + const filterCards = document.querySelectorAll(FILTER_CLASSES.join(", ")); |
| 130 | + filterCards.forEach((card) => { |
| 131 | + if (removeIfBlockByADBlocker(card)) return; |
| 132 | + if (card.dataset.processed) return; |
| 133 | + observer.observe(card); |
| 134 | + card.dataset.processed = "true"; |
| 135 | + }); |
| 136 | + } |
| 137 | + __name(observeNewCards, "observeNewCards"); |
| 138 | + var mutationObserver = new MutationObserver((mutations) => { |
| 139 | + if (isProcessing) return; |
| 140 | + isProcessing = true; |
| 141 | + logMessages += `\u{1F913}mutationObserver, mutations: ${mutations.length} |
| 142 | +`; |
| 143 | + mutations.forEach((mutation) => { |
| 144 | + if (mutation.type === "childList") { |
| 145 | + observeNewCards(); |
| 146 | + } |
| 147 | + }); |
| 148 | + isProcessing = false; |
| 149 | + }); |
| 150 | + var container = document.querySelector(".container.is-version8"); |
| 151 | + if (container) { |
| 152 | + mutationObserver.observe(container, { |
| 153 | + childList: true |
| 154 | + }); |
| 155 | + } |
| 156 | + observeNewCards(); |
| 157 | + var logMessages = ""; |
| 158 | + setInterval(() => { |
| 159 | + if (logMessages === "") return; |
| 160 | + console.log(logMessages); |
| 161 | + logMessages = ""; |
| 162 | + }, 1e4); |
| 163 | +})(); |
0 commit comments