哔哩哔哩(B站|Bilibili)收藏夹Fix (检测隐藏视频)

检测收藏夹中被UP主设置为仅自己可见的视频

目前為 2025-03-15 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name bilibili favlist hidden video detection
  3. // @name:zh-CN 哔哩哔哩(B站|Bilibili)收藏夹Fix (检测隐藏视频)
  4. // @name:zh-TW 哔哩哔哩(B站|Bilibili)收藏夹Fix (检测隐藏视频)
  5. // @namespace http://tampermonkey.net/
  6. // @version 10
  7. // @description detect videos in favlist that only visiable to upper
  8. // @description:zh-CN 检测收藏夹中被UP主设置为仅自己可见的视频
  9. // @description:zh-TW 检测收藏夹中被UP主设置为仅自己可见的视频
  10. // @author YTB0710
  11. // @match https://space.bilibili.com/*
  12. // @connect bilibili.com
  13. // @grant GM_openInTab
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @grant GM_xmlhttpRequest
  17. // @grant GM_cookie
  18. // ==/UserScript==
  19.  
  20. (function () {
  21. 'use strict';
  22.  
  23. const AVRegex = /^[1-9]\d*$/;
  24. const BVRegex = /^BV[A-Za-z0-9]{10}$/;
  25. const startsWithAVRegex = /^av/i;
  26. const favlistURLRegex = /https:\/\/space\.bilibili\.com\/\d+\/favlist.*/;
  27. const getFidFromURLRegex = /fid=(\d+)/;
  28. const getUIDFromURLRegex = /https:\/\/space\.bilibili\.com\/(\d+)/;
  29. const getBVFromURLRegex = /video\/(\w+)/;
  30.  
  31. let onFavlistPage = false;
  32. let newFreshSpace;
  33. let classAppendNewFreshSpace;
  34. let pageSize;
  35. let divMessageHeightFixed = false;
  36. let order = 'mtime';
  37.  
  38. const settings = GM_getValue('settings', {
  39. apiURL: 1,
  40. apiURL1DetectionScope: 'page',
  41. autoClearMessage: true,
  42. });
  43.  
  44. if (GM_getValue('detectionScope', '')) {
  45. settings.apiURL1DetectionScope = GM_getValue('detectionScope', '');
  46. GM_setValue('detectionScope', '');
  47. GM_setValue('settings', settings);
  48. }
  49.  
  50. const sideObserver = new MutationObserver((_mutations, observer) => {
  51. if (document.querySelector('div.favlist-aside')) {
  52. observer.disconnect();
  53. newFreshSpace = true;
  54. classAppendNewFreshSpace = '-newFreshSpace';
  55. pageSize = window.innerWidth < 1760 ? 40 : 36;
  56. main();
  57. radioFilterObserver.observe(document.querySelector('div.fav-list-header-filter__left > div'), { subtree: true, characterData: false, attributeFilter: ['class'] });
  58. headerFilterLeftChildListObserver.observe(document.querySelector('div.fav-list-header-filter__left'), { childList: true, attributes: false, characterData: false });
  59. return;
  60. }
  61. if (document.querySelector('div.fav-sidenav')) {
  62. observer.disconnect();
  63. newFreshSpace = false;
  64. classAppendNewFreshSpace = '';
  65. pageSize = 20;
  66. main();
  67. return;
  68. }
  69. });
  70.  
  71. const radioFilterObserver = new MutationObserver(mutations => {
  72. for (const mutation of mutations) {
  73. if (mutation.target.classList.contains('radio-filter__item--active')) {
  74. const orderText = mutation.target.innerText;
  75. if (orderText.includes('收藏')) {
  76. order = 'mtime';
  77. } else if (orderText.includes('播放')) {
  78. order = 'view';
  79. } else if (orderText.includes('投稿')) {
  80. order = 'pubtime';
  81. } else {
  82. addMessage('无法确定各个视频的排序方式, 请反馈该问题', false, true);
  83. }
  84. }
  85. }
  86. });
  87.  
  88. const headerFilterLeftChildListObserver = new MutationObserver(mutations => {
  89. for (const mutation of mutations) {
  90. for (const addedNode of mutation.addedNodes) {
  91. if (addedNode.nodeType === 1 && addedNode.classList.contains('radio-filter')) {
  92. order = 'mtime';
  93. radioFilterObserver.observe(addedNode, { subtree: true, characterData: false, attributeFilter: ['class'] });
  94. }
  95. }
  96. for (const removedNode of mutation.removedNodes) {
  97. if (removedNode.nodeType === 1 && removedNode.classList.contains('radio-filter')) {
  98. radioFilterObserver.disconnect();
  99. }
  100. }
  101. }
  102. });
  103.  
  104. checkURL();
  105.  
  106. const originalPushState = history.pushState;
  107. history.pushState = function (...args) {
  108. originalPushState.apply(this, args);
  109. checkURL();
  110. };
  111.  
  112. const originalReplaceState = history.replaceState;
  113. history.replaceState = function (...args) {
  114. originalReplaceState.apply(this, args);
  115. checkURL();
  116. };
  117.  
  118. window.addEventListener('popstate', checkURL);
  119.  
  120. function checkURL() {
  121. if (favlistURLRegex.test(location.href)) {
  122. if (!onFavlistPage) {
  123. onFavlistPage = true;
  124. sideObserver.observe(document.body, { subtree: true, childList: true, attributes: false, characterData: false });
  125. }
  126. } else {
  127. if (onFavlistPage) {
  128. onFavlistPage = false;
  129. sideObserver.disconnect();
  130. }
  131. }
  132. }
  133.  
  134. function main() {
  135.  
  136. const style = document.createElement('style');
  137. style.textContent = `
  138. .detect-div-first {
  139. padding: 2px;
  140. }
  141. .detect-div-first-newFreshSpace {
  142. padding: 2px 0;
  143. }
  144. .detect-div-second {
  145. padding: 2px 0 2px 16px;
  146. }
  147. .detect-div-second-newFreshSpace {
  148. padding: 2px 0 2px 16px;
  149. }
  150. .detect-inputText, .detect-inputText-newFreshSpace {
  151. box-sizing: content-box;
  152. border: 1px solid #cccccc;
  153. line-height: 1;
  154. }
  155. .detect-inputText {
  156. width: 140px;
  157. height: 14px;
  158. border-radius: 2px;
  159. padding: 2px;
  160. font-size: 14px;
  161. }
  162. .detect-inputText-newFreshSpace {
  163. width: 160px;
  164. height: 16px;
  165. border-radius: 3px;
  166. padding: 3px;
  167. font-size: 16px;
  168. }
  169. .detect-button, .detect-button-newFreshSpace {
  170. border: 1px solid #cccccc;
  171. line-height: 1;
  172. cursor: pointer;
  173. }
  174. .detect-button {
  175. border-radius: 2px;
  176. padding: 2px;
  177. font-size: 14px;
  178. }
  179. .detect-button-newFreshSpace {
  180. border-radius: 3px;
  181. padding: 3px;
  182. font-size: 16px;
  183. }
  184. .detect-label, .detect-label-newFreshSpace {
  185. line-height: 1;
  186. }
  187. .detect-disabled, .detect-disabled-newFreshSpace {
  188. opacity: 0.5;
  189. pointer-events: none;
  190. }
  191. .detect-divMessage, .detect-divMessage-newFreshSpace {
  192. overflow-y: auto;
  193. background-color: #eeeeee;
  194. line-height: 1.5;
  195. scrollbar-width: none;
  196. }
  197. .detect-divMessage {
  198. margin: 2px;
  199. }
  200. .detect-divMessage-heightFixed {
  201. height: 350px;
  202. }
  203. .detect-divMessage::-webkit-scrollbar {
  204. display: none;
  205. }
  206. .detect-divMessage-newFreshSpace {
  207. margin: 2px 0;
  208. }
  209. .detect-divMessage-heightFixed-newFreshSpace {
  210. height: 400px;
  211. }
  212. .detect-divMessage-newFreshSpace::-webkit-scrollbar {
  213. display: none;
  214. }
  215. `;
  216. document.head.appendChild(style);
  217.  
  218. const divSide = document.querySelector(newFreshSpace ? 'div.favlist-aside' : 'div.fav-sidenav');
  219. if (!newFreshSpace && divSide.querySelector('a.watch-later')) {
  220. divSide.querySelector('a.watch-later').style.borderBottom = '1px solid #eeeeee';
  221. }
  222.  
  223. const divControls = document.createElement('div');
  224. divControls.classList.add('detect-div-first' + classAppendNewFreshSpace);
  225. if (!newFreshSpace) {
  226. divControls.style.borderTop = '1px solid #e4e9f0';
  227. }
  228. divSide.appendChild(divControls);
  229.  
  230. const divInputTextAVBV = document.createElement('div');
  231. divInputTextAVBV.classList.add('detect-div-first' + classAppendNewFreshSpace);
  232. divControls.appendChild(divInputTextAVBV);
  233.  
  234. const inputTextAVBV = document.createElement('input');
  235. inputTextAVBV.type = 'text';
  236. inputTextAVBV.classList.add('detect-inputText' + classAppendNewFreshSpace);
  237. inputTextAVBV.placeholder = '输入AV号或BV号';
  238. divInputTextAVBV.appendChild(inputTextAVBV);
  239.  
  240. const divButtonDetect = document.createElement('div');
  241. divButtonDetect.classList.add('detect-div-first' + classAppendNewFreshSpace);
  242. divButtonDetect.setAttribute('title',
  243. '接口1和接口2在未来可能会失效。');
  244. divControls.appendChild(divButtonDetect);
  245.  
  246. const buttonDetect = document.createElement('button');
  247. buttonDetect.type = 'button';
  248. buttonDetect.classList.add('detect-button' + classAppendNewFreshSpace);
  249. buttonDetect.textContent = '检测隐藏视频';
  250. buttonDetect.addEventListener('click', async () => {
  251. try {
  252. if (settings.autoClearMessage) {
  253. clearMessage();
  254. }
  255.  
  256. let currentFavlist;
  257. if (newFreshSpace) {
  258. currentFavlist = document.querySelector('div.vui_sidebar-item--active');
  259. if (!document.querySelector('div.fav-collapse').contains(currentFavlist)) {
  260. throw ['不支持处理特殊收藏夹'];
  261. }
  262. } else {
  263. currentFavlist = document.querySelector('.fav-item.cur');
  264. if (!document.querySelector('div.nav-container').contains(currentFavlist)) {
  265. throw ['不支持处理特殊收藏夹'];
  266. }
  267. }
  268.  
  269. let fid;
  270. if (newFreshSpace) {
  271. const getFidFromURLMatch = location.href.match(getFidFromURLRegex);
  272. if (getFidFromURLMatch) {
  273. fid = parseInt(getFidFromURLMatch[1], 10);
  274. } else {
  275. throw ['无法获取当前收藏夹的fid, 刷新页面可能有帮助'];
  276. }
  277. } else {
  278. fid = document.querySelector('.fav-item.cur').getAttribute('fid');
  279. }
  280.  
  281. let pageNumber;
  282. if (newFreshSpace) {
  283. const pagenation = document.querySelector('button.vui_pagenation--btn-num.vui_button--active');
  284. if (!pagenation) {
  285. pageNumber = 1;
  286. } else {
  287. pageNumber = parseInt(pagenation.innerText, 10);
  288. }
  289. } else {
  290. pageNumber = parseInt(document.querySelector('li.be-pager-item-active > a').innerText, 10);
  291. }
  292.  
  293. if (settings.apiURL === 1) {
  294. const defaultFilter = await appendParamsForApiURL2(fid, 1, 20) === `https://api.bilibili.com/medialist/gateway/base/spaceDetail?media_id=${fid}&pn=1&ps=20&keyword=&order=mtime&type=0&tid=0&jsonp=jsonp`;
  295.  
  296. if (settings.apiURL1DetectionScope === 'page') {
  297. if (!defaultFilter) {
  298. throw ['请刷新页面, 恢复默认的排序方式和筛选条件'];
  299. }
  300. if ((pageNumber - 1) * pageSize > 999) {
  301. throw ['仅能获取按最近收藏排序的前1000个视频'];
  302. }
  303.  
  304. const response = await new Promise((resolve, reject) => {
  305. GM.xmlHttpRequest({
  306. method: 'GET',
  307. url: `https://api.bilibili.com/x/v3/fav/resource/ids?media_id=${fid}&platform=web`,
  308. timeout: 5000,
  309. responseType: 'json',
  310. onload: (res) => resolve(res),
  311. onerror: (res) => reject(['请求失败', 'api.bilibili.com/x/v3/fav/resource/ids', res.error]),
  312. ontimeout: () => reject(['请求超时', 'api.bilibili.com/x/v3/fav/resource/ids'])
  313. });
  314. });
  315. const AVBVsFavlistAll = response.response.data;
  316.  
  317. const sliceStart = (pageNumber - 1) * pageSize;
  318. const AVBVsPageAll = AVBVsFavlistAll.slice(sliceStart, sliceStart + pageSize);
  319.  
  320. const videosPageVisable = document.querySelectorAll(newFreshSpace ? 'div.items__item' : 'li.small-item');
  321. let BVsPageVisable;
  322. if (newFreshSpace) {
  323. BVsPageVisable = Array.from(videosPageVisable).map(el => el.querySelector('a').getAttribute('href').match(getBVFromURLRegex)[1]);
  324. } else {
  325. BVsPageVisable = Array.from(videosPageVisable).map(el => el.getAttribute('data-aid'));
  326. }
  327.  
  328. const AVBVsPageHidden = AVBVsPageAll.filter(el => !BVsPageVisable.includes(el.bvid) && el.type === 2);
  329. if (!AVBVsPageHidden.length) {
  330. addMessage('没有找到隐藏的视频', false, 'green');
  331. return;
  332. }
  333. AVBVsPageHidden.forEach(el => {
  334. addMessage(`在当前页的位置: ${AVBVsPageAll.findIndex(ele => ele.bvid === el.bvid) + 1}`, false, 'green');
  335. addMessage(`AV号: ${el.id}`);
  336. addMessage(`BV号: ${el.bvid}`);
  337. });
  338.  
  339. } else {
  340. const response = await new Promise((resolve, reject) => {
  341. GM.xmlHttpRequest({
  342. method: 'GET',
  343. url: `https://api.bilibili.com/x/v3/fav/resource/ids?media_id=${fid}&platform=web`,
  344. timeout: 5000,
  345. responseType: 'json',
  346. onload: (res) => resolve(res),
  347. onerror: (res) => reject(['请求失败', 'api.bilibili.com/x/v3/fav/resource/ids', res.error]),
  348. ontimeout: () => reject(['请求超时', 'api.bilibili.com/x/v3/fav/resource/ids'])
  349. });
  350. });
  351. const AVBVsFavlistAll = response.response.data;
  352.  
  353. let pageNumber = 1;
  354. let BVsFavlistVisable = [];
  355. while (true) {
  356. const response = await new Promise((resolve, reject) => {
  357. GM.xmlHttpRequest({
  358. method: 'GET',
  359. url: `https://api.bilibili.com/x/v3/fav/resource/list?media_id=${fid}&pn=${pageNumber}&ps=40&platform=web`,
  360. timeout: 5000,
  361. responseType: 'json',
  362. onload: (res) => resolve(res),
  363. onerror: (res) => reject(['请求失败', 'api.bilibili.com/x/v3/fav/resource/list', res.error]),
  364. ontimeout: () => reject(['请求超时', 'api.bilibili.com/x/v3/fav/resource/list'])
  365. });
  366. });
  367. if (!response.response.data.info.media_count) {
  368. addMessage('没有找到隐藏的视频', false, 'green');
  369. return;
  370. }
  371. if (response.response.data.medias) {
  372. BVsFavlistVisable.push(...response.response.data.medias.map(el => el.bvid));
  373. }
  374. addMessage(`已获取可见视频个数: ${BVsFavlistVisable.length}`, true);
  375. if (!response.response.data.has_more) {
  376. break;
  377. }
  378. if (pageNumber === 25) {
  379. addMessage('仅能获取按最近收藏排序的前1000个视频', false, 'red');
  380. break;
  381. }
  382. pageNumber++;
  383. }
  384.  
  385. const AVBVsFavlistHidden = AVBVsFavlistAll.filter(el => !BVsFavlistVisable.includes(el.bvid) && el.type === 2);
  386. if (!AVBVsFavlistHidden.length) {
  387. addMessage('没有找到隐藏的视频', false, 'green');
  388. return;
  389. }
  390. let count = 1;
  391. AVBVsFavlistHidden.forEach(el => {
  392. if (defaultFilter) {
  393. addMessage(`第 ${Math.floor(AVBVsFavlistAll.findIndex(ele => ele.bvid === el.bvid) / pageSize) + 1} 页:`, false, 'green');
  394. } else {
  395. addMessage(`第 ${count++} 个:`, false, 'green');
  396. }
  397. addMessage(`AV号: ${el.id}`);
  398. addMessage(`BV号: ${el.bvid}`);
  399. });
  400. }
  401.  
  402. } else {
  403. const newRequests = [];
  404. const oldPageStart = (pageNumber - 1) * pageSize;
  405. const oldPageEnd = oldPageStart + pageSize;
  406. let newPageNumber = Math.floor(oldPageStart / 20) + 1;
  407. while ((newPageNumber - 1) * 20 < oldPageEnd) {
  408. const newPageStart = (newPageNumber - 1) * 20;
  409. const newPageEnd = newPageStart + 20;
  410. const sliceStart = Math.max(oldPageStart, newPageStart);
  411. const sliceEnd = Math.min(oldPageEnd, newPageEnd);
  412. newRequests.push({ newPageNumber, sliceStart, sliceEnd });
  413. newPageNumber++;
  414. }
  415.  
  416. const InfosPageAll = [];
  417. for (const newRequest of newRequests) {
  418. const urlWithParams = await appendParamsForApiURL2(fid, newRequest.newPageNumber, 20);
  419. const response = await new Promise(async (resolve, reject) => {
  420. GM.xmlHttpRequest({
  421. method: 'GET',
  422. url: urlWithParams,
  423. timeout: 5000,
  424. responseType: 'json',
  425. onload: (res) => resolve(res),
  426. onerror: (res) => reject(['请求失败', 'api.bilibili.com/medialist/gateway/base/spaceDetail', res.error]),
  427. ontimeout: () => reject(['请求超时', 'api.bilibili.com/medialist/gateway/base/spaceDetail'])
  428. });
  429. });
  430.  
  431. if (response.response.code === -403 || response.response.code === 7201004) {
  432. throw ['不支持处理私密收藏夹'];
  433. } else if (response.response.code) {
  434. throw ['发生未知错误, 请反馈该问题', JSON.stringify(response.response)];
  435. }
  436.  
  437. if (!response.response.data.medias) {
  438. break;
  439. }
  440.  
  441. const sliceStart = newRequest.sliceStart - (newRequest.newPageNumber - 1) * 20;
  442. const sliceEnd = newRequest.sliceEnd - (newRequest.newPageNumber - 1) * 20;
  443. InfosPageAll.push(...response.response.data.medias.slice(sliceStart, sliceEnd));
  444. }
  445.  
  446. const videosPageVisable = document.querySelectorAll(newFreshSpace ? 'div.items__item' : 'li.small-item');
  447. let BVsPageVisable;
  448. if (newFreshSpace) {
  449. BVsPageVisable = Array.from(videosPageVisable).map(el => el.querySelector('a').getAttribute('href').match(getBVFromURLRegex)[1]);
  450. } else {
  451. BVsPageVisable = Array.from(videosPageVisable).map(el => el.getAttribute('data-aid'));
  452. }
  453.  
  454. const InfosPageHidden = InfosPageAll.filter(el => !BVsPageVisable.includes(el.bvid));
  455. if (!InfosPageHidden.length) {
  456. addMessage('没有找到隐藏的视频', false, 'green');
  457. return;
  458. }
  459. InfosPageHidden.forEach(el => {
  460. addMessage(`在当前页的位置: ${InfosPageAll.findIndex(ele => ele.bvid === el.bvid) + 1}`, false, 'green');
  461. addMessage(`AV号: ${el.id}`);
  462. addMessage(`BV号: ${el.bvid}`);
  463. addMessage(`简介: ${el.intro}`);
  464. addMessage(`UP主: <a href="https://space.bilibili.com/${el.upper.mid}" target="_blank" style="text-decoration-line: underline;">${el.upper.name}</a>`, true);
  465. addMessage(`上传时间: ${new Date(1000 * el.ctime).toLocaleString()}`, true);
  466. addMessage(`发布时间: ${new Date(1000 * el.pubtime).toLocaleString()}`, true);
  467. addMessage(`收藏时间: ${new Date(1000 * el.fav_time).toLocaleString()}`, true);
  468. if (Array.isArray(el.pages)) {
  469. el.pages.forEach(ele => {
  470. addMessage(`分集${ele.page}: cid: ${ele.id}`, true);
  471. addMessage(`标题: ${ele.title}`);
  472. });
  473. }
  474. });
  475. }
  476.  
  477. } catch (error) {
  478. if (error instanceof Error) {
  479. catchUnknownError(error);
  480. } else {
  481. addMessage(error[0], false, 'red');
  482. for (let i = 1; i < error.length; i++) {
  483. addMessage(error[i], true);
  484. }
  485. }
  486. }
  487. });
  488. divButtonDetect.appendChild(buttonDetect);
  489.  
  490. const divSwitchApiURL1 = document.createElement('div');
  491. divSwitchApiURL1.classList.add('detect-div-first' + classAppendNewFreshSpace);
  492. divSwitchApiURL1.setAttribute('title',
  493. '地址: https://api.bilibili.com/x/v3/fav/resource/ids?media_id={收藏夹fid}&platform=web\n' +
  494. '数据: 当前收藏夹内按最近收藏排序的前1000个视频的AV号和BV号');
  495. divControls.appendChild(divSwitchApiURL1);
  496.  
  497. const labelApiURL1 = document.createElement('label');
  498. labelApiURL1.classList.add('detect-label' + classAppendNewFreshSpace);
  499. labelApiURL1.textContent = '从接口1获取数据';
  500. divSwitchApiURL1.appendChild(labelApiURL1);
  501.  
  502. const radioApiURL1 = document.createElement('input');
  503. radioApiURL1.type = 'radio';
  504. radioApiURL1.name = 'apiURL';
  505. radioApiURL1.value = 1;
  506. radioApiURL1.checked = settings.apiURL === 1;
  507. radioApiURL1.addEventListener('change', () => {
  508. try {
  509. settings.apiURL = 1;
  510. divSwitchApiURL1DetectionScope1.classList.remove('detect-disabled' + classAppendNewFreshSpace);
  511. divSwitchApiURL1DetectionScope2.classList.remove('detect-disabled' + classAppendNewFreshSpace);
  512. divSwitchApiURL2DetectionScope1.classList.add('detect-disabled' + classAppendNewFreshSpace);
  513. GM_setValue('settings', settings);
  514. } catch (error) {
  515. catchUnknownError(error);
  516. }
  517. });
  518. labelApiURL1.insertAdjacentElement('afterbegin', radioApiURL1);
  519.  
  520. const divSwitchApiURL1DetectionScope1 = document.createElement('div');
  521. divSwitchApiURL1DetectionScope1.classList.add('detect-div-second' + classAppendNewFreshSpace);
  522. divSwitchApiURL1DetectionScope1.setAttribute('title',
  523. '请确保当前是默认的排序方式和筛选条件。\n' +
  524. '如果您刚刚在当前收藏夹添加或移除了视频, 请刷新页面后再使用此功能。\n' +
  525. '仅能获取按最近收藏排序的前1000个视频。如果某个隐藏视频在这个范围之外, 请将该视频之前的一些视频移出该收藏夹。\n' +
  526. '如果您正在使用的其他脚本或插件修改了视频封面和标题的链接地址, 请将其关闭后刷新页面再使用此功能。');
  527. divControls.appendChild(divSwitchApiURL1DetectionScope1);
  528.  
  529. const labelApiURL1DetectionScope1 = document.createElement('label');
  530. labelApiURL1DetectionScope1.classList.add('detect-label' + classAppendNewFreshSpace);
  531. labelApiURL1DetectionScope1.textContent = '检测当前页';
  532. divSwitchApiURL1DetectionScope1.appendChild(labelApiURL1DetectionScope1);
  533.  
  534. const radioApiURL1DetectionScope1 = document.createElement('input');
  535. radioApiURL1DetectionScope1.type = 'radio';
  536. radioApiURL1DetectionScope1.name = 'apiURL1DetectionScope';
  537. radioApiURL1DetectionScope1.value = 'page';
  538. radioApiURL1DetectionScope1.checked = settings.apiURL1DetectionScope === 'page';
  539. radioApiURL1DetectionScope1.addEventListener('change', () => {
  540. try {
  541. settings.apiURL1DetectionScope = 'page';
  542. GM_setValue('settings', settings);
  543. } catch (error) {
  544. catchUnknownError(error);
  545. }
  546. });
  547. labelApiURL1DetectionScope1.insertAdjacentElement('afterbegin', radioApiURL1DetectionScope1);
  548.  
  549. const divSwitchApiURL1DetectionScope2 = document.createElement('div');
  550. divSwitchApiURL1DetectionScope2.classList.add('detect-div-second' + classAppendNewFreshSpace);
  551. divSwitchApiURL1DetectionScope2.setAttribute('title',
  552. '仅能获取按最近收藏排序的前1000个视频。如果某个隐藏视频在这个范围之外, 请将该视频之前的一些视频移出该收藏夹。');
  553. divControls.appendChild(divSwitchApiURL1DetectionScope2);
  554.  
  555. const labelApiURL1DetectionScope2 = document.createElement('label');
  556. labelApiURL1DetectionScope2.classList.add('detect-label' + classAppendNewFreshSpace);
  557. labelApiURL1DetectionScope2.textContent = '检测当前收藏夹';
  558. divSwitchApiURL1DetectionScope2.appendChild(labelApiURL1DetectionScope2);
  559.  
  560. const radioApiURL1DetectionScope2 = document.createElement('input');
  561. radioApiURL1DetectionScope2.type = 'radio';
  562. radioApiURL1DetectionScope2.name = 'apiURL1DetectionScope';
  563. radioApiURL1DetectionScope2.value = 'favlist';
  564. radioApiURL1DetectionScope2.checked = settings.apiURL1DetectionScope === 'favlist';
  565. radioApiURL1DetectionScope2.addEventListener('change', () => {
  566. try {
  567. settings.apiURL1DetectionScope = 'favlist';
  568. GM_setValue('settings', settings);
  569. } catch (error) {
  570. catchUnknownError(error);
  571. }
  572. });
  573. labelApiURL1DetectionScope2.insertAdjacentElement('afterbegin', radioApiURL1DetectionScope2);
  574.  
  575. const divSwitchApiURL2 = document.createElement('div');
  576. divSwitchApiURL2.classList.add('detect-div-first' + classAppendNewFreshSpace);
  577. divSwitchApiURL2.setAttribute('title',
  578. '地址: https://api.bilibili.com/medialist/gateway/base/spaceDetail?media_id={收藏夹fid}&pn={页码}&ps={每页展示视频数量}\n' +
  579. '数据: AV号, BV号, 简介, UP主, 上传时间, 发布时间, 收藏时间, 每个分集的标题, cid\n' +
  580. '您需要将当前收藏夹设置为公开后才能获取到数据。如果收藏夹内第一个视频不是失效视频, 修改可见性会导致收藏夹的封面被固定为该视频的封面, 建议修改可见性之前先复制一个失效视频到当前收藏夹的首位。');
  581. divControls.appendChild(divSwitchApiURL2);
  582.  
  583. const labelApiURL2 = document.createElement('label');
  584. labelApiURL2.classList.add('detect-label' + classAppendNewFreshSpace);
  585. labelApiURL2.textContent = '从接口2获取数据';
  586. divSwitchApiURL2.appendChild(labelApiURL2);
  587.  
  588. const radioApiURL2 = document.createElement('input');
  589. radioApiURL2.type = 'radio';
  590. radioApiURL2.name = 'apiURL';
  591. radioApiURL2.value = 2;
  592. radioApiURL2.checked = settings.apiURL === 2;
  593. radioApiURL2.addEventListener('change', () => {
  594. try {
  595. settings.apiURL = 2;
  596. divSwitchApiURL2DetectionScope1.classList.remove('detect-disabled' + classAppendNewFreshSpace);
  597. divSwitchApiURL1DetectionScope1.classList.add('detect-disabled' + classAppendNewFreshSpace);
  598. divSwitchApiURL1DetectionScope2.classList.add('detect-disabled' + classAppendNewFreshSpace);
  599. GM_setValue('settings', settings);
  600. } catch (error) {
  601. catchUnknownError(error);
  602. }
  603. });
  604. labelApiURL2.insertAdjacentElement('afterbegin', radioApiURL2);
  605.  
  606. const divSwitchApiURL2DetectionScope1 = document.createElement('div');
  607. divSwitchApiURL2DetectionScope1.classList.add('detect-div-second' + classAppendNewFreshSpace);
  608. divSwitchApiURL2DetectionScope1.setAttribute('title',
  609. '如果您刚刚在当前收藏夹添加或移除了视频, 请刷新页面后再使用此功能。\n' +
  610. '如果您正在使用的其他脚本或插件修改了视频封面和标题的链接地址, 请将其关闭后刷新页面再使用此功能。');
  611. divControls.appendChild(divSwitchApiURL2DetectionScope1);
  612.  
  613. const labelApiURL2DetectionScope1 = document.createElement('label');
  614. labelApiURL2DetectionScope1.classList.add('detect-label' + classAppendNewFreshSpace);
  615. labelApiURL2DetectionScope1.textContent = '检测当前页';
  616. divSwitchApiURL2DetectionScope1.appendChild(labelApiURL2DetectionScope1);
  617.  
  618. const radioApiURL2DetectionScope1 = document.createElement('input');
  619. radioApiURL2DetectionScope1.type = 'radio';
  620. radioApiURL2DetectionScope1.name = 'apiURL2DetectionScope';
  621. radioApiURL2DetectionScope1.value = 'page';
  622. radioApiURL2DetectionScope1.checked = true;
  623. labelApiURL2DetectionScope1.insertAdjacentElement('afterbegin', radioApiURL2DetectionScope1);
  624.  
  625. if (settings.apiURL === 1) {
  626. divSwitchApiURL2DetectionScope1.classList.add('detect-disabled' + classAppendNewFreshSpace);
  627. } else {
  628. divSwitchApiURL1DetectionScope1.classList.add('detect-disabled' + classAppendNewFreshSpace);
  629. divSwitchApiURL1DetectionScope2.classList.add('detect-disabled' + classAppendNewFreshSpace);
  630. }
  631.  
  632. const divButtonJump = document.createElement('div');
  633. divButtonJump.classList.add('detect-div-first' + classAppendNewFreshSpace);
  634. divButtonJump.setAttribute('title',
  635. '在文本框内输入某个视频的BV号后, 点击此按钮, 将会跳转至该视频在各个第三方网站的页面。');
  636. divControls.appendChild(divButtonJump);
  637.  
  638. const buttonJump = document.createElement('button');
  639. buttonJump.type = 'button';
  640. buttonJump.classList.add('detect-button' + classAppendNewFreshSpace);
  641. buttonJump.textContent = '查询视频信息';
  642. buttonJump.addEventListener('click', () => {
  643. try {
  644. const BV = inputTextAVBV.value;
  645. if (!BVRegex.test(BV)) {
  646. throw ['请输入BV号'];
  647. }
  648. GM_openInTab(`https://www.biliplus.com/video/${BV}`, { active: true, insert: false, setParent: true });
  649. GM_openInTab(`https://xbeibeix.com/video/${BV}`, { insert: false, setParent: true });
  650. GM_openInTab(`https://www.jijidown.com/video/${BV}`, { insert: false, setParent: true });
  651.  
  652. } catch (error) {
  653. if (error instanceof Error) {
  654. catchUnknownError(error);
  655. } else {
  656. addMessage(error[0], false, 'red');
  657. for (let i = 1; i < error.length; i++) {
  658. addMessage(error[i], true);
  659. }
  660. }
  661. }
  662. });
  663. divButtonJump.appendChild(buttonJump);
  664.  
  665. const divButtonRemove = document.createElement('div');
  666. divButtonRemove.classList.add('detect-div-first' + classAppendNewFreshSpace);
  667. divButtonRemove.setAttribute('title',
  668. '在文本框内输入某个视频的AV号后, 点击此按钮, 将会从当前收藏夹中移除该视频。');
  669. divControls.appendChild(divButtonRemove);
  670.  
  671. const buttonRemove = document.createElement('button');
  672. buttonRemove.type = 'button';
  673. buttonRemove.classList.add('detect-button' + classAppendNewFreshSpace);
  674. buttonRemove.textContent = '取消收藏';
  675. buttonRemove.addEventListener('click', () => {
  676. try {
  677. GM_cookie.list({ name: 'bili_jct' }, async (cookies, error) => {
  678. if (error) {
  679. throw ['无法读取cookie, 更新Tampermonkey可能有帮助'];
  680. }
  681.  
  682. try {
  683. let AV = inputTextAVBV.value;
  684. if (startsWithAVRegex.test(AV)) {
  685. AV = AV.slice(2);
  686. }
  687. if (!AVRegex.test(AV)) {
  688. throw ['请输入AV号'];
  689. }
  690.  
  691. let fid;
  692. if (newFreshSpace) {
  693. const getFidFromURLMatch = location.href.match(getFidFromURLRegex);
  694. if (getFidFromURLMatch) {
  695. fid = parseInt(getFidFromURLMatch[1], 10);
  696. } else {
  697. throw ['无法获取当前收藏夹的fid, 刷新页面可能有帮助'];
  698. }
  699. } else {
  700. fid = document.querySelector('.fav-item.cur').getAttribute('fid');
  701. }
  702.  
  703. const csrf = cookies[0].value;
  704. const data = `resources=${AV}%3A2&media_id=${fid}&platform=web&csrf=${csrf}`;
  705. const response = await new Promise((resolve, reject) => {
  706. GM.xmlHttpRequest({
  707. method: 'POST',
  708. url: 'https://api.bilibili.com/x/v3/fav/resource/batch-del',
  709. data: data,
  710. timeout: 5000,
  711. headers: {
  712. 'Content-Length': data.length,
  713. 'Content-Type': 'application/x-www-form-urlencoded'
  714. },
  715. onload: (res) => resolve(res),
  716. onerror: (res) => reject(['请求失败', 'api.bilibili.com/x/v3/fav/resource/batch-del', res.error]),
  717. ontimeout: () => reject(['请求超时', 'api.bilibili.com/x/v3/fav/resource/batch-del'])
  718. });
  719. });
  720. addMessage('B站接口响应内容:');
  721. addMessage(response.response, true);
  722.  
  723. } catch (error) {
  724. if (error instanceof Error) {
  725. catchUnknownError(error);
  726. } else {
  727. addMessage(error[0], false, 'red');
  728. for (let i = 1; i < error.length; i++) {
  729. addMessage(error[i], true);
  730. }
  731. }
  732. }
  733. });
  734.  
  735. } catch (error) {
  736. catchUnknownError(error);
  737. }
  738. });
  739. divButtonRemove.appendChild(buttonRemove);
  740.  
  741. const divButtonAdd = document.createElement('div');
  742. divButtonAdd.classList.add('detect-div-first' + classAppendNewFreshSpace);
  743. divButtonAdd.setAttribute('title',
  744. '在文本框内输入某个视频的AV号后, 点击此按钮, 将会添加该视频到当前收藏夹的首位。如果当前收藏夹是公开收藏夹, 将收藏夹分享到动态后, 就可以看到该视频的封面。');
  745. divControls.appendChild(divButtonAdd);
  746.  
  747. const buttonAdd = document.createElement('button');
  748. buttonAdd.type = 'button';
  749. buttonAdd.classList.add('detect-button' + classAppendNewFreshSpace);
  750. buttonAdd.textContent = '添加收藏';
  751. buttonAdd.addEventListener('click', () => {
  752. try {
  753. GM_cookie.list({ name: 'bili_jct' }, async (cookies, error) => {
  754. if (error) {
  755. throw ['无法读取cookie, 更新Tampermonkey可能有帮助'];
  756. }
  757.  
  758. try {
  759. let AV = inputTextAVBV.value;
  760. if (startsWithAVRegex.test(AV)) {
  761. AV = AV.slice(2);
  762. }
  763. if (!AVRegex.test(AV)) {
  764. throw ['请输入AV号'];
  765. }
  766.  
  767. let fid;
  768. if (newFreshSpace) {
  769. const getFidFromURLMatch = location.href.match(getFidFromURLRegex);
  770. if (getFidFromURLMatch) {
  771. fid = parseInt(getFidFromURLMatch[1], 10);
  772. } else {
  773. throw ['无法获取当前收藏夹的fid, 刷新页面可能有帮助'];
  774. }
  775. } else {
  776. fid = document.querySelector('.fav-item.cur').getAttribute('fid');
  777. }
  778.  
  779. const csrf = cookies[0].value;
  780. const data = `rid=${AV}&type=2&add_media_ids=${fid}&csrf=${csrf}`;
  781. const response = await new Promise((resolve, reject) => {
  782. GM.xmlHttpRequest({
  783. method: 'POST',
  784. url: 'https://api.bilibili.com/x/v3/fav/resource/deal',
  785. data: data,
  786. timeout: 5000,
  787. headers: {
  788. 'Content-Length': data.length,
  789. 'Content-Type': 'application/x-www-form-urlencoded'
  790. },
  791. onload: (res) => resolve(res),
  792. onerror: (res) => reject(['请求失败', 'api.bilibili.com/x/v3/fav/resource/deal', res.error]),
  793. ontimeout: () => reject(['请求超时', 'api.bilibili.com/x/v3/fav/resource/deal'])
  794. });
  795. });
  796. addMessage('B站接口响应内容:');
  797. addMessage(response.response, true);
  798.  
  799. } catch (error) {
  800. if (error instanceof Error) {
  801. catchUnknownError(error);
  802. } else {
  803. addMessage(error[0], false, 'red');
  804. for (let i = 1; i < error.length; i++) {
  805. addMessage(error[i], true);
  806. }
  807. }
  808. }
  809. });
  810.  
  811. } catch (error) {
  812. catchUnknownError(error);
  813. }
  814. });
  815. divButtonAdd.appendChild(buttonAdd);
  816.  
  817. const divLabelAutoClearMessage = document.createElement('div');
  818. divLabelAutoClearMessage.classList.add('detect-div-first' + classAppendNewFreshSpace);
  819. divLabelAutoClearMessage.setAttribute('title',
  820. '控制是否在每次检测隐藏视频之前清空提示信息。');
  821. divControls.appendChild(divLabelAutoClearMessage);
  822.  
  823. const labelAutoClearMessage = document.createElement('label');
  824. labelAutoClearMessage.classList.add('detect-label' + classAppendNewFreshSpace);
  825. labelAutoClearMessage.textContent = '自动清空提示信息';
  826. divLabelAutoClearMessage.appendChild(labelAutoClearMessage);
  827.  
  828. const checkboxAutoClearMessage = document.createElement('input');
  829. checkboxAutoClearMessage.type = 'checkbox';
  830. checkboxAutoClearMessage.checked = settings.autoClearMessage;
  831. checkboxAutoClearMessage.addEventListener('change', () => {
  832. try {
  833. settings.autoClearMessage = checkboxAutoClearMessage.checked;
  834. GM_setValue('settings', settings);
  835. } catch (error) {
  836. catchUnknownError(error);
  837. }
  838. });
  839. labelAutoClearMessage.insertAdjacentElement('afterbegin', checkboxAutoClearMessage);
  840.  
  841. const divMessage = document.createElement('div');
  842. divMessage.classList.add('detect-divMessage' + classAppendNewFreshSpace);
  843. divControls.appendChild(divMessage);
  844.  
  845. function addMessage(msg, smallFontSize, border) {
  846. let px;
  847. if (smallFontSize) {
  848. px = newFreshSpace ? 11 : 10;
  849. } else {
  850. px = newFreshSpace ? 13 : 12;
  851. }
  852. const p = document.createElement('p');
  853. p.innerHTML = msg;
  854. p.style.fontSize = `${px}px`;
  855. if (border === 'red') {
  856. p.style.borderTop = '1px solid #800000';
  857. } else if (border === 'green') {
  858. p.style.borderTop = '1px solid #008000';
  859. }
  860. divMessage.appendChild(p);
  861.  
  862. if (divMessageHeightFixed) {
  863. divMessage.scrollTop = divMessage.scrollHeight;
  864. } else {
  865. if (newFreshSpace) {
  866. if (divMessage.scrollHeight > 400) {
  867. divMessage.classList.add('detect-divMessage-heightFixed-newFreshSpace');
  868. divMessageHeightFixed = true;
  869. divMessage.scrollTop = divMessage.scrollHeight;
  870. }
  871. } else {
  872. if (divMessage.scrollHeight > 350) {
  873. divMessage.classList.add('detect-divMessage-heightFixed');
  874. divMessageHeightFixed = true;
  875. divMessage.scrollTop = divMessage.scrollHeight;
  876. }
  877. }
  878. }
  879.  
  880. divMessage.scrollIntoView({ behavior: 'instant', block: 'nearest' });
  881. }
  882.  
  883. function clearMessage() {
  884. while (divMessage.firstChild) {
  885. divMessage.removeChild(divMessage.firstChild);
  886. }
  887. divMessage.classList.remove('detect-divMessage-heightFixed' + classAppendNewFreshSpace);
  888. divMessageHeightFixed = false;
  889. }
  890.  
  891. function catchUnknownError(error) {
  892. addMessage('发生未知错误, 请反馈该问题', false, 'red');
  893. addMessage(error.stack, true);
  894. console.error(error);
  895. }
  896.  
  897. async function appendParamsForApiURL2(fid, pageNumber, pageSize) {
  898. const inputKeyword = document.querySelector(newFreshSpace ? 'input.fav-list-header-filter__search' : 'input.search-fav-input');
  899. let keyword = '';
  900. if (inputKeyword) {
  901. keyword = encodeURIComponent(inputKeyword.value);
  902. }
  903.  
  904. let divFilterOrder;
  905. let divTid;
  906.  
  907. if (!newFreshSpace) {
  908. const divDropdownFilterItems = document.querySelectorAll('div.fav-filters > div.be-dropdown.filter-item');
  909. if (divDropdownFilterItems.length === 2) {
  910. divFilterOrder = divDropdownFilterItems[1].querySelector('span');
  911. divTid = divDropdownFilterItems[0].querySelector('span');
  912. } else {
  913. divFilterOrder = divDropdownFilterItems[0].querySelector('span');
  914. divTid = null;
  915. }
  916. }
  917.  
  918. if (!newFreshSpace) {
  919. let orderText = '收藏';
  920. if (divFilterOrder) {
  921. orderText = divFilterOrder.innerText;
  922. }
  923. if (orderText.includes('收藏')) {
  924. order = 'mtime';
  925. } else if (orderText.includes('播放')) {
  926. order = 'view';
  927. } else if (orderText.includes('投稿')) {
  928. order = 'pubtime';
  929. } else {
  930. throw ['无法确定各个视频的排序方式, 请反馈该问题'];
  931. }
  932. }
  933.  
  934. const divType = document.querySelector(newFreshSpace ? 'div.vui_input__prepend' : 'div.search-types');
  935. let typeText = '当前';
  936. if (divType) {
  937. typeText = divType.innerText;
  938. }
  939. if (newFreshSpace && keyword === '') {
  940. typeText = '当前';
  941. }
  942. let type;
  943. if (typeText.includes('当前')) {
  944. type = 0;
  945. } else if (typeText.includes('全部')) {
  946. type = 1;
  947. } else {
  948. throw ['无法确定搜索的范围为当前收藏夹还是全部收藏夹, 请反馈该问题'];
  949. }
  950.  
  951. if (newFreshSpace) {
  952. divTid = document.querySelector('div.fav-list-header-collapse div.radio-filter__item--active');
  953. }
  954. let tidText = '全部分区';
  955. if (divTid) {
  956. tidText = divTid.innerText;
  957. }
  958. let tid;
  959. if (tidText.includes('全部')) {
  960. tid = 0;
  961. } else {
  962. const UID = parseInt(location.href.match(getUIDFromURLRegex)[1], 10);
  963. const response = await new Promise((resolve, reject) => {
  964. GM.xmlHttpRequest({
  965. method: 'GET',
  966. url: `https://api.bilibili.com/x/v3/fav/resource/partition?up_mid=${UID}&media_id=${fid}` + (newFreshSpace ? '&web_location=333.1387' : ''),
  967. timeout: 5000,
  968. responseType: 'json',
  969. onload: (res) => resolve(res),
  970. onerror: (res) => reject(['请求失败', 'api.bilibili.com/x/v3/fav/resource/partition', res.error]),
  971. ontimeout: () => reject(['请求超时', 'api.bilibili.com/x/v3/fav/resource/partition'])
  972. });
  973. });
  974.  
  975. const target = response.response.data.find(el => tidText.includes(el.name));
  976. if (target) {
  977. tid = target.tid;
  978. } else {
  979. throw ['无法确定选择的分区, 请反馈该问题'];
  980. }
  981. }
  982.  
  983. return (`https://api.bilibili.com/medialist/gateway/base/spaceDetail?media_id=${fid}&pn=${pageNumber}&ps=${pageSize}&keyword=${keyword}&order=${order}&type=${type}&tid=${tid}&jsonp=jsonp`);
  984. }
  985. }
  986. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址