libBilibiliToken_copy

哔哩哔哩cookie获取token

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/437326/1000267/libBilibiliToken_copy.js

  1. // ==UserScript==
  2. // @name libBilibiliToken_copy
  3. // @namespace https://github.com/lzghzr/TampermonkeyJS
  4. // @version 0.0.4
  5. // @author lzghzr
  6. // @description 哔哩哔哩cookie获取token
  7. // @match *://*.bilibili.com/*
  8. // @connect passport.bilibili.com
  9. // @license MIT
  10. // @grant GM_xmlhttpRequest
  11. // @run-at document-start
  12. // ==/UserScript==
  13. class BilibiliToken {
  14. constructor() {
  15. this._W = typeof unsafeWindow === 'undefined' ? window : unsafeWindow;
  16. this.biliLocalId = BilibiliToken.biliLocalId;
  17. this.buvid = BilibiliToken.buvid;
  18. this.deviceId = this.biliLocalId;
  19. this.fingerprint = BilibiliToken.fingerprint;
  20. this.guid = this.buvid;
  21. this.localFingerprint = this.fingerprint;
  22. this.localId = this.buvid;
  23. this.headers = {
  24. 'User-Agent': 'Mozilla/5.0 BiliTV/1.2.4.1 (bbcallen@gmail.com)',
  25. 'APP-KEY': BilibiliToken.mobiApp,
  26. 'Buvid': this.buvid,
  27. 'env': 'prod'
  28. };
  29. }
  30. static get biliLocalId() { return this.RandomID(20); }
  31. static get buvid() { return this.RandomID(37).toLocaleUpperCase(); }
  32. static get deviceId() { return this.biliLocalId; }
  33. static get fingerprint() { return this.RandomID(62); }
  34. static get guid() { return this.buvid; }
  35. static get localFingerprint() { return this.fingerprint; }
  36. static get localId() { return this.buvid; }
  37. static get TS() { return Math.floor(Date.now() / 1000); }
  38. static get RND() { return this.RandomNum(9); }
  39. static RandomNum(length) {
  40. const words = '0123456789';
  41. let randomNum = '';
  42. randomNum += words[Math.floor(Math.random() * 9) + 1];
  43. for (let i = 0; i < length - 1; i++)
  44. randomNum += words[Math.floor(Math.random() * 10)];
  45. return +randomNum;
  46. }
  47. static RandomID(length) {
  48. const words = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  49. let randomID = '';
  50. randomID += words[Math.floor(Math.random() * 61) + 1];
  51. for (let i = 0; i < length - 1; i++)
  52. randomID += words[Math.floor(Math.random() * 62)];
  53. return randomID;
  54. }
  55. static get headers() {
  56. return {
  57. 'User-Agent': 'Mozilla/5.0 BiliTV/1.2.4.1 (bbcallen@gmail.com)',
  58. 'APP-KEY': this.mobiApp,
  59. 'Buvid': this.buvid,
  60. 'env': 'prod'
  61. };
  62. }
  63. static get loginQuery() {
  64. const biliLocalId = this.biliLocalId;
  65. const buvid = this.buvid;
  66. const fingerprint = this.fingerprint;
  67. return `appkey=${this.loginAppKey}&bili_local_id=${biliLocalId}&build=${this.build}&buvid=${buvid}&channel=${this.channel}&device=${biliLocalId}\
  68. &device_id=${this.deviceId}&device_name=${this.deviceName}&device_platform=${this.devicePlatform}&fingerprint=${fingerprint}&guid=${buvid}\
  69. &local_fingerprint=${fingerprint}&local_id=${buvid}&mobi_app=${this.mobiApp}&networkstate=${this.networkstate}&platform=${this.platform}`;
  70. }
  71. get loginQuery() {
  72. const biliLocalId = this.biliLocalId;
  73. const buvid = this.buvid;
  74. const fingerprint = this.fingerprint;
  75. return `appkey=${BilibiliToken.loginAppKey}&bili_local_id=${biliLocalId}&build=${BilibiliToken.build}&buvid=${buvid}&channel=${BilibiliToken.channel}&device=${biliLocalId}\
  76. &device_id=${this.deviceId}&device_name=${BilibiliToken.deviceName}&device_platform=${BilibiliToken.devicePlatform}&fingerprint=${fingerprint}&guid=${buvid}\
  77. &local_fingerprint=${fingerprint}&local_id=${buvid}&mobi_app=${BilibiliToken.mobiApp}&networkstate=${BilibiliToken.networkstate}&platform=${BilibiliToken.platform}`;
  78. }
  79. static signQuery(params, ts = true, secretKey = this.__secretKey) {
  80. let paramsSort = params;
  81. if (ts)
  82. paramsSort = `${params}&ts=${this.TS}`;
  83. paramsSort = paramsSort.split('&').sort().join('&');
  84. const paramsSecret = paramsSort + secretKey;
  85. const paramsHash = this.md5(paramsSecret);
  86. return `${paramsSort}&sign=${paramsHash}`;
  87. }
  88. static signLoginQuery(params) {
  89. const paramsBase = params === undefined ? this.loginQuery : `${params}&${this.loginQuery}`;
  90. return this.signQuery(paramsBase, true, this.__loginSecretKey);
  91. }
  92. signLoginQuery(params) {
  93. const paramsBase = params === undefined ? this.loginQuery : `${params}&${this.loginQuery}`;
  94. return BilibiliToken.signQuery(paramsBase, true, BilibiliToken.__loginSecretKey);
  95. }
  96. async getAuthCode() {
  97. const authCode = await BilibiliToken.XHR({
  98. GM: true,
  99. anonymous: true,
  100. method: 'POST',
  101. url: 'https://passport.bilibili.com/x/passport-tv-login/qrcode/auth_code',
  102. data: this.signLoginQuery(),
  103. responseType: 'json',
  104. headers: this.headers
  105. });
  106. if (authCode !== undefined && authCode.response.status === 200 && authCode.body.code === 0)
  107. return authCode.body.data.auth_code;
  108. return console.error('getAuthCode', authCode);
  109. }
  110. async qrcodeConfirm(authCode, csrf) {
  111. const confirm = await BilibiliToken.XHR({
  112. GM: true,
  113. method: 'POST',
  114. url: 'https://passport.bilibili.com/x/passport-tv-login/h5/qrcode/confirm',
  115. data: `auth_code=${authCode}&csrf=${csrf}`,
  116. responseType: 'json',
  117. headers: this.headers
  118. });
  119. if (confirm !== undefined && confirm.response.status === 200 && confirm.body.code === 0)
  120. return confirm.body.data.gourl;
  121. return console.error('qrcodeConfirm', confirm);
  122. }
  123. async qrcodePoll(authCode) {
  124. const poll = await BilibiliToken.XHR({
  125. GM: true,
  126. anonymous: true,
  127. method: 'POST',
  128. url: 'https://passport.bilibili.com/x/passport-tv-login/qrcode/poll',
  129. data: this.signLoginQuery(`auth_code=${authCode}`),
  130. responseType: 'json',
  131. headers: this.headers
  132. });
  133. if (poll !== undefined && poll.response.status === 200 && poll.body.code === 0)
  134. return poll.body.data;
  135. return console.error('qrcodePoll', poll);
  136. }
  137. async getToken() {
  138. const cookie = this._W.document.cookie.match(/bili_jct=(?<csrf>.*?);/);
  139. if (cookie === null || cookie.groups === undefined)
  140. return console.error('getToken', 'cookie获取失败');
  141. const csrf = cookie.groups['csrf'];
  142. const authCode = await this.getAuthCode();
  143. if (authCode === undefined)
  144. return;
  145. const confirm = await this.qrcodeConfirm(authCode, csrf);
  146. if (confirm === undefined)
  147. return;
  148. const token = await this.qrcodePoll(authCode);
  149. if (token === undefined)
  150. return;
  151. return token;
  152. }
  153. static md5(string, key, raw) {
  154. return md5(string, key, raw);
  155. function safeAdd(x, y) {
  156. var lsw = (x & 0xffff) + (y & 0xffff);
  157. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  158. return (msw << 16) | (lsw & 0xffff);
  159. }
  160. function bitRotateLeft(num, cnt) {
  161. return (num << cnt) | (num >>> (32 - cnt));
  162. }
  163. function md5cmn(q, a, b, x, s, t) {
  164. return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b);
  165. }
  166. function md5ff(a, b, c, d, x, s, t) {
  167. return md5cmn((b & c) | (~b & d), a, b, x, s, t);
  168. }
  169. function md5gg(a, b, c, d, x, s, t) {
  170. return md5cmn((b & d) | (c & ~d), a, b, x, s, t);
  171. }
  172. function md5hh(a, b, c, d, x, s, t) {
  173. return md5cmn(b ^ c ^ d, a, b, x, s, t);
  174. }
  175. function md5ii(a, b, c, d, x, s, t) {
  176. return md5cmn(c ^ (b | ~d), a, b, x, s, t);
  177. }
  178. function binlMD5(x, len) {
  179. x[len >> 5] |= 0x80 << len % 32;
  180. x[(((len + 64) >>> 9) << 4) + 14] = len;
  181. var i;
  182. var olda;
  183. var oldb;
  184. var oldc;
  185. var oldd;
  186. var a = 1732584193;
  187. var b = -271733879;
  188. var c = -1732584194;
  189. var d = 271733878;
  190. for (i = 0; i < x.length; i += 16) {
  191. olda = a;
  192. oldb = b;
  193. oldc = c;
  194. oldd = d;
  195. a = md5ff(a, b, c, d, x[i], 7, -680876936);
  196. d = md5ff(d, a, b, c, x[i + 1], 12, -389564586);
  197. c = md5ff(c, d, a, b, x[i + 2], 17, 606105819);
  198. b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
  199. a = md5ff(a, b, c, d, x[i + 4], 7, -176418897);
  200. d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
  201. c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
  202. b = md5ff(b, c, d, a, x[i + 7], 22, -45705983);
  203. a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
  204. d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
  205. c = md5ff(c, d, a, b, x[i + 10], 17, -42063);
  206. b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
  207. a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
  208. d = md5ff(d, a, b, c, x[i + 13], 12, -40341101);
  209. c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
  210. b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
  211. a = md5gg(a, b, c, d, x[i + 1], 5, -165796510);
  212. d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
  213. c = md5gg(c, d, a, b, x[i + 11], 14, 643717713);
  214. b = md5gg(b, c, d, a, x[i], 20, -373897302);
  215. a = md5gg(a, b, c, d, x[i + 5], 5, -701558691);
  216. d = md5gg(d, a, b, c, x[i + 10], 9, 38016083);
  217. c = md5gg(c, d, a, b, x[i + 15], 14, -660478335);
  218. b = md5gg(b, c, d, a, x[i + 4], 20, -405537848);
  219. a = md5gg(a, b, c, d, x[i + 9], 5, 568446438);
  220. d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
  221. c = md5gg(c, d, a, b, x[i + 3], 14, -187363961);
  222. b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
  223. a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
  224. d = md5gg(d, a, b, c, x[i + 2], 9, -51403784);
  225. c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
  226. b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
  227. a = md5hh(a, b, c, d, x[i + 5], 4, -378558);
  228. d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
  229. c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
  230. b = md5hh(b, c, d, a, x[i + 14], 23, -35309556);
  231. a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
  232. d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
  233. c = md5hh(c, d, a, b, x[i + 7], 16, -155497632);
  234. b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
  235. a = md5hh(a, b, c, d, x[i + 13], 4, 681279174);
  236. d = md5hh(d, a, b, c, x[i], 11, -358537222);
  237. c = md5hh(c, d, a, b, x[i + 3], 16, -722521979);
  238. b = md5hh(b, c, d, a, x[i + 6], 23, 76029189);
  239. a = md5hh(a, b, c, d, x[i + 9], 4, -640364487);
  240. d = md5hh(d, a, b, c, x[i + 12], 11, -421815835);
  241. c = md5hh(c, d, a, b, x[i + 15], 16, 530742520);
  242. b = md5hh(b, c, d, a, x[i + 2], 23, -995338651);
  243. a = md5ii(a, b, c, d, x[i], 6, -198630844);
  244. d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
  245. c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
  246. b = md5ii(b, c, d, a, x[i + 5], 21, -57434055);
  247. a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
  248. d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
  249. c = md5ii(c, d, a, b, x[i + 10], 15, -1051523);
  250. b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
  251. a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
  252. d = md5ii(d, a, b, c, x[i + 15], 10, -30611744);
  253. c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
  254. b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
  255. a = md5ii(a, b, c, d, x[i + 4], 6, -145523070);
  256. d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
  257. c = md5ii(c, d, a, b, x[i + 2], 15, 718787259);
  258. b = md5ii(b, c, d, a, x[i + 9], 21, -343485551);
  259. a = safeAdd(a, olda);
  260. b = safeAdd(b, oldb);
  261. c = safeAdd(c, oldc);
  262. d = safeAdd(d, oldd);
  263. }
  264. return [a, b, c, d];
  265. }
  266. function binl2rstr(input) {
  267. var i;
  268. var output = '';
  269. var length32 = input.length * 32;
  270. for (i = 0; i < length32; i += 8) {
  271. output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff);
  272. }
  273. return output;
  274. }
  275. function rstr2binl(input) {
  276. var i;
  277. var output = [];
  278. output[(input.length >> 2) - 1] = undefined;
  279. for (i = 0; i < output.length; i += 1) {
  280. output[i] = 0;
  281. }
  282. var length8 = input.length * 8;
  283. for (i = 0; i < length8; i += 8) {
  284. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << i % 32;
  285. }
  286. return output;
  287. }
  288. function rstrMD5(s) {
  289. return binl2rstr(binlMD5(rstr2binl(s), s.length * 8));
  290. }
  291. function rstrHMACMD5(key, data) {
  292. var i;
  293. var bkey = rstr2binl(key);
  294. var ipad = [];
  295. var opad = [];
  296. var hash;
  297. ipad[15] = opad[15] = undefined;
  298. if (bkey.length > 16) {
  299. bkey = binlMD5(bkey, key.length * 8);
  300. }
  301. for (i = 0; i < 16; i += 1) {
  302. ipad[i] = bkey[i] ^ 0x36363636;
  303. opad[i] = bkey[i] ^ 0x5c5c5c5c;
  304. }
  305. hash = binlMD5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  306. return binl2rstr(binlMD5(opad.concat(hash), 512 + 128));
  307. }
  308. function rstr2hex(input) {
  309. var hexTab = '0123456789abcdef';
  310. var output = '';
  311. var x;
  312. var i;
  313. for (i = 0; i < input.length; i += 1) {
  314. x = input.charCodeAt(i);
  315. output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f);
  316. }
  317. return output;
  318. }
  319. function str2rstrUTF8(input) {
  320. return unescape(encodeURIComponent(input));
  321. }
  322. function rawMD5(s) {
  323. return rstrMD5(str2rstrUTF8(s));
  324. }
  325. function hexMD5(s) {
  326. return rstr2hex(rawMD5(s));
  327. }
  328. function rawHMACMD5(k, d) {
  329. return rstrHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d));
  330. }
  331. function hexHMACMD5(k, d) {
  332. return rstr2hex(rawHMACMD5(k, d));
  333. }
  334. function md5(string, key, raw) {
  335. if (!key) {
  336. if (!raw) {
  337. return hexMD5(string);
  338. }
  339. return rawMD5(string);
  340. }
  341. if (!raw) {
  342. return hexHMACMD5(key, string);
  343. }
  344. return rawHMACMD5(key, string);
  345. }
  346. }
  347. static XHR(XHROptions) {
  348. return new Promise(resolve => {
  349. const onerror = (error) => {
  350. console.error(GM_info.script.name, error);
  351. resolve(undefined);
  352. };
  353. if (XHROptions.GM) {
  354. if (XHROptions.method === 'POST') {
  355. if (XHROptions.headers === undefined)
  356. XHROptions.headers = {};
  357. if (XHROptions.headers['Content-Type'] === undefined)
  358. XHROptions.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
  359. }
  360. XHROptions.timeout = 30 * 1000;
  361. XHROptions.onload = res => resolve({ response: res, body: res.response });
  362. XHROptions.onerror = onerror;
  363. XHROptions.ontimeout = onerror;
  364. GM_xmlhttpRequest(XHROptions);
  365. }
  366. else {
  367. const xhr = new XMLHttpRequest();
  368. xhr.open(XHROptions.method, XHROptions.url);
  369. if (XHROptions.method === 'POST' && xhr.getResponseHeader('Content-Type') === null)
  370. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
  371. if (XHROptions.cookie)
  372. xhr.withCredentials = true;
  373. if (XHROptions.responseType !== undefined)
  374. xhr.responseType = XHROptions.responseType;
  375. xhr.timeout = 30 * 1000;
  376. xhr.onload = ev => {
  377. const res = ev.target;
  378. resolve({ response: res, body: res.response });
  379. };
  380. xhr.onerror = onerror;
  381. xhr.ontimeout = onerror;
  382. xhr.send(XHROptions.data);
  383. }
  384. });
  385. }
  386. }
  387. BilibiliToken.__loginSecretKey = '59b43e04ad6965f34319062b478f83dd';
  388. BilibiliToken.loginAppKey = '4409e2ce8ffd12b8';
  389. BilibiliToken.__secretKey = '560c52ccd288fed045859ed18bffd973';
  390. BilibiliToken.appKey = '1d8b6e7d45233436';
  391. BilibiliToken.build = '102401';
  392. BilibiliToken.channel = 'master';
  393. BilibiliToken.device = 'Sony';
  394. BilibiliToken.deviceName = 'J9110';
  395. BilibiliToken.devicePlatform = 'Android10SonyJ9110';
  396. BilibiliToken.mobiApp = 'android_tv_yst';
  397. BilibiliToken.networkstate = 'wifi';
  398. BilibiliToken.platform = 'android';

QingJ © 2025

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