Greasy Fork 还支持 简体中文。

bilibili script

bilibili网站操作脚本

  1. // ==UserScript==
  2. // @name bilibili script
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.36.1
  5. // @description bilibili网站操作脚本
  6. // @author You
  7. // @match https://www.bilibili.com/
  8. // @match https://search.bilibili.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. "use strict";
  16.  
  17. // Your code here...
  18. // bilibili网站加载成功后执行,去除一些模块
  19. window.onload = function () {
  20. console.log(
  21. "%csuccess%c:",
  22. "background:#47c00e;padding:3px;border-radius:3px",
  23. "",
  24. "脚本加载成功"
  25. );
  26. //方法内容
  27. let currentUrl = window.location.href;
  28. if (currentUrl.indexOf("https://www.bilibili.com/") == 0) {
  29. // 移除首页多余卡片
  30. RemoveCard();
  31. // 执行快捷键刷新和数字键跳转视频操作
  32. RefreshRecommend();
  33. }
  34. if (currentUrl.indexOf("https://search.bilibili.com/") == 0) {
  35. // 执行快捷键刷新和数字键跳转视频操作
  36. searchPageSelectCard();
  37. }
  38. };
  39. function RemoveCard() {
  40. let section_index = [
  41. 6, 7, 8, 9, 10, 11, 13, 15, 16, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  42. 30, 31, 32, 33,
  43. ];
  44. let documentary = document.querySelectorAll(".bili-grid");
  45. // console.log('纪录片',documentary);
  46. for (let i = 0; i < section_index.length; i++) {
  47. documentary[section_index[i]].remove();
  48. // documentary[section_index[i]].style.display="none";
  49. }
  50. let bili_footer = document.querySelectorAll(".bili-footer");
  51. // console.log('bili_footer',bili_footer);
  52. bili_footer[0].remove();
  53. }
  54. // 快捷键,快捷刷新推荐视频
  55. function RefreshRecommend() {
  56. // alt+r按键监听
  57. document.addEventListener("keydown", function (event) {
  58. // 检测到光标聚焦于输入框时,则不触发快捷键事件。
  59. if (
  60. ["input", "textarea"].indexOf(event.target.tagName.toLowerCase()) > -1
  61. ) {
  62. return false;
  63. }
  64. if (event.altKey && event.keyCode == 82) {
  65. event.preventDefault();
  66. // console.log('alt+r');
  67. // 快捷键按下后,执行的方法
  68. let refresh_button =
  69. document.querySelectorAll(".roll-btn-wrap")[0].children[0];
  70. refresh_button.click();
  71. }
  72. // 监听数字键盘
  73. if (event.keyCode >= 48 && event.keyCode <= 57 && !event.ctrlKey) {
  74. event.preventDefault();
  75. let num = event.keyCode - 48 > 0 ? event.keyCode - 48 : 10;
  76. // 获取推荐视频的卡片
  77. try {
  78. let recommend_video = document.querySelectorAll(
  79. ".recommend-container__2-line"
  80. )[0].children[num].children[1].children[0];
  81. // 点击相应卡片
  82. recommend_video.click();
  83. return;
  84. } catch (error) {
  85. console.log(
  86. "%cerror%c:",
  87. "background:#ff0000;padding:3px;border-radius:3px",
  88. "",
  89. '未获取到元素'
  90. );
  91. }
  92. }
  93. });
  94. }
  95. function searchPageSelectCard() {
  96. document.addEventListener("keydown", function (event) {
  97. // 检测到光标聚焦于输入框时,则不触发快捷键事件。
  98. if (
  99. ["input", "textarea"].indexOf(event.target.tagName.toLowerCase()) > -1
  100. ) {
  101. return false;
  102. }
  103. // 监听数字键盘
  104. if (event.keyCode >= 48 && event.keyCode <= 57 && !event.ctrlKey) {
  105. event.preventDefault();
  106. let num = event.keyCode - 48 > 0 ? event.keyCode - 48 : 10;
  107. // 获取搜索页面的视频卡片
  108. try {
  109. let search_video = document.querySelectorAll(
  110. ".bili-video-card__wrap"
  111. )[num].children[0];
  112. // 点击相应卡片
  113. search_video.click();
  114. } catch (error) {
  115. console.log(
  116. "%cerror%c:",
  117. "background:#ff0000;padding:3px;border-radius:3px",
  118. "",
  119. error
  120. );
  121. }
  122. }
  123. });
  124. }
  125. })();