Bilibili 哔哩哔哩视频点踩

为视频页面增加点踩选项,在视频下方工具栏最右边更多菜单中

  1. // ==UserScript==
  2. // @name Bilibili 哔哩哔哩视频点踩
  3. // @namespace https://github.com/Tsuk1ko
  4. // @version 1.0.2
  5. // @description 为视频页面增加点踩选项,在视频下方工具栏最右边更多菜单中
  6. // @author 神代綺凛
  7. // @license GPL-3.0
  8. // @match https://www.bilibili.com/video/*
  9. // @icon https://www.bilibili.com/favicon.ico
  10. // @connect passport.bilibili.com
  11. // @connect app.bilibili.com
  12. // @grant GM_addStyle
  13. // @grant GM_setValue
  14. // @grant GM_getValue
  15. // @grant GM_deleteValue
  16. // @grant GM_registerMenuCommand
  17. // @grant GM_xmlhttpRequest
  18. // @grant unsafeWindow
  19. // @run-at document-start
  20. // ==/UserScript==
  21.  
  22. (async () => {
  23. 'use strict';
  24.  
  25. // https://github.com/lzghzr/TampermonkeyJS/blob/master/libBilibiliToken/libBilibiliToken.js
  26. class BilibiliToken {
  27. static _W = typeof unsafeWindow === 'undefined' ? window : unsafeWindow;
  28. static __loginSecretKey = '2653583c8873dea268ab9386918b1d65';
  29. static loginAppKey = '783bbb7264451d82';
  30. static __secretKey = '560c52ccd288fed045859ed18bffd973';
  31. static appKey = '1d8b6e7d45233436';
  32. build = '6720300';
  33. buvid = BilibiliToken.buvidXX;
  34. Clocale = 'zh-Hans_CN';
  35. channel = 'website';
  36. localId = this.buvid;
  37. mobiApp = 'android';
  38. platform = 'android';
  39. Slocale = 'zh-Hans_CN';
  40. static get buvidXX() {
  41. const buvid = this.md5(Math.random().toString()).toUpperCase();
  42. return 'XX' + buvid[2] + buvid[12] + buvid[22] + buvid;
  43. }
  44. static get TS() {
  45. return Math.floor(Date.now() / 1000);
  46. }
  47. headers = {
  48. 'user-agent':
  49. 'Mozilla/5.0 BiliDroid/6.72.0 (bbcallen@gmail.com) os/android model/XQ-CT72 mobi_app/android build/6720300 channel/bilih5 innerVer/6720310 osVer/12 network/2',
  50. buvid: this.buvid,
  51. };
  52. get loginQuery() {
  53. return `appkey=${BilibiliToken.loginAppKey}&build=${this.build}&c_locale=${this.Clocale}&channel=${this.channel}&local_id=${this.localId}&mobi_app=${this.mobiApp}&platform=${this.platform}&s_locale=${this.Slocale}`;
  54. }
  55. static signQuery(params, ts = true, secretKey = this.__secretKey) {
  56. let paramsSort = params;
  57. if (ts) paramsSort = `${params}&ts=${this.TS}`;
  58. paramsSort = paramsSort.split('&').sort().join('&');
  59. const paramsSecret = paramsSort + secretKey;
  60. const paramsHash = this.md5(paramsSecret);
  61. return `${paramsSort}&sign=${paramsHash}`;
  62. }
  63. signLoginQuery(params) {
  64. const paramsBase = params === undefined ? this.loginQuery : `${params}&${this.loginQuery}`;
  65. return BilibiliToken.signQuery(paramsBase, true, BilibiliToken.__loginSecretKey);
  66. }
  67. async getAuthCode() {
  68. const authCode = await BilibiliToken.XHR({
  69. GM: true,
  70. anonymous: true,
  71. method: 'POST',
  72. url: 'https://passport.bilibili.com/x/passport-tv-login/qrcode/auth_code',
  73. data: this.signLoginQuery(),
  74. responseType: 'json',
  75. headers: this.headers,
  76. });
  77. if (authCode !== undefined && authCode.response.status === 200 && authCode.body.code === 0)
  78. return authCode.body.data.auth_code;
  79. return console.error(GM_info.script.name, 'getAuthCode', authCode);
  80. }
  81. async qrcodeConfirm(authCode, csrf) {
  82. const confirm = await BilibiliToken.XHR({
  83. GM: true,
  84. method: 'POST',
  85. url: 'https://passport.bilibili.com/x/passport-tv-login/h5/qrcode/confirm',
  86. data: `auth_code=${authCode}&csrf=${csrf}&scanning_type=1`,
  87. responseType: 'json',
  88. headers: this.headers,
  89. });
  90. if (confirm !== undefined && confirm.response.status === 200 && confirm.body.code === 0)
  91. return confirm.body.data.gourl;
  92. return console.error(GM_info.script.name, 'qrcodeConfirm', confirm);
  93. }
  94. async qrcodePoll(authCode) {
  95. const poll = await BilibiliToken.XHR({
  96. GM: true,
  97. anonymous: true,
  98. method: 'POST',
  99. url: 'https://passport.bilibili.com/x/passport-tv-login/qrcode/poll',
  100. data: this.signLoginQuery(`auth_code=${authCode}`),
  101. responseType: 'json',
  102. headers: this.headers,
  103. });
  104. if (poll !== undefined && poll.response.status === 200 && poll.body.code === 0)
  105. return poll.body.data;
  106. return console.error(GM_info.script.name, 'qrcodePoll', poll);
  107. }
  108. async getToken() {
  109. const cookie = BilibiliToken._W.document.cookie.match(/bili_jct=(?<csrf>.*?);/);
  110. if (cookie === null || cookie.groups === undefined)
  111. return console.error(GM_info.script.name, 'getToken', 'cookie获取失败');
  112. const csrf = cookie.groups['csrf'];
  113. const authCode = await this.getAuthCode();
  114. if (authCode === undefined) return;
  115. const confirm = await this.qrcodeConfirm(authCode, csrf);
  116. if (confirm === undefined) return;
  117. const token = await this.qrcodePoll(authCode);
  118. if (token === undefined) return;
  119. return token;
  120. }
  121. static md5(string, key, raw) {
  122. return md5(string, key, raw);
  123. function safeAdd(x, y) {
  124. var lsw = (x & 0xffff) + (y & 0xffff);
  125. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  126. return (msw << 16) | (lsw & 0xffff);
  127. }
  128. function bitRotateLeft(num, cnt) {
  129. return (num << cnt) | (num >>> (32 - cnt));
  130. }
  131. function md5cmn(q, a, b, x, s, t) {
  132. return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b);
  133. }
  134. function md5ff(a, b, c, d, x, s, t) {
  135. return md5cmn((b & c) | (~b & d), a, b, x, s, t);
  136. }
  137. function md5gg(a, b, c, d, x, s, t) {
  138. return md5cmn((b & d) | (c & ~d), a, b, x, s, t);
  139. }
  140. function md5hh(a, b, c, d, x, s, t) {
  141. return md5cmn(b ^ c ^ d, a, b, x, s, t);
  142. }
  143. function md5ii(a, b, c, d, x, s, t) {
  144. return md5cmn(c ^ (b | ~d), a, b, x, s, t);
  145. }
  146. function binlMD5(x, len) {
  147. x[len >> 5] |= 0x80 << len % 32;
  148. x[(((len + 64) >>> 9) << 4) + 14] = len;
  149. var i;
  150. var olda;
  151. var oldb;
  152. var oldc;
  153. var oldd;
  154. var a = 1732584193;
  155. var b = -271733879;
  156. var c = -1732584194;
  157. var d = 271733878;
  158. for (i = 0; i < x.length; i += 16) {
  159. olda = a;
  160. oldb = b;
  161. oldc = c;
  162. oldd = d;
  163. a = md5ff(a, b, c, d, x[i], 7, -680876936);
  164. d = md5ff(d, a, b, c, x[i + 1], 12, -389564586);
  165. c = md5ff(c, d, a, b, x[i + 2], 17, 606105819);
  166. b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
  167. a = md5ff(a, b, c, d, x[i + 4], 7, -176418897);
  168. d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
  169. c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
  170. b = md5ff(b, c, d, a, x[i + 7], 22, -45705983);
  171. a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
  172. d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
  173. c = md5ff(c, d, a, b, x[i + 10], 17, -42063);
  174. b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
  175. a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
  176. d = md5ff(d, a, b, c, x[i + 13], 12, -40341101);
  177. c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
  178. b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
  179. a = md5gg(a, b, c, d, x[i + 1], 5, -165796510);
  180. d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
  181. c = md5gg(c, d, a, b, x[i + 11], 14, 643717713);
  182. b = md5gg(b, c, d, a, x[i], 20, -373897302);
  183. a = md5gg(a, b, c, d, x[i + 5], 5, -701558691);
  184. d = md5gg(d, a, b, c, x[i + 10], 9, 38016083);
  185. c = md5gg(c, d, a, b, x[i + 15], 14, -660478335);
  186. b = md5gg(b, c, d, a, x[i + 4], 20, -405537848);
  187. a = md5gg(a, b, c, d, x[i + 9], 5, 568446438);
  188. d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
  189. c = md5gg(c, d, a, b, x[i + 3], 14, -187363961);
  190. b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
  191. a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
  192. d = md5gg(d, a, b, c, x[i + 2], 9, -51403784);
  193. c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
  194. b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
  195. a = md5hh(a, b, c, d, x[i + 5], 4, -378558);
  196. d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
  197. c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
  198. b = md5hh(b, c, d, a, x[i + 14], 23, -35309556);
  199. a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
  200. d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
  201. c = md5hh(c, d, a, b, x[i + 7], 16, -155497632);
  202. b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
  203. a = md5hh(a, b, c, d, x[i + 13], 4, 681279174);
  204. d = md5hh(d, a, b, c, x[i], 11, -358537222);
  205. c = md5hh(c, d, a, b, x[i + 3], 16, -722521979);
  206. b = md5hh(b, c, d, a, x[i + 6], 23, 76029189);
  207. a = md5hh(a, b, c, d, x[i + 9], 4, -640364487);
  208. d = md5hh(d, a, b, c, x[i + 12], 11, -421815835);
  209. c = md5hh(c, d, a, b, x[i + 15], 16, 530742520);
  210. b = md5hh(b, c, d, a, x[i + 2], 23, -995338651);
  211. a = md5ii(a, b, c, d, x[i], 6, -198630844);
  212. d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
  213. c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
  214. b = md5ii(b, c, d, a, x[i + 5], 21, -57434055);
  215. a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
  216. d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
  217. c = md5ii(c, d, a, b, x[i + 10], 15, -1051523);
  218. b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
  219. a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
  220. d = md5ii(d, a, b, c, x[i + 15], 10, -30611744);
  221. c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
  222. b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
  223. a = md5ii(a, b, c, d, x[i + 4], 6, -145523070);
  224. d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
  225. c = md5ii(c, d, a, b, x[i + 2], 15, 718787259);
  226. b = md5ii(b, c, d, a, x[i + 9], 21, -343485551);
  227. a = safeAdd(a, olda);
  228. b = safeAdd(b, oldb);
  229. c = safeAdd(c, oldc);
  230. d = safeAdd(d, oldd);
  231. }
  232. return [a, b, c, d];
  233. }
  234. function binl2rstr(input) {
  235. var i;
  236. var output = '';
  237. var length32 = input.length * 32;
  238. for (i = 0; i < length32; i += 8) {
  239. output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff);
  240. }
  241. return output;
  242. }
  243. function rstr2binl(input) {
  244. var i;
  245. var output = [];
  246. output[(input.length >> 2) - 1] = undefined;
  247. for (i = 0; i < output.length; i += 1) {
  248. output[i] = 0;
  249. }
  250. var length8 = input.length * 8;
  251. for (i = 0; i < length8; i += 8) {
  252. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << i % 32;
  253. }
  254. return output;
  255. }
  256. function rstrMD5(s) {
  257. return binl2rstr(binlMD5(rstr2binl(s), s.length * 8));
  258. }
  259. function rstrHMACMD5(key, data) {
  260. var i;
  261. var bkey = rstr2binl(key);
  262. var ipad = [];
  263. var opad = [];
  264. var hash;
  265. ipad[15] = opad[15] = undefined;
  266. if (bkey.length > 16) {
  267. bkey = binlMD5(bkey, key.length * 8);
  268. }
  269. for (i = 0; i < 16; i += 1) {
  270. ipad[i] = bkey[i] ^ 0x36363636;
  271. opad[i] = bkey[i] ^ 0x5c5c5c5c;
  272. }
  273. hash = binlMD5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  274. return binl2rstr(binlMD5(opad.concat(hash), 512 + 128));
  275. }
  276. function rstr2hex(input) {
  277. var hexTab = '0123456789abcdef';
  278. var output = '';
  279. var x;
  280. var i;
  281. for (i = 0; i < input.length; i += 1) {
  282. x = input.charCodeAt(i);
  283. output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f);
  284. }
  285. return output;
  286. }
  287. function str2rstrUTF8(input) {
  288. return unescape(encodeURIComponent(input));
  289. }
  290. function rawMD5(s) {
  291. return rstrMD5(str2rstrUTF8(s));
  292. }
  293. function hexMD5(s) {
  294. return rstr2hex(rawMD5(s));
  295. }
  296. function rawHMACMD5(k, d) {
  297. return rstrHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d));
  298. }
  299. function hexHMACMD5(k, d) {
  300. return rstr2hex(rawHMACMD5(k, d));
  301. }
  302. function md5(string, key, raw) {
  303. if (!key) {
  304. if (!raw) {
  305. return hexMD5(string);
  306. }
  307. return rawMD5(string);
  308. }
  309. if (!raw) {
  310. return hexHMACMD5(key, string);
  311. }
  312. return rawHMACMD5(key, string);
  313. }
  314. }
  315. static XHR(XHROptions) {
  316. return new Promise(resolve => {
  317. const onerror = error => {
  318. console.error(GM_info.script.name, error);
  319. resolve(undefined);
  320. };
  321. if (XHROptions.GM) {
  322. if (XHROptions.method === 'POST') {
  323. if (XHROptions.headers === undefined) XHROptions.headers = {};
  324. if (XHROptions.headers['Content-Type'] === undefined)
  325. XHROptions.headers['Content-Type'] =
  326. 'application/x-www-form-urlencoded; charset=utf-8';
  327. }
  328. XHROptions.timeout = 30 * 1000;
  329. XHROptions.onload = res => resolve({ response: res, body: res.response });
  330. XHROptions.onerror = onerror;
  331. XHROptions.ontimeout = onerror;
  332. GM_xmlhttpRequest(XHROptions);
  333. } else {
  334. const xhr = new XMLHttpRequest();
  335. xhr.open(XHROptions.method, XHROptions.url);
  336. if (XHROptions.method === 'POST' && xhr.getResponseHeader('Content-Type') === null)
  337. xhr.setRequestHeader(
  338. 'Content-Type',
  339. 'application/x-www-form-urlencoded; charset=utf-8'
  340. );
  341. if (XHROptions.withCredentials) xhr.withCredentials = true;
  342. if (XHROptions.responseType !== undefined) xhr.responseType = XHROptions.responseType;
  343. xhr.timeout = 30 * 1000;
  344. xhr.onload = ev => {
  345. const res = ev.target;
  346. resolve({ response: res, body: res.response });
  347. };
  348. xhr.onerror = onerror;
  349. xhr.ontimeout = onerror;
  350. xhr.send(XHROptions.data);
  351. }
  352. });
  353. }
  354. }
  355.  
  356. GM_registerMenuCommand('重置 access_key', () => {
  357. GM_deleteValue('access_key');
  358. alert('刷新页面生效');
  359. });
  360.  
  361. const css = ([style]) => GM_addStyle(style);
  362.  
  363. css`
  364. .video-dislike-icon {
  365. transform: scaleY(-1);
  366. }
  367. `;
  368.  
  369. const client = new BilibiliToken();
  370.  
  371. let accessKey = '';
  372. (async () => {
  373. const saved = GM_getValue('access_key');
  374. if (saved) {
  375. accessKey = saved;
  376. return;
  377. }
  378. const data = await client.getToken();
  379. const val = data?.access_token;
  380. if (val) GM_setValue('access_key', val);
  381. accessKey = val;
  382. })();
  383.  
  384. const queryStringify = data =>
  385. Object.entries(data)
  386. .map(([k, v]) => `${k}=${v}`)
  387. .join('&');
  388.  
  389. const dislike = async (isCancel = false) => {
  390. if (!accessKey) {
  391. alert('access_key 尚未成功获取,功能不可用');
  392. throw new Error('no access_key');
  393. }
  394. const params = BilibiliToken.signQuery(
  395. queryStringify({
  396. access_key: accessKey,
  397. actionKey: 'appkey',
  398. aid: BilibiliToken._W.__INITIAL_STATE__.aid,
  399. appkey: BilibiliToken.appKey,
  400. build: client.build,
  401. c_locale: client.Clocale,
  402. device: 'phone',
  403. disable_rcmd: 0,
  404. dislike: isCancel ? 1 : 0,
  405. mobi_app: client.mobiApp,
  406. platform: client.platform,
  407. s_locale: client.Slocale,
  408. })
  409. );
  410. const data = await BilibiliToken.XHR({
  411. GM: true,
  412. anonymous: true,
  413. method: 'POST',
  414. url: 'https://app.bilibili.com/x/v2/view/dislike',
  415. data: params,
  416. responseType: 'json',
  417. headers: client.headers,
  418. });
  419. console.log('[BilibiliDislike]', data);
  420. return data.body;
  421. };
  422.  
  423. /**
  424. * @param {string} selector
  425. * @param {number} [timeout]
  426. * @returns {Promise<HTMLElement>}
  427. */
  428. const waitSelector = (selector, timeout = 10000) =>
  429. new Promise((resolve, reject) => {
  430. const el = document.querySelector(selector);
  431. if (el) {
  432. resolve(el);
  433. return;
  434. }
  435. const timeoutTimer = setTimeout(() => {
  436. clearInterval(timer);
  437. reject();
  438. }, timeout);
  439. const timer = setInterval(() => {
  440. const el = document.querySelector(selector);
  441. if (el) {
  442. clearTimeout(timeoutTimer);
  443. clearInterval(timer);
  444. resolve(el);
  445. }
  446. }, 200);
  447. });
  448.  
  449. const showToast = text => {
  450. BilibiliToken._W.player.toast.create({ text, duration: 3000 });
  451. };
  452.  
  453. window.addEventListener('load', async () => {
  454. /** @type {SVGElement} */
  455. const dislikeIcon = (await waitSelector('.toolbar-left-item-wrap .video-like svg')).cloneNode(
  456. true
  457. );
  458. dislikeIcon.classList.replace('video-like-icon', 'video-dislike-icon');
  459.  
  460. const rightItem = await waitSelector('.video-use-phone.video-toolbar-right-item');
  461. /** @type {HTMLElement} */
  462. const dislikeBtn = rightItem.cloneNode(true);
  463. dislikeBtn.classList.replace('video-use-phone', 'video-dislike');
  464. const text = dislikeBtn.querySelector('.video-toolbar-item-text');
  465. text.classList.replace('video-use-phone-info', 'video-dislike-info');
  466. text.innerHTML = '不喜欢';
  467. dislikeBtn.addEventListener('click', async () => {
  468. const { code, message } = await dislike();
  469. if (code === 0) {
  470. showToast('点踩成功');
  471. } else {
  472. showToast(message);
  473. }
  474. });
  475. dislikeBtn.addEventListener('contextmenu', async e => {
  476. e.preventDefault();
  477. const { code, message } = await dislike(true);
  478. if (code === 0) {
  479. showToast('取消点踩成功');
  480. } else {
  481. showToast(message);
  482. }
  483. });
  484. dislikeBtn.querySelector('svg').replaceWith(dislikeIcon);
  485. rightItem.parentElement.append(dislikeBtn);
  486. });
  487. })();