哔哩哔哩工具合集-开发中

注:练手作品,多多包涵,不接受恶意差评,有问题群内咨询(Q群1031014013):批量播放视频;批量拉黑;批量关注。。。(友情天选群746790091 )

当前为 2021-03-05 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/422731/907491/%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9%E5%B7%A5%E5%85%B7%E5%90%88%E9%9B%86-%E5%BC%80%E5%8F%91%E4%B8%AD.js

  1. // ==UserScript==
  2. // @name 哔哩哔哩工具合集-开发中
  3. // @description 注:练手作品,多多包涵,不接受恶意差评,有问题群内咨询(Q群1031014013):批量播放视频;批量拉黑;批量关注。。。(友情天选群746790091 )
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.3.2
  6. // @description try to take over the world!
  7. // @author 荒年(QQ:2019676120)
  8. // @match *://space.bilibili.com/*
  9. // @require https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js
  10. // @resource animate http://cdn.javaex.cn/javaex/pc/css/animate.css
  11. // @resource common http://cdn.javaex.cn/javaex/pc/css/common.css
  12. // @resource defaultCss http://cdn.javaex.cn/javaex/pc/css/skin/default.css
  13. // @grant unsafeWindow
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_xmlhttpRequest
  17. // @grant GM_getResourceText
  18. // @grant GM_openInTab
  19. // @grant GM_addStyle
  20. // @run-at document-idle
  21. // @license MIT License
  22. // @namespace http://tampermonkey.net/
  23. // ==/UserScript==
  24.  
  25.  
  26. GM_addStyle(`
  27. #left_navbar{
  28. z-index: 1000;
  29. position: absolute;
  30. top: 100px;
  31. left: 5px;
  32. }
  33. button[hnBtn="btn_open"] {
  34. z-index: 1001;
  35. z-index: 1000px;
  36. position: absolute;
  37. top: 58px;
  38. left: 220px;
  39. }
  40. .javaex-panel.javaex-panel-right.javaex-animated-zoom-in {
  41. top: 60px;
  42. }
  43. .javaex-menu-container {
  44. border: 1px solid #eee;
  45. }
  46. .javaex-panel-box p {
  47. text-align: center;
  48. background: #fb7299;
  49. }
  50. .fontbold{
  51. color: #00a1d6;
  52. font-weight: bold;
  53. cursor: pointer;
  54. }
  55. `);
  56.  
  57. let Live_info = {
  58. author_uid: 99439379,
  59. room_id: undefined,
  60. uid: undefined,
  61. blocked: undefined,
  62. csrf_token: undefined,
  63. visit_id: undefined,
  64. rnd: undefined,
  65. ruid: undefined,
  66. uname: undefined,
  67. user_level: undefined,
  68. ruid: undefined,
  69. };
  70.  
  71. let BILIBLI_PATH = {
  72.  
  73. "passport": "https://passport.bilibili.com",
  74. "api": "https://api.bilibili.com",
  75. "video": "https://www.bilibili.com/video/",
  76. "single_unread": "http://api.vc.bilibili.com/"
  77. }
  78. /**
  79. * 参考 https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/login/login_info.md
  80. * @type {{nav: string}}
  81. */
  82. let BILIBILI_URL = {
  83. // 导航栏用户信息
  84. "nav": BILIBLI_PATH.api + "/x/web-interface/nav",
  85. }
  86.  
  87. let BiliData = {
  88. login_code: null,
  89. login_message: "",
  90. LoginData: {},
  91. CoinLog: {},
  92. Unread: null,
  93. Video: {},
  94. replyAction: {},
  95. upCard: {},
  96. BVList: [],
  97. }
  98.  
  99. let script = document.createElement('script');
  100. script.setAttribute('type', 'text/javascript');
  101. script.src = "https://cdn.jsdelivr.net/gh/java-xiaoyi/HNGG-RES@main/javaex/pc/js/javaex.min.js";
  102. document.documentElement.appendChild(script);
  103.  
  104. let script2 = document.createElement('script');
  105. script2.setAttribute('type', 'text/javascript');
  106. script2.src = "https://cdn.jsdelivr.net/gh/java-xiaoyi/HNGG-RES@main/javaex/pc/js/javaex-formVerify.js";
  107. document.documentElement.appendChild(script2);
  108.  
  109. window.onload = function () {
  110.  
  111.  
  112.  
  113.  
  114. //界面显示
  115. init();
  116.  
  117. document.getElementById("getAtMsg").click();
  118.  
  119. }
  120.  
  121.  
  122. function init() {
  123.  
  124. Live_info.csrf_token = HNTOOL.getCookie("bili_jct");
  125. Live_info.uid = HNTOOL.getCookie("DedeUserID");
  126.  
  127. addStyle();
  128. addSetPage();
  129.  
  130. $("#getCoinLog").bind("click", async function () {
  131.  
  132.  
  133. if (HNAPI.getCoinLog()) {
  134.  
  135. await HNTOOL.Sleep(500);
  136. let html = '';
  137. html += '<div class="javaex-panel-box" style="width: 250px;">';
  138. html += ' <ul>';
  139. html += ` <p style="color: #FFF;">硬币使用记录</p>`;
  140.  
  141. BiliData.CoinLog.list.forEach(function (item) {
  142.  
  143. let BV = item.reason.replace(/[^a-z0-9]+/ig, "");
  144. if (BV.indexOf("BV") != -1) {
  145. html += `<li><a href="${BILIBLI_PATH.video}${BV}">${item.reason}${item.delta}币 ${item.time}</a> </li> `;
  146. }
  147. html += `<li>${item.reason}${item.delta}币 ${item.time}</li> `;
  148.  
  149.  
  150. });
  151. html += ' <hr class="javaex-divider"></hr>';
  152. html += ' </ul>';
  153. html += '</div>';
  154.  
  155. javaex.panel(this, {
  156. position: "right",
  157. //offsetTop: "104",
  158. content: html
  159. });
  160. javaex.tip({
  161. content: "获取用户成功!",
  162. type: "success"
  163. });
  164.  
  165. } else {
  166. javaex.tip({
  167. content: "获取硬币记录失败,请稍后重试!",
  168. type: "error"
  169. });
  170. }
  171.  
  172.  
  173. });
  174.  
  175. $("#getUnread").bind("click", async function () {
  176.  
  177. javaex.tip({
  178. content: "获取中,请稍候...",
  179. type: "submit"
  180. });
  181. HNAPI.getUnread();
  182.  
  183.  
  184. });
  185.  
  186.  
  187. $("#likeInDynamicComments").bind("click", async function () {
  188.  
  189. let prid = LocalData.get("likeInDynamic_PRID") == null ? "4015599048" : LocalData.get("likeInDynamic_PRID");
  190. let oid = LocalData.get("likeInDynamic_OID") == null ? "108928314" : LocalData.get("likeInDynamic_OID");
  191.  
  192. let html = '';
  193.  
  194. html += '<div style="padding: 10px 10px 10px 0px;">'
  195. html += '<form id="likeInDynamic_form">'
  196. html += ' <div class="javaex-unit clear">'
  197. html += ' <div class="javaex-unit-left"><p class="subtitle required">评论RPID</p></div>'
  198. html += ' <div class="javaex-unit-right">'
  199. html += ' <input type="text" class="javaex-text" data-type="positive_int" error-msg="评论RPID格式错误"'
  200. html += ` id="likeInDynamic_PRID" value=${prid} placeholder="请输入评论RPID"/>`
  201. html += ' </div>'
  202. html += ' </div>'
  203. html += ' <div class="javaex-unit clear">'
  204. html += ' <div class="javaex-unit-left"><p class="subtitle required">OID</p></div>'
  205. html += ' <div class="javaex-unit-right">'
  206. html += ' <input type="text" class="javaex-text" data-type="positive_int" error-msg="OID格式错误"'
  207. html += ` id="likeInDynamic_OID" value=${oid} placeholder="请输入OID"/>`
  208. html += ' </div>'
  209. html += ' </div>'
  210. html += ' <div class="javaex-unit clear tc">'
  211. html += ' <input type="button" id="likeInDynamic_no" class="javaex-hover-pulse javaex-btn no " value="取消赞"/>'
  212. html += ' <input type="button" id="likeInDynamic_yes" class="javaex-hover-pulse javaex-btn yes" value="点赞"/>'
  213. html += ' </div>'
  214. html += '</form>';
  215. html += '<hr class="javaex-divider"></hr>';
  216. html += '</div>';
  217.  
  218.  
  219. javaex.panel(this, {
  220. position: "right",
  221. content: html
  222. });
  223.  
  224.  
  225. // 监听点击保存按钮事件
  226. $("#likeInDynamic_yes").click(async function () {
  227. // 表单验证函数
  228. if (javaexVerify()) {
  229.  
  230. HNAPI.likeInDynamicComments(+javaex.getSelectVal("#likeInDynamic_OID"), +javaex.getSelectVal("#likeInDynamic_PRID"), 1);
  231. await HNTOOL.Sleep(1000);
  232.  
  233. if (BiliData.replyAction.code == 0) {
  234.  
  235. LocalData.set("likeInDynamic_PRID", javaex.getSelectVal("#likeInDynamic_PRID"));
  236. LocalData.set("likeInDynamic_OID", javaex.getSelectVal("#likeInDynamic_OID"));
  237.  
  238. javaex.tip({content: "评论区点赞成功!", type: "success"});
  239. } else {
  240. javaex.tip({content: "评论区点赞失败!", type: "error"});
  241. }
  242. if (BiliData.replyAction.code == 65006) {
  243. javaex.tip({content: "请勿重复点赞!", type: "error"});
  244. }
  245. }
  246. });
  247.  
  248. // 监听点击返回按钮事件
  249. $("#likeInDynamic_no").click(async function () {
  250.  
  251. if (javaexVerify()) {
  252.  
  253. HNAPI.likeInDynamicComments(+javaex.getLocalStorage("#likeInDynamic_OID"), +javaex.getLocalStorage("#likeInDynamic_PRID"), 0);
  254. await HNTOOL.Sleep(500);
  255.  
  256. if (BiliData.replyAction.code == 0) {
  257.  
  258. javaex.setLocalStorage("likeInDynamic_PRID", javaex.getSelectVal("#likeInDynamic_PRID"));
  259. javaex.setLocalStorage("likeInDynamic_OID", javaex.getSelectVal("#likeInDynamic_OID"));
  260.  
  261. javaex.tip({content: "评论区取消点赞成功!", type: "success"});
  262. } else {
  263. javaex.tip({content: "评论区取消点赞失败!", type: "error"});
  264. }
  265. if (BiliData.replyAction.code == 65006) {
  266. javaex.tip({content: "请勿重复取消点赞!", type: "error"});
  267. }
  268. }
  269. });
  270.  
  271. });
  272.  
  273. $("#biliUserMsg").bind("click", async function () {
  274.  
  275.  
  276. //刷新信息
  277. HNAPI.getUserNav();
  278. await HNTOOL.Sleep(500);
  279.  
  280. let isLogin = "未登录";
  281. let vipType = "无";
  282. //判断是否成功
  283. if (BiliData.login_code == 0) {
  284.  
  285.  
  286. if (BiliData.LoginData.isLogin) {
  287. isLogin = "已登录";
  288. }
  289.  
  290. if (BiliData.LoginData.vipType == 1) {
  291. vipType = "月度大会员";
  292. } else if (BiliData.LoginData.vipType == 2) {
  293. vipType = "年度大会员";
  294. }
  295.  
  296. let html = '';
  297. html += '<div class="javaex-panel-box" style="width: 250px;">';
  298. html += ' <ul>';
  299. html += ` <p style="color: #FFF;">用户信息</p>`;
  300. html += ` <li><a href="https://account.bilibili.com/account/home">当前用户:${BiliData.LoginData.uname}</a> </li> `;
  301. html += ` <li>用户状态:${isLogin} </li> `;
  302. html += ` <li>用户等级:${BiliData.LoginData.level_info.current_level}</li> `;
  303. html += ` <li>升级下一等级需:${BiliData.LoginData.level_info.next_exp - BiliData.LoginData.level_info.current_exp} 经验</li> `;
  304. html += ` <li>UID${BiliData.LoginData.mid} </li> `;
  305. html += ` <li><a href="https://account.bilibili.com/account/coin">硬币:${BiliData.LoginData.money}</a> </li> `;
  306. html += ` <li>节操值:${BiliData.LoginData.moral} </li> `;
  307. html += ` <li>会员:${vipType} </li> `;
  308. html += ' <hr class="javaex-divider"></hr>';
  309. html += ' </ul>';
  310. html += '</div>';
  311.  
  312.  
  313. javaex.panel(this, {
  314. position: "right",
  315. content: html
  316. });
  317.  
  318. javaex.tip({
  319. content: "获取用户成功!",
  320. type: "success"
  321. });
  322.  
  323. } else {
  324. javaex.tip({
  325. content: "获取用户信息失败!",
  326. type: "error"
  327. });
  328. }
  329.  
  330.  
  331. });
  332.  
  333. $("#getAtMsg").bind("click", async function () {
  334.  
  335. if (HNAPI.getMsgfeed("at") && typeof (HNAPI.getMsgfeed("at")) != "undefined") {
  336.  
  337. javaex.tip({
  338. content: "可能中奖了!!!",
  339. type: "success"
  340. });
  341. }
  342. });
  343.  
  344.  
  345. $("#disUser").bind("click", async function () {
  346.  
  347. let html = '';
  348. html += '<div class="javaex-panel-box" style="width: 500px;">';
  349. html += ' <div class="javaex-unit clear">';
  350. html += ' <div class="javaex-unit-left ml-10 mt-10">*拉黑列表</div>';
  351. html += ' <div class="javaex-unit-right mr-10 mt-10">';
  352. html += ' <textarea id="disUser_uidList" class="javaex-desc" placeholder="请填写用户UID,多个用英文逗号,隔开"></textarea>';
  353. html += ' </div>';
  354. html += ' </div>';
  355. html += ' <div class="javaex-unit clear tc">';
  356. html += ' <input type="button" id="disUser_beg" class="javaex-btn yes" value="开始" />';
  357. html += ' </div>';
  358. html += '</div>';
  359.  
  360. javaex.panel(this, {
  361. position: "right",
  362. content: html
  363. });
  364.  
  365. $("#disUser_beg").bind("click", async function () {
  366.  
  367. let disUser_str = javaex.getSelectVal("#disUser_uidList");
  368. let disUser_uidList = disUser_str.split(",");
  369.  
  370. for (const vmid of disUser_uidList) {
  371.  
  372. HNAPI.disUserByUid(vmid);
  373.  
  374. await HNTOOL.Sleep(1500);
  375.  
  376. }
  377.  
  378. });
  379.  
  380. });
  381.  
  382. $("#batchFollow").bind("click", async function () {
  383.  
  384. let html = '';
  385. html += '<div class="javaex-panel-box" style="width: 500px;">';
  386. html += ' <div class="javaex-unit clear">';
  387. html += ' <div class="javaex-unit-left ml-10 mt-10">*关注列表</div>';
  388. html += ' <div class="javaex-unit-right mr-10 mt-10">';
  389. html += ' <textarea id="batchFollow_uidList" class="javaex-desc" placeholder="请填写用户UID,多个用英文逗号,隔开,此功能会强制关注,解决账号异常不能关注问题!!!"></textarea>';
  390. html += ' </div>';
  391. html += ' </div>';
  392. html += ' <div class="javaex-unit clear tc">';
  393. html += ' <input type="button" id="batchFollow_beg" class="javaex-btn yes" value="开始" />';
  394. html += ' </div>';
  395. html += '</div>';
  396.  
  397. javaex.panel(this, {
  398. position: "right",
  399. content: html
  400. });
  401.  
  402. $("#batchFollow_beg").bind("click", async function () {
  403.  
  404. let disUser_str = javaex.getSelectVal("#batchFollow_uidList");
  405. disUser_uidList = disUser_str.split(",");
  406. HNAPI.batchFollowByUid(disUser_uidList);
  407.  
  408. });
  409.  
  410. });
  411.  
  412. //
  413. $("#getUpMassage").bind("click", async function () {
  414.  
  415.  
  416. let html = '';
  417.  
  418. html += '<div style="padding: 10px 10px 10px 0px;">'
  419. html += '<form id="getUpMassage_form">'
  420. html += ' <div class="javaex-unit clear">'
  421. html += ' <div class="javaex-unit-left mr-10"><p class="subtitle required">用户uid</p></div>'
  422. html += ' <div class="javaex-unit-left mr-10">'
  423. html += ' <input type="text" class="javaex-text javaex-grid-4" data-type="positive_int" error-msg="用户uid"'
  424. html += ` id="up_uid" value="4578433" placeholder="请输入评论uid"/>`
  425. html += ' </div>'
  426. html += ' <div class="javaex-unit-right mr-10">'
  427. html += ' <input type="button" id="getUpMsg" class="javaex-btn blue" value="查询"/>'
  428. html += ' </div>'
  429. html += ' </div>'
  430. html += ' <div class="javaex-unit clear tc" style="width: 500px;">'
  431. html += ' <table id="upMsgTable" class="javaex-table td-c-1">'
  432. html += ' <thead>'
  433. html += ' <tr><th>内容</th><th>详情</th></tr>'
  434. html += ' </thead>'
  435. html += ' <tbody id="upMsgTbody">'
  436.  
  437. html += ' </tbody>'
  438. html += '</table> '
  439. html += '</div>'
  440. html += '</form>';
  441. html += '<hr class="javaex-divider"></hr>';
  442. html += '</div>';
  443.  
  444.  
  445. javaex.panel(this, {
  446. position: "right",
  447. content: html
  448. });
  449.  
  450. javaex.table({
  451. id: "upMsgTable",
  452. isDragColWidth: true, // 是否允许拖动改变列宽
  453. mode: "overflow", // overflow表示允许使用父容器溢出来调整列的大小
  454. leftFixedColNum: 2, // 左侧固定列数
  455. rightFixedColNum: 1, // 右侧固定列数
  456. sort: {
  457. "1": "create_time", // 4表示第几列(从1开始计)。create_time表示排序字段,回调函数会返回该值
  458. "2": "update_time"
  459. },
  460. sortCallback: function (rtn) {
  461. //console.log(rtn.sortArr);
  462. }
  463. });
  464.  
  465. //
  466. $("#getUpMsg").click(async function () {
  467.  
  468. let temp = HNAPI.getUpCard(parseInt(javaex.getSelectVal("#up_uid")));
  469.  
  470.  
  471. await HNTOOL.Sleep(500);
  472.  
  473.  
  474. if (temp != false) {
  475. let data = BiliData.upCard.data;
  476.  
  477. let html = ``;
  478. html += `<tr><td>用户UID</td><td>${data.card.mid}</td></tr> `
  479. html += `<tr><td>用户等级</td><td>lv${data.card.level_info.current_level}</td></tr> `
  480. html += `<tr><td>用户昵称</td><td>${data.card.name}</td></tr> `
  481. html += `<tr><td>用户性别</td><td>${data.card.sex}</td></tr> `
  482. html += `<tr><td>用户头像</td><td><a href="javascript:;"><img src= ${data.card.face} width="50%"></a></td></tr> `
  483. html += `<tr><td>粉丝数</td><td>${data.follower}</td></tr> `
  484. html += `<tr><td>关注数</td><td>${data.card.friend}</td></tr> `
  485. html += `<tr><td>是否关注此用户</td><td>${data.following}</td></tr> `
  486. html += `<tr><td>用户稿件数</td><td>${data.archive_count}</td></tr> `
  487. html += `<tr><td>用户勋章</td><td>${data.card.nameplate.name}<a href="javascript:;"><img src= ${data.card.nameplate.image} width="50%"></a> </td></tr> `
  488. html += `<tr><td>用户勋章等级</td><td>${data.card.nameplate.level}</td></tr> `
  489. html += `<tr><td>认证信息</td><td>${data.card.Official.title}</td></tr> `
  490.  
  491.  
  492. $("#upMsgTbody").empty();
  493. $("#upMsgTbody").append(html);
  494. }
  495.  
  496.  
  497. });
  498. //点击放大
  499. /* $("img").click(function() {
  500. javaex.dialog({
  501. type : "image", // 弹出层类型
  502. url : $(this).attr("src") // 图片地址
  503. });
  504. });*/
  505.  
  506. });
  507.  
  508.  
  509. $("#addPlay").bind("click", async function () {
  510.  
  511.  
  512. let html = '';
  513.  
  514. html += '<div style="padding: 10px 10px 10px 0px;">'
  515. html += '<form id="getUpMassage_form">'
  516.  
  517. html += ' <div class="javaex-unit clear">'
  518. html += ' <div class="javaex-unit-left"><p class="subtitle required">UID</p></div>'
  519. html += ` <div class="javaex-unit-right" tooltip="请输入用户UID,默认自己" tooltip-pos="up"> `
  520. html += ' <input type="text" class="javaex-text" data-type="positive_int|required" error-msg="请正确输入uid"'
  521. html += ` id="addPlay_uid" value="${Live_info.uid}" placeholder="默认${Live_info.author_uid}"/>`
  522. html += ' </div>'
  523. html += ' </div>'
  524. html += ' <div class="javaex-unit clear">'
  525. html += ' <div class="javaex-unit-left"><p class="subtitle required">BV</p></div>'
  526. html += ' <div class="javaex-unit-right" tooltip="请输入视频BV号 例如BV14r4y1A7Q9 (0表示全部)" tooltip-pos="up">'
  527. html += ' <input type="text" class="javaex-text" data-type="required|english_number" error-msg=""'
  528. html += ` id="addPlay_bv" value="0" placeholder="请输入视频BV号 例如BV14r4y1A7Q9 (0表示全部)"/>`
  529. html += ' </div>'
  530. html += ' </div>'
  531. html += '</form>';
  532. html += ' <div class="javaex-unit clear tc">'
  533. html += ' <button id="addPlay_bgn" class="javaex-btn blue radius-3">开始</button> '
  534. html += ' </div>'
  535. html += '<hr class="javaex-divider"></hr>';
  536. html += '</div>';
  537.  
  538.  
  539. javaex.panel(this, {
  540. position: "right",
  541. content: html
  542. });
  543.  
  544. $("#addPlay_bgn").click(async function () {
  545.  
  546. let MID = javaex.getSelectVal("#addPlay_uid")
  547. let BV = javaex.getSelectVal("#addPlay_bv");
  548. let bvArr = [];
  549. let bvArr2 = [];
  550.  
  551. MID = javaex.ifnull(MID, Live_info.author_uid);
  552.  
  553.  
  554. if (BV == "0") {
  555.  
  556. //1.查询up所有视频BV号 2.发送心跳间隔2s
  557. HNAPI.getUpAllBV(Live_info.author_uid, 1, 2);
  558. await HNTOOL.Sleep(500);
  559. bvArr2 = BiliData.BVList;
  560.  
  561. for (let i = 1; i > 0; i++) {
  562.  
  563. HNAPI.getUpAllBV(MID, i);
  564.  
  565. await HNTOOL.Sleep(500);
  566.  
  567. if (BiliData.BVList.length == 0) {
  568. if (bvArr.length == 0) {
  569. javaex.tip({
  570. content: "该用户无投稿视频,请确认是否有误!",
  571. type: "success"
  572. });
  573. await HNTOOL.Sleep(2000);
  574. }
  575. //bvArr = bvArr.ifnull(bvArr2, bvArr.concat(bvArr2));
  576. bvArr = bvArr.concat(bvArr2);
  577. break;
  578. }
  579.  
  580. bvArr = bvArr.concat(BiliData.BVList);
  581. }
  582.  
  583.  
  584. if (bvArr.length != 0) {
  585.  
  586. for (const emt of bvArr) {
  587.  
  588. HNAPI.playOneVideo(emt.bvid,MID);
  589.  
  590. await HNTOOL.Sleep(2000);
  591.  
  592. }
  593.  
  594. alert("播放完成!!!");
  595.  
  596. }
  597.  
  598.  
  599. } else {
  600. //1.调用点击播放接口
  601. //2.发送心跳
  602.  
  603. await HNTOOL.Sleep(500);
  604.  
  605. HNAPI.playOneVideo(BV);
  606. }
  607.  
  608.  
  609. });
  610.  
  611.  
  612. });
  613.  
  614. }
  615.  
  616.  
  617. let showUnreadMsg = function () {
  618.  
  619. var html = '';
  620. html += '<div class="javaex-panel-box" style="width: 250px;">';
  621. html += ' <ul>';
  622. html += ` <p style="color: #FFF;">用户消息</p>`;
  623. html += ` <li><a href="https://message.bilibili.com/#/at">未读艾特数:${BiliData.Unread.at}</a> </li> `;
  624. html += ` <li><a href="https://message.bilibili.com/#/love">未读点赞数:${BiliData.Unread.like}</a> </li> `;
  625. html += ` <li><a href="https://message.bilibili.com/#/reply">未读回复数:${BiliData.Unread.reply}</a> </li> `;
  626. html += ` <li><a href="https://message.bilibili.com/#/system">未读系统通知数:${BiliData.Unread.sys_msg}</a> </li> `;
  627. html += ` <li><a href="">UP主助手信息数:${BiliData.Unread.up}</a> </li> `;
  628. html += ' <hr class="javaex-divider"></hr>';
  629. html += ' </ul>';
  630. html += '</div>';
  631.  
  632.  
  633. javaex.panel(document.getElementById("getUnread"), {
  634. position: "right",
  635. content: html
  636. });
  637.  
  638. javaex.tip({
  639. content: "查询用户通知成功!",
  640. type: "success"
  641. });
  642.  
  643. }
  644.  
  645. /**
  646. * 检查 艾特信息
  647. * @returns {Promise<void>}
  648. */
  649. let chackAtMsg = async function (items) {
  650.  
  651. await HNTOOL.Sleep(500);
  652.  
  653. if (items != false && typeof (items) != "undefined") {
  654.  
  655.  
  656. var html = '';
  657. html += '<div class="javaex-panel-box" style="width: auto;">';
  658. html += ' <ul>';
  659. html += ` <p style="color: #FFF;">@信息</p>`;
  660. items.forEach(function (item) {
  661.  
  662. var atTime = javaex.dateFormat(item.at_time, 'yyyy-MM-dd HH:mm:ss');
  663.  
  664. html += ` <li>${atTime} <a class="fontbold" href="https://space.bilibili.com/${item.user.mid}">:${item.user.nickname}</a>在动态中艾特了你!</li> `
  665. html += ` <li class="fontbold">内容:${item.item.source_content}</li> `;
  666. html += ` <li><a href="${item.item.uri}">---点击查看详情---</a> </li> `;
  667. html += ' <hr class="javaex-divider"></hr>';
  668. });
  669. html += ' </ul>';
  670. html += '</div>';
  671.  
  672. if (($("#chackList").attr("class")).indexOf("hover") > -1) {
  673. javaex.panel(document.getElementById("getAtMsg"), {
  674. position: "right",
  675. content: html
  676. });
  677. } else {
  678. javaex.panel(document.getElementById("chackList"), {
  679. position: "right",
  680. content: html
  681. });
  682. }
  683.  
  684.  
  685. }
  686.  
  687. }
  688.  
  689.  
  690. let addSetPage = async function () {
  691.  
  692. HNTOOL.Sleep(1000);
  693.  
  694. $('body').append('' +
  695. '<div id="left_navbar">' +
  696. '<div class="javaex-menu-container"> ' +
  697. ' <div id="menu" class="javaex-menu"> ' +
  698. ' <ul> ' +
  699. ' <li class="javaex-menu-item alone hover" > ' +
  700. ' <a href="javaScript:;" style="background: #fb7299;padding-left: 0px;text-align: center;color: #ffffff;font-weight: bold;border-bottom: 1px solid #eee;" ' +
  701. ` tooltip="我是来测试的" tooltip-pos="right">管理中心 v${GM_info.script.version}</a> ` +
  702. ' </li> ' +
  703. ' </ul> ' +
  704. ' <ul> ' +
  705. ' <li class="javaex-menu-item"> ' +
  706. ' <a href="javascript:;">个人中心<i class="icon-angle-down"></i></a> ' +
  707. ' <ul> ' +
  708. ' <li id="biliUserMsg" class="javaex-hover-pulse"><a href="javaScript:;">用户信息</a></li> ' +
  709. ' </ul> ' +
  710. ' </li> ' +
  711. ' <li id="chackList" class="javaex-menu-item"> ' +
  712. ' <a href="javaScript:;">查询<i class="icon-angle-down"></i></a> ' +
  713. ' <ul> ' +
  714. ' <li id="getUpMassage" ><a href="javaScript:;">查询用户详细信息</a></li> ' +
  715. ' <li><a href="javaScript:;">查询每日最热视频【禁用】</a></li> ' +
  716. ' <li id="getAtMsg"><a href="javaScript:;">查询动态艾特信息</a></li> ' +
  717. ' <li id="getCoinLog" class="javaex-hover-pulse"><a href="javaScript:;">查询硬币变化</a></li> ' +
  718. ' <li id="getUnread" class="javaex-hover-pulse"><a href="javaScript:;">查询消息</a></li> ' +
  719. ' </ul> ' +
  720. ' </li> ' +
  721. ' <li class="javaex-menu-item"> ' +
  722. ' <a href="javascript:;">工具<i class="icon-angle-down"></i></a> ' +
  723. ' <ul> ' +
  724. ' <li class="javaex-hover-pulse"><a href="javaScript:;" >移动粉丝勋章【禁用】</a></li> ' +
  725. ' <li id="addPlay" class="javaex-hover-pulse"><a href="javaScript:;" >批量播放视频</a></li> ' +
  726. ' <li id="disUser" class="javaex-hover-pulse"><a href="javaScript:;" >批量拉黑</a></li> ' +
  727. ' <li id="" class="javaex-hover-pulse" ><a href="javaScript:;">动态评论区点赞【禁用】</a></li> ' +//likeInDynamicComments
  728. ' <li id="HNSetup" class="javaex-hover-pulse" ><span class="icon-cogs" style="font-size:50px;color:#ff0000;"></span><a href="javaScript:;">设置【禁用】</a></li> ' +
  729. ' </ul> ' +
  730. ' </li> ' +
  731.  
  732.  
  733. ' </ul> ' +
  734. ' </div> ' +
  735. '</div>' +
  736. '</div>');
  737. //$("#left_navbar").before('<button hnBtn="btn_open" class="javaex-btn green" onclick="openBar()">点我关闭</button>')
  738.  
  739. javaex.menu({
  740. id: "menu",
  741. isShowAll: false
  742. });
  743.  
  744. }
  745.  
  746.  
  747. let addStyle = function () {
  748. const animate = GM_getResourceText('animate');
  749. const common = GM_getResourceText('common');
  750. const defaultCss = GM_getResourceText('defaultCss');
  751. //const icomoonex = GM_getResourceText('icomoonex');
  752. //const AllCss = animate + common + defaultCss + icomoonex;
  753. const AllCss = animate + common + defaultCss;
  754. const style = document.createElement('style');
  755. style.innerHTML = AllCss;
  756. return document.getElementsByTagName('head')[0].appendChild(style);
  757. }
  758.  
  759. /*-----------------------------------------------点击事件绑定--------------------------------------------------*/
  760.  
  761.  
  762. /*-----------------------------------------------常用API--------------------------------------------------*/
  763.  
  764. let HNAPI = {
  765.  
  766. /**
  767. * https://api.bilibili.com/x/relation/batch/modify
  768. * @param uid
  769. */
  770.  
  771. batchFollowByUid:function (uids) {
  772. Ajax.post({
  773. url: BILIBLI_PATH.api + "/x/relation/batch/modify",
  774. hasCookies: true,
  775. dataType: 'application/x-www-form-urlencoded; charset=UTF-8',
  776. data: {
  777.  
  778. "fids": uids,
  779. "act": 1, //1 关注 5 拉黑
  780. "re_src": 151,
  781. csrf: Live_info.csrf_token,
  782. },
  783. success: responseText => {
  784.  
  785. let json = JSON.parse(responseText);
  786.  
  787. if (/^{"code":0/.test(responseText)) {
  788.  
  789. javaex.tip({
  790. content: "批量关注成功!" + json.message,
  791. type: "success"
  792. });
  793.  
  794. } else {
  795. javaex.tip({
  796. content: "批量关注失败!" + json.message,
  797. type: "error"
  798. });
  799. }
  800. }
  801. })
  802. },
  803.  
  804.  
  805. /**
  806. * //https://api.bilibili.com/x/relation/modify
  807. *
  808. * @param UID
  809. */
  810. disUserByUid:function (vmid) {
  811.  
  812. Ajax.post({
  813. url: BILIBLI_PATH.api + '/x/relation/modify',
  814. data: {
  815. fid: parseInt(vmid),
  816. re_src: 11,
  817. act: 5,
  818. jsonp: "jsonp",
  819. csrf: Live_info.csrf_token,
  820. },
  821. hasCookies: true,
  822. dataType: 'application/x-www-form-urlencoded; charset=UTF-8',
  823. success: responseText => {
  824.  
  825. var json = JSON.parse(responseText);
  826.  
  827. if (json.code != 0) {
  828.  
  829. console.log("拉黑用户失败uid:" + vmid + " 错误信息"+ json.message);
  830. javaex.tip({
  831. content: "拉黑用户失败uid:" + vmid + " 错误信息"+ json.message,
  832. type: "error"
  833. });
  834.  
  835. }else{
  836. console.log("拉黑用户成功 uid:" + vmid);
  837. javaex.tip({
  838. content: "拉黑用户成功 uid:" + vmid,
  839. type: "success"
  840. });
  841. }
  842. }
  843. });
  844.  
  845. },
  846. /**
  847. * http://api.bilibili.com/x/web-interface/view
  848. * @param BV
  849. * 心跳间隔15秒
  850. */
  851. playOneVideo: function (BV,MID) {
  852.  
  853. //1.调用点击播放接口 https://api.bilibili.com/x/click-interface/click/web/h5
  854. Ajax.post({
  855. url: BILIBLI_PATH.api + "/x/click-interface/click/web/h5",
  856. hasCookies: true,
  857. dataType: 'application/x-www-form-urlencoded; charset=UTF-8',
  858. data: {
  859.  
  860. "bvid": BV,
  861. "part": 1,
  862. "mid": Live_info.uid,
  863. "lv": 2,
  864. "jsonp": "jsonp",
  865. "type": 3,
  866. "sub_type": 0
  867. },
  868. success: responseText => {
  869.  
  870. let json = JSON.parse(responseText);
  871.  
  872. if (/^{"code":0/.test(responseText)) {
  873. //发送一次心跳
  874. HNAPI.sendHeartbeat(BV,MID);
  875.  
  876. } else {
  877. javaex.tip({
  878. content: "视频" + data.bvid + "播放失败!" + json.message,
  879. type: "error"
  880. });
  881. }
  882. }
  883. })
  884.  
  885.  
  886. },
  887.  
  888. //上报视频播放心跳(web端)
  889. /**
  890. * http://api.bilibili.com/x/click-interface/web/heartbeat
  891. * @param json
  892. */
  893. sendHeartbeat: function (bvid,MID) {
  894.  
  895. Ajax.post({
  896. url: BILIBLI_PATH.api + "/x/click-interface/web/heartbeat",
  897. hasCookies: true,
  898. dataType: 'application/x-www-form-urlencoded; charset=UTF-8',
  899. //aid=756988764&bvid=BV1wr4y1P782&cid=304166326&page=1
  900. data: {
  901. "bvid": bvid,
  902. "type": 3,
  903. "dt": 2,
  904. "played_time": HNTOOL.random(12, 40),
  905. "realtime": HNTOOL.random(12, 40),
  906. "play_type": 0
  907. },
  908. success: responseText => {
  909.  
  910. if (/^{"code":0/.test(responseText) && MID != Live_info.author_uid) {
  911.  
  912. javaex.tip({
  913. content: "视频BV" + bvid + "播放一次!",
  914. type: "success"
  915. });
  916. }
  917. }
  918. })
  919.  
  920. },
  921.  
  922. //获取up所有投稿视频
  923. /**
  924. * https://api.bilibili.com/x/space/arc/search?mid=99439379&ps=30&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp
  925. * @param mid
  926. */
  927. getUpAllBV: function (mid, pn, ps) {
  928. ps = (ps === undefined ? 30 : ps); // a默认值为1
  929. Ajax.get({
  930. url: BILIBLI_PATH.api + '/x/space/arc/search',
  931. queryStringsObj: {
  932. mid: mid,
  933. tid: 0,
  934. order: 'pubdate',
  935. jsonp: 'jsonp',
  936. pn: pn,
  937. ps: ps
  938. },
  939. hasCookies: true,
  940. success: responseText => {
  941.  
  942. var json = JSON.parse(responseText);
  943.  
  944. if (json.code != 0) {
  945.  
  946. javaex.tip({
  947. content: "获取up所有投稿视频失败!" + json.message,
  948. type: "success"
  949. });
  950.  
  951. } else {
  952. BiliData.BVList = json.data.list.vlist;
  953.  
  954. }
  955. }
  956. });
  957.  
  958. },
  959.  
  960.  
  961. /**
  962. *
  963. * @param path
  964. * @param data
  965. * @param saveObj
  966. * @param Func
  967. */
  968. publicGet: function (path, queryStringsObj) {
  969.  
  970. if (typeof (queryStringsObj) != "undefined") {
  971. queryStringsObj = {};
  972. }
  973. Ajax.get({
  974. url: BILIBLI_PATH.api + path,
  975. queryStringsObj: queryStringsObj,
  976. hasCookies: true,
  977. success: responseText => {
  978.  
  979. let json = JSON.parse(responseText);
  980. if (chackCode(json.code) && json.code == 0) {
  981.  
  982. return json.data;
  983. }
  984.  
  985. return false;
  986.  
  987. }
  988. })
  989.  
  990. },
  991.  
  992. /**
  993. * 导航栏用户信息
  994. */
  995. getUserNav: async function () {
  996.  
  997. GM_xmlhttpRequest({
  998. method: "GET",
  999. url: BILIBLI_PATH.api + "/x/web-interface/nav",
  1000. //timeout:3000,
  1001. onload: function (response) {
  1002.  
  1003. let json = JSON.parse(response.response);
  1004. console.log(json);
  1005. BiliData.login_code = json.code;
  1006. BiliData.login_message = json.message;
  1007. BiliData.LoginData = json.data;
  1008.  
  1009. }
  1010. });
  1011.  
  1012. },
  1013.  
  1014. /**
  1015. * 获取某up详细信息
  1016. * http://api.bilibili.com/x/web-interface/card
  1017. */
  1018. getUpCard: function (mid) {
  1019.  
  1020. Ajax.get({
  1021. url: BILIBLI_PATH.api + '/x/web-interface/card',
  1022. queryStringsObj: {
  1023. mid: mid,
  1024. photo: false
  1025. },
  1026. hasCookies: true,
  1027. success: responseText => {
  1028.  
  1029. var json = JSON.parse(responseText);
  1030.  
  1031. if (json.code != 0) {
  1032.  
  1033. javaex.tip({
  1034. content: "查询失败!" + json.message,
  1035. type: "success"
  1036. });
  1037. return false;
  1038. } else {
  1039. BiliData.upCard = json;
  1040.  
  1041. }
  1042.  
  1043.  
  1044. }
  1045. })
  1046.  
  1047. },
  1048.  
  1049. /**
  1050. *
  1051. * @param uid 用户uid
  1052. * @param ps 每页视频个数 默认30个
  1053. * @param pn 第几页
  1054. */
  1055. getUpAllVideo: function (uid, ps, pn) {
  1056.  
  1057. },
  1058. getChallenge: async function () {
  1059.  
  1060.  
  1061. },
  1062. /**
  1063. * 硬币记录
  1064. * @returns {Promise<void>}
  1065. */
  1066. getCoinLog: async function () {
  1067.  
  1068. GM_xmlhttpRequest({
  1069. method: "GET",
  1070. url: BILIBLI_PATH.api + "/x/member/web/coin/log",
  1071. //url: "http://api.bilibili.com/x/member/web/coin/log",
  1072. onload: function (response) {
  1073.  
  1074. let json = JSON.parse(response.response);
  1075. if (json.code == 0) {
  1076.  
  1077. console.log("硬币记录" + json);
  1078. BiliData.CoinLog = json.data;
  1079.  
  1080. return true;
  1081. }
  1082.  
  1083. }
  1084. });
  1085. },
  1086. /**
  1087. * 未读消息数 http://api.bilibili.com/x/msgfeed/unread
  1088. * @returns {Promise<void>}
  1089. */
  1090. getUnread: async function () {
  1091.  
  1092. Ajax.get({
  1093. url: BILIBLI_PATH.api + "/x/msgfeed/unread",
  1094. hasCookies: true,
  1095. success: responseText => {
  1096.  
  1097. let json = JSON.parse(responseText);
  1098. if (chackCode(json.code) && json.code == 0) {
  1099.  
  1100. BiliData.Unread = json.data;
  1101. showUnreadMsg();
  1102. }
  1103.  
  1104. return false;
  1105.  
  1106. }
  1107. })
  1108.  
  1109. /* GM_xmlhttpRequest({
  1110. method: "GET",
  1111. url: BILIBLI_PATH.api + "/x/msgfeed/unread",
  1112. onload: function (response) {
  1113.  
  1114. let json = JSON.parse(response.response);
  1115.  
  1116. if (chackCode(json.code) && json.code == 0) {
  1117.  
  1118. BiliData.Unread = json.code;
  1119. showUnreadMsg();
  1120.  
  1121. }
  1122.  
  1123. }
  1124. });*/
  1125. },
  1126. /**
  1127. *
  1128. * @param type reply:回复我的 at @我的
  1129. */
  1130. getMsgfeed: function (type) {
  1131. Ajax.get({
  1132. url: BILIBLI_PATH.api + '/x/msgfeed/' + type,
  1133. queryStringsObj: {
  1134. build: 0,
  1135. mobi_app: 'web'
  1136. },
  1137. hasCookies: true,
  1138. success: responseText => {
  1139.  
  1140. var json = JSON.parse(responseText);
  1141.  
  1142. if (json.code != 0 || !json.data.cursor.is_end || json.data.items == null) {
  1143. return false;
  1144. } else {
  1145. chackAtMsg(json.data.items);
  1146.  
  1147. return true;
  1148. }
  1149.  
  1150.  
  1151. }
  1152. })
  1153.  
  1154. },
  1155.  
  1156. /** 未读消息数 http://api.bilibili.com/x/space/arc/search?mid=99439379&pn=1&ps=5
  1157. * @returns {Promise<void>}
  1158. */
  1159. getUnrewad: async function () {
  1160.  
  1161. GM_xmlhttpRequest({
  1162. method: "GET",
  1163. data: {
  1164. "mid": 99439379,
  1165. "pn": 1,
  1166. "ps": 5,
  1167. },
  1168. url: BILIBLI_PATH.api + "/x/space/arc/search",
  1169. onload: function (response) {
  1170.  
  1171. let json = JSON.parse(response.response);
  1172. if (json.code == 0) {
  1173.  
  1174. BiliData.Video = json.data;
  1175. console.log("未读消息数" + json);
  1176. }
  1177.  
  1178. }
  1179. });
  1180. },
  1181.  
  1182. /** 动态评论区点赞 https://api.bilibili.com/x/v2/reply/action
  1183. * @param {number} oid
  1184. * @param {nbumber} rpid
  1185. * @param {number} action
  1186. * @returns {Promise<void>}
  1187. * type: 11
  1188. * ordering: heat
  1189. * jsonp: jsonp
  1190. * csrf: 54a******* CSRF Token(位于cookie)
  1191. */
  1192.  
  1193. likeInDynamicComments: async function (oid, rpid, action) {
  1194.  
  1195.  
  1196. Ajax.post({
  1197. url: BILIBLI_PATH.api + "/x/v2/reply/action",
  1198. hasCookies: true,
  1199. dataType: 'application/x-www-form-urlencoded; charset=UTF-8',
  1200. data: {
  1201. "oid": oid,
  1202. "type": 11,
  1203. "rpid": rpid,
  1204. "action": action,
  1205. "ordering": "heat",
  1206. "jsonp": "jsonp",
  1207. "csrf": Live_info.csrf_token,
  1208. },
  1209. success: responseText => {
  1210.  
  1211. if (/^{"code":0/.test(responseText)) {
  1212.  
  1213. BiliData.replyAction = JSON.parse(responseText);
  1214. console.log("点赞成功!" + responseText);
  1215. }
  1216. }
  1217. })
  1218.  
  1219. },
  1220.  
  1221.  
  1222. }
  1223.  
  1224. /*-----------------------------------------------常用工具函数--------------------------------------------------*/
  1225.  
  1226.  
  1227. let HNTOOL = {
  1228.  
  1229. /**
  1230. * 获取cookie 中某个键对应的值
  1231. * @param name
  1232. */
  1233.  
  1234. getCookie: function (name) {
  1235. let arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  1236. if (arr != null) return unescape(arr[2]);
  1237. return false;
  1238. },
  1239. /**
  1240. * 得到一个字符串数组的中随机一个
  1241. * @param {List} list
  1242. */
  1243. getRandomWordOfList: function (list) {
  1244. var L = list.length;
  1245. var i = Math.ceil(Math.random() * L);
  1246.  
  1247. return list[i];
  1248. },
  1249. random: function (min, max) {
  1250.  
  1251. return Math.floor(Math.random() * (max - min)) + min;
  1252. },
  1253.  
  1254. Sleep: function (ms) {
  1255. return new Promise(resolve => setTimeout(() => resolve('sleep'), ms));
  1256. }
  1257.  
  1258. }
  1259.  
  1260. let errorMsg = {
  1261. "-1": "应用程序不存在或已被封禁!",
  1262. "-2": "Access Key错误!",
  1263. "-3": "API校验密匙错误!",
  1264. "-4": "调用方对该Method没有权限!",
  1265. "-101": "账号未登录!",
  1266. "-102": "账号被封停!",
  1267. "-103": "积分不足!",
  1268. "-104": "硬币不足!",
  1269. "-105": "验证码错误!",
  1270. "-106": "账号非正式会员或在适应期!",
  1271. "-107": "应用不存在或者被封禁!",
  1272. "-108": "未绑定手机!",
  1273. "-109": "未知!",
  1274. "-110": "未绑定手机!",
  1275. "-111": "csrf 校验失败!",
  1276. "-112": "系统升级中!",
  1277. "-113": "账号尚未实名认证!",
  1278. "-114": "请先绑定手机!",
  1279. "-115": "请先完成实名认证!",
  1280. "-304": "木有改动!",
  1281. "-307": "撞车跳转!",
  1282. "-400": "请求错误!",
  1283. "-401": "未认证!",
  1284. "-403": "访问权限不足!",
  1285. "-404": "啥都木有!",
  1286. }
  1287.  
  1288. /*-----------------------------------------------CODE 统一检查--------------------------------------------------*/
  1289. /**
  1290. *
  1291. * @param code
  1292. * @constructor
  1293. */
  1294. let chackCode = function (code) {
  1295.  
  1296. let emsg = errorMsg[code + ""];
  1297. if (emsg != null) {
  1298. javaex.tip({content: emsg, type: "error"});
  1299.  
  1300. return false;
  1301. }
  1302.  
  1303. return true;
  1304. }
  1305.  
  1306.  
  1307. /*-----------------------------------------------localStorage封装 (本地存储信息)--------------------------------------------------*/
  1308. //javaex.setLocalStorage(key, value); var str = javaex.getLocalStorage(key); javaex.deleteLocalStorage(key);
  1309.  
  1310. /*window.onbeforeunload = function() {
  1311. if (!canLeavePage()) {
  1312. return ('确认离开当前页面吗?未保存的数据将会丢失!');
  1313. }
  1314. };*/
  1315.  
  1316.  
  1317. /**
  1318. * Ajax请求对象
  1319. */
  1320. const Ajax = (() => {
  1321. /**
  1322. * 检查options是否符合要求
  1323. * @param {object} options
  1324. * @returns {boolean}
  1325. */
  1326. function checkOptions(options) {
  1327. let result = false;
  1328. if (typeof options !== 'object') {
  1329. console.warn('类型错误: typeof Options !== Object');
  1330. return result;
  1331. } else {
  1332. if (typeof options.url !== 'string') {
  1333. console.warn('类型错误: typeof Link !== Strings');
  1334. return result;
  1335. } else {
  1336. const reg = /^https?:\/\/(?:\w+\.?)+(?:\/.*)*\/?$/i;
  1337. if (!reg.test(options.url)) {
  1338. console.warn('url字符串须为完整http链接');
  1339. return result;
  1340. }
  1341. result = true;
  1342. }
  1343. }
  1344. return result;
  1345. }
  1346.  
  1347. /**
  1348. * 对象转URL编码
  1349. * @param {object} data
  1350. */
  1351. function objToURLCode(data) {
  1352. var _result = [];
  1353. for (var key in data) {
  1354. var value = data[key];
  1355. if (value instanceof Array) {
  1356. value.forEach(function (_value) {
  1357. _result.push(key + "=" + _value);
  1358. });
  1359. } else {
  1360. _result.push(key + '=' + value);
  1361. }
  1362. }
  1363. return _result.join('&');
  1364. }
  1365.  
  1366. /**
  1367. * 请求
  1368. * @param {string} method
  1369. * @param {object} options
  1370. */
  1371. function request(method, options) {
  1372. if (checkOptions(options)) {
  1373. let xhr = new XMLHttpRequest();
  1374. const {url: _url, queryStringsObj, data, dataType, hasCookies} = options
  1375. , url = typeof queryStringsObj === 'object' ?
  1376. _url + '?' + objToURLCode(queryStringsObj) : _url;
  1377. switch (method) {
  1378. case 'GET':
  1379. xhr.open("GET", url);
  1380. break;
  1381. case 'POST':
  1382. xhr.open("POST", url);
  1383. xhr.setRequestHeader('Content-Type', dataType);
  1384. break;
  1385. default:
  1386. break;
  1387. }
  1388. if (hasCookies) xhr.withCredentials = true;
  1389. xhr.timeout = 3000;
  1390. xhr.addEventListener('load', () => {
  1391. if (xhr.status === 200) {
  1392. options.success(xhr.responseText)
  1393. } else {
  1394. console.error(`status:${xhr.status}`);
  1395. options.success(`{"code":${xhr.status},"msg":"频繁访问"}`);
  1396. }
  1397. })
  1398. xhr.addEventListener('error', () => {
  1399. console.error('ajax请求出错')
  1400. options.success('{"code":-1,"msg":"ajax请求出错"}');
  1401. })
  1402. xhr.addEventListener('timeout', () => {
  1403. console.error('请求超时')
  1404. options.success('{"code":-1,"msg":"请求超时"}');
  1405. })
  1406. switch (method) {
  1407. case 'GET':
  1408. xhr.send()
  1409. break;
  1410. case 'POST':
  1411. xhr.send((/urlencoded/.test(dataType)) ? objToURLCode(data) : data)
  1412. break;
  1413. default:
  1414. break;
  1415. }
  1416. }
  1417. }
  1418.  
  1419. return {
  1420. /**
  1421. * 发送Get请求
  1422. * @param {Object} options
  1423. */
  1424. get(options) {
  1425. request("GET", options);
  1426. },
  1427. /**
  1428. * 发送Post请求
  1429. * @param {object} options
  1430. */
  1431. post(options) {
  1432. request("POST", options);
  1433. }
  1434. }
  1435. })()