🏠 返回首頁 

Greasy Fork is available in English.

bkBliBli

哔哩哔哩首页快捷拉黑,去广告,直播推广


Installer dette script?
  1. // ==UserScript==
  2. // @name bkBliBli
  3. // @github https://github.com/hunmeng/bkBilibili
  4. // @description 哔哩哔哩首页快捷拉黑,去广告,直播推广
  5. // @author lkj
  6. // @namespace lkj
  7. // @version 1.0.7
  8. // @create 2024-05-06
  9. // @lastmodified 2025-01-09
  10. // @note 首次更新,拉黑功能覆盖到首页分区
  11. // @charset UTF-8
  12. // @match *://www.bilibili.com/
  13. // @match *://www.bilibili.com/?*
  14. // @match *://www.bilibili.com/v/*
  15. // @match https://api.bilibili.com/x/relation/modify
  16. // @grant unsafeWindow
  17. // @compatible chrome
  18. // @license MIT
  19. // ==/UserScript==
  20. (async function() {
  21. 'use strict';
  22. const bili_jct = (await cookieStore.get('bili_jct')).value;
  23. console.log('bili_jct=======',bili_jct)
  24. // 添加自定义CSS样式
  25. var style = document.createElement('style');
  26. style.type = 'text/css';
  27. // 设置a标签的样式
  28. style.innerHTML = '.custom-link { margin-left: auto; }.feed-card { margin-top: 0px !important; margin-bottom: 0px !important; }.bili-video-card{ margin-top: 5px !important; margin-bottom: 20px !important; }.bili-video-card.is-rcmd.enable-no-interest { margin-top: 5px !important; margin-bottom: 20x !important; }';
  29. document.head.appendChild(style);
  30. async function addToBlack(uid,act) {
  31. return fetch('https://api.bilibili.com/x/relation/modify', {
  32. method: 'POST',
  33. headers: {
  34. 'Content-Type': 'application/x-www-form-urlencoded'
  35. },
  36. credentials: 'include',
  37. body: `fid=${uid}&act=${act}&csrf=${bili_jct}`
  38. })
  39. .then(async res => void console.log('请求成功, 响应体: ', await res.text()))
  40. .catch(err => void console.error('请求错误', err));
  41. }
  42. // 函数用于排序类名数组并返回字符串
  43. function sortClassNames(classList) {
  44. if (typeof classList === 'string') {
  45. return classList;
  46. } else if (classList === undefined) {
  47. // 处理未定义的情况
  48. //console.warn('classList is undefined');
  49. return '';
  50. } else {
  51. // 处理其他非字符串情况
  52. console.error('Invalid classList:', classList);
  53. return classList;
  54. }
  55. }
  56. // 定义要删除的类名集合
  57. var classNamesToRemove = [
  58. 'bili-video-card is-rcmd',
  59. 'floor-single-card',
  60. 'bili-live-card is-rcmd enable-no-interest',
  61. 'recommended-swipe grid-anchor'
  62. ];
  63. function reEl(childNode){
  64. var classNames = sortClassNames(childNode.className);
  65. // 检查子节点的类名是否包含要删除的类名
  66. if (classNamesToRemove.some(cn => classNames == cn)) {
  67. childNode.remove();
  68. return false;
  69. } else if (classNames == 'feed-card') { // 如果子节点的类名包含 'feed-card'
  70. // 获取 feed-card 的子节点
  71. var feedCardChildren = childNode.children;
  72. var isRe = false;
  73. // 函数用于检查子节点的类名是否包含要删除的类名
  74. function hasClassToRemove(child) {
  75. var childNames = sortClassNames(child.className);
  76. return classNamesToRemove.some(cn => childNames == cn);
  77. }
  78. // 遍历 feed-card 的子节点,检查是否包含要删除的类名
  79. for (var j = 0; j < feedCardChildren.length; j++) {
  80. var feedCardChild = feedCardChildren[j];
  81. // 如果子节点的类名包含要删除的类名,则设置 isRe 为 true 并结束循环
  82. if (hasClassToRemove(feedCardChild)) {
  83. isRe = true;
  84. break;
  85. }
  86. }
  87. if(isRe){
  88. childNode.remove();
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. // 处理视频条目函数
  95. function handleVideoItems(videoItems) {
  96. // 正则表达式匹配数字ID的模式
  97. var regex = /\/(\d+)$/;
  98. // 遍历每个视频条目
  99. videoItems.forEach(function(item) {
  100. // 查找带有类名为 "bili-video-card__info--owner" 的元素
  101. var ownerLink = item.querySelector('.bili-video-card__info--owner');
  102. var idtext = '';
  103. // 检查是否找到了链接元素
  104. if (ownerLink) {
  105. // 获取链接的 href 属性值
  106. var hrefValue = ownerLink.getAttribute('href');
  107. // 使用正则表达式匹配数字ID
  108. var match = hrefValue.match(regex);
  109. if (match) {
  110. idtext = match[1]; // 提取匹配到的数字ID
  111. } else {
  112. console.error('未能匹配到数字ID');
  113. }
  114. }
  115. // 检查是否已经添加过“拉黑”链接
  116. if (!item.classList.contains('blacklisted')) {
  117. // 创建一个a标签元素
  118. var link = document.createElement('a');
  119. link.textContent = '拉黑'; // 链接显示的文本
  120. link.href = '#'; // 链接的目标URL
  121. // 添加自定义类以应用样式
  122. link.classList.add('custom-link');
  123. // 添加点击事件处理程序
  124. var isBlacklisted = false; // 拉黑状态,默认为 false
  125. link.addEventListener('click', function(event) {
  126. // 阻止默认行为,例如不跳转到链接的href
  127. event.preventDefault();
  128. if (!isBlacklisted) {
  129. // 如果未拉黑,则执行拉黑操作
  130. addToBlack(idtext, 5);
  131. link.textContent = '取消拉黑'; // 修改链接文本
  132. isBlacklisted = true; // 更新状态为已拉黑
  133. } else {
  134. // 如果已拉黑,则执行取消拉黑操作
  135. addToBlack(idtext, 6);
  136. link.textContent = '拉黑'; // 修改链接文本
  137. isBlacklisted = false; // 更新状态为未拉黑
  138. }
  139. });
  140. // 将链接添加到视频条目中
  141. item.appendChild(link);
  142. // 添加已处理标记
  143. item.classList.add('blacklisted');
  144. }
  145. });
  146. }
  147. // 创建一个 MutationObserver 实例,监听 DOM 变化
  148. var observer = new MutationObserver(function(mutations) {
  149. mutations.forEach(function(mutation) {
  150. // 检查每个变化是否是节点的添加
  151. mutation.addedNodes.forEach(function(addedNode) {
  152. //处理广告/推广等视频推广
  153. if(reEl(addedNode)){
  154. // 如果添加的节点是你想要的视频条目元素,则执行相应的操作
  155. if (addedNode.querySelectorAll && addedNode.querySelectorAll('.bili-video-card__info--bottom').length > 0) {
  156. // 获取所有新增的视频条目的元素
  157. var newVideoItems = addedNode.querySelectorAll('.bili-video-card__info--bottom');
  158. // 处理新增的视频条目
  159. handleVideoItems(newVideoItems);
  160. }
  161. }
  162. });
  163. });
  164. });
  165. // 监听指定元素的变化
  166. var targetElement = document.querySelector('.container.is-version8') || document.querySelector('.channel-layout');
  167. if (!targetElement) {
  168. console.error('未找到指定元素');
  169. return;
  170. }
  171. // 配置 MutationObserver,指定要观察的节点和要观察的变化类型
  172. var config = {
  173. childList: true, subtree: true
  174. };
  175. // 将观察器绑定到指定元素上,并开始观察
  176. observer.observe(targetElement, config);
  177. // 获取所有子节点,并使用箭头函数遍历处理每个子节点
  178. Array.from(targetElement.children).forEach(childNode => reEl(childNode));
  179. // 处理初始视频条目
  180. var initialVideoItems = targetElement.querySelectorAll('.bili-video-card__info--bottom');
  181. //handleVideoItems(initialVideoItems);
  182. setTimeout(() => {
  183. handleVideoItems(initialVideoItems);
  184. }, 300);
  185. //test
  186. })();