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

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

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/422731/907480/%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.1
  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="batchFollow" class="javaex-hover-pulse"><a href="javaScript:;" >批量关注</a></li> ' +
  728. ' <li id="" class="javaex-hover-pulse" ><a href="javaScript:;">动态评论区点赞【禁用】</a></li> ' +//likeInDynamicComments
  729. ' <li id="HNSetup" class="javaex-hover-pulse" ><span class="icon-cogs" style="font-size:50px;color:#ff0000;"></span><a href="javaScript:;">设置【禁用】</a></li> ' +
  730. ' </ul> ' +
  731. ' </li> ' +
  732.  
  733.  
  734. ' </ul> ' +
  735. ' </div> ' +
  736. '</div>' +
  737. '</div>');
  738. //$("#left_navbar").before('<button hnBtn="btn_open" class="javaex-btn green" onclick="openBar()">点我关闭</button>')
  739.  
  740. javaex.menu({
  741. id: "menu",
  742. isShowAll: false
  743. });
  744.  
  745. }
  746.  
  747.  
  748. let addStyle = function () {
  749. const animate = GM_getResourceText('animate');
  750. const common = GM_getResourceText('common');
  751. const defaultCss = GM_getResourceText('defaultCss');
  752. //const icomoonex = GM_getResourceText('icomoonex');
  753. //const AllCss = animate + common + defaultCss + icomoonex;
  754. const AllCss = animate + common + defaultCss;
  755. const style = document.createElement('style');
  756. style.innerHTML = AllCss;
  757. return document.getElementsByTagName('head')[0].appendChild(style);
  758. }
  759.  
  760. /*-----------------------------------------------点击事件绑定--------------------------------------------------*/
  761.  
  762.  
  763. /*-----------------------------------------------常用API--------------------------------------------------*/
  764.  
  765. let HNAPI = {
  766.  
  767. /**
  768. * https://api.bilibili.com/x/relation/batch/modify
  769. * @param uid
  770. */
  771.  
  772. batchFollowByUid:function (uids) {
  773. Ajax.post({
  774. url: BILIBLI_PATH.api + "/x/relation/batch/modify",
  775. hasCookies: true,
  776. dataType: 'application/x-www-form-urlencoded; charset=UTF-8',
  777. data: {
  778.  
  779. "fids": uids,
  780. "act": 1, //1 关注 5 拉黑
  781. "re_src": 151,
  782. csrf: Live_info.csrf_token,
  783. },
  784. success: responseText => {
  785.  
  786. let json = JSON.parse(responseText);
  787.  
  788. if (/^{"code":0/.test(responseText)) {
  789.  
  790. javaex.tip({
  791. content: "批量关注成功!" + json.message,
  792. type: "success"
  793. });
  794.  
  795. } else {
  796. javaex.tip({
  797. content: "批量关注失败!" + json.message,
  798. type: "error"
  799. });
  800. }
  801. }
  802. })
  803. },
  804.  
  805.  
  806. /**
  807. * //https://api.bilibili.com/x/relation/modify
  808. *
  809. * @param UID
  810. */
  811. disUserByUid:function (vmid) {
  812.  
  813. Ajax.post({
  814. url: BILIBLI_PATH.api + '/x/relation/modify',
  815. data: {
  816. fid: parseInt(vmid),
  817. re_src: 11,
  818. act: 5,
  819. jsonp: "jsonp",
  820. csrf: Live_info.csrf_token,
  821. },
  822. hasCookies: true,
  823. dataType: 'application/x-www-form-urlencoded; charset=UTF-8',
  824. success: responseText => {
  825.  
  826. var json = JSON.parse(responseText);
  827.  
  828. if (json.code != 0) {
  829.  
  830. console.log("拉黑用户失败uid:" + vmid + " 错误信息"+ json.message);
  831. javaex.tip({
  832. content: "拉黑用户失败uid:" + vmid + " 错误信息"+ json.message,
  833. type: "error"
  834. });
  835.  
  836. }else{
  837. console.log("拉黑用户成功 uid:" + vmid);
  838. javaex.tip({
  839. content: "拉黑用户成功 uid:" + vmid,
  840. type: "success"
  841. });
  842. }
  843. }
  844. });
  845.  
  846. },
  847. /**
  848. * http://api.bilibili.com/x/web-interface/view
  849. * @param BV
  850. * 心跳间隔15秒
  851. */
  852. playOneVideo: function (BV,MID) {
  853.  
  854. //1.调用点击播放接口 https://api.bilibili.com/x/click-interface/click/web/h5
  855. Ajax.post({
  856. url: BILIBLI_PATH.api + "/x/click-interface/click/web/h5",
  857. hasCookies: true,
  858. dataType: 'application/x-www-form-urlencoded; charset=UTF-8',
  859. data: {
  860.  
  861. "bvid": BV,
  862. "part": 1,
  863. "mid": Live_info.uid,
  864. "lv": 2,
  865. "jsonp": "jsonp",
  866. "type": 3,
  867. "sub_type": 0
  868. },
  869. success: responseText => {
  870.  
  871. let json = JSON.parse(responseText);
  872.  
  873. if (/^{"code":0/.test(responseText)) {
  874. //发送一次心跳
  875. HNAPI.sendHeartbeat(BV,MID);
  876.  
  877. } else {
  878. javaex.tip({
  879. content: "视频" + data.bvid + "播放失败!" + json.message,
  880. type: "error"
  881. });
  882. }
  883. }
  884. })
  885.  
  886.  
  887. },
  888.  
  889. //上报视频播放心跳(web端)
  890. /**
  891. * http://api.bilibili.com/x/click-interface/web/heartbeat
  892. * @param json
  893. */
  894. sendHeartbeat: function (bvid,MID) {
  895.  
  896. Ajax.post({
  897. url: BILIBLI_PATH.api + "/x/click-interface/web/heartbeat",
  898. hasCookies: true,
  899. dataType: 'application/x-www-form-urlencoded; charset=UTF-8',
  900. //aid=756988764&bvid=BV1wr4y1P782&cid=304166326&page=1
  901. data: {
  902. "bvid": bvid,
  903. "type": 3,
  904. "dt": 2,
  905. "played_time": HNTOOL.random(12, 40),
  906. "realtime": HNTOOL.random(12, 40),
  907. "play_type": 0
  908. },
  909. success: responseText => {
  910.  
  911. if (/^{"code":0/.test(responseText) && MID != Live_info.author_uid) {
  912.  
  913. javaex.tip({
  914. content: "视频BV" + bvid + "播放一次!",
  915. type: "success"
  916. });
  917. }
  918. }
  919. })
  920.  
  921. },
  922.  
  923. //获取up所有投稿视频
  924. /**
  925. * https://api.bilibili.com/x/space/arc/search?mid=99439379&ps=30&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp
  926. * @param mid
  927. */
  928. getUpAllBV: function (mid, pn, ps) {
  929. ps = (ps === undefined ? 30 : ps); // a默认值为1
  930. Ajax.get({
  931. url: BILIBLI_PATH.api + '/x/space/arc/search',
  932. queryStringsObj: {
  933. mid: mid,
  934. tid: 0,
  935. order: 'pubdate',
  936. jsonp: 'jsonp',
  937. pn: pn,
  938. ps: ps
  939. },
  940. hasCookies: true,
  941. success: responseText => {
  942.  
  943. var json = JSON.parse(responseText);
  944.  
  945. if (json.code != 0) {
  946.  
  947. javaex.tip({
  948. content: "获取up所有投稿视频失败!" + json.message,
  949. type: "success"
  950. });
  951.  
  952. } else {
  953. BiliData.BVList = json.data.list.vlist;
  954.  
  955. }
  956. }
  957. });
  958.  
  959. },
  960.  
  961.  
  962. /**
  963. *
  964. * @param path
  965. * @param data
  966. * @param saveObj
  967. * @param Func
  968. */
  969. publicGet: function (path, queryStringsObj) {
  970.  
  971. if (typeof (queryStringsObj) != "undefined") {
  972. queryStringsObj = {};
  973. }
  974. Ajax.get({
  975. url: BILIBLI_PATH.api + path,
  976. queryStringsObj: queryStringsObj,
  977. hasCookies: true,
  978. success: responseText => {
  979.  
  980. let json = JSON.parse(responseText);
  981. if (chackCode(json.code) && json.code == 0) {
  982.  
  983. return json.data;
  984. }
  985.  
  986. return false;
  987.  
  988. }
  989. })
  990.  
  991. },
  992.  
  993. /**
  994. * 导航栏用户信息
  995. */
  996. getUserNav: async function () {
  997.  
  998. GM_xmlhttpRequest({
  999. method: "GET",
  1000. url: BILIBLI_PATH.api + "/x/web-interface/nav",
  1001. //timeout:3000,
  1002. onload: function (response) {
  1003.  
  1004. let json = JSON.parse(response.response);
  1005. console.log(json);
  1006. BiliData.login_code = json.code;
  1007. BiliData.login_message = json.message;
  1008. BiliData.LoginData = json.data;
  1009.  
  1010. }
  1011. });
  1012.  
  1013. },
  1014.  
  1015. /**
  1016. * 获取某up详细信息
  1017. * http://api.bilibili.com/x/web-interface/card
  1018. */
  1019. getUpCard: function (mid) {
  1020.  
  1021. Ajax.get({
  1022. url: BILIBLI_PATH.api + '/x/web-interface/card',
  1023. queryStringsObj: {
  1024. mid: mid,
  1025. photo: false
  1026. },
  1027. hasCookies: true,
  1028. success: responseText => {
  1029.  
  1030. var json = JSON.parse(responseText);
  1031.  
  1032. if (json.code != 0) {
  1033.  
  1034. javaex.tip({
  1035. content: "查询失败!" + json.message,
  1036. type: "success"
  1037. });
  1038. return false;
  1039. } else {
  1040. BiliData.upCard = json;
  1041.  
  1042. }
  1043.  
  1044.  
  1045. }
  1046. })
  1047.  
  1048. },
  1049.  
  1050. /**
  1051. *
  1052. * @param uid 用户uid
  1053. * @param ps 每页视频个数 默认30个
  1054. * @param pn 第几页
  1055. */
  1056. getUpAllVideo: function (uid, ps, pn) {
  1057.  
  1058. },
  1059. getChallenge: async function () {
  1060.  
  1061.  
  1062. },
  1063. /**
  1064. * 硬币记录
  1065. * @returns {Promise<void>}
  1066. */
  1067. getCoinLog: async function () {
  1068.  
  1069. GM_xmlhttpRequest({
  1070. method: "GET",
  1071. url: BILIBLI_PATH.api + "/x/member/web/coin/log",
  1072. //url: "http://api.bilibili.com/x/member/web/coin/log",
  1073. onload: function (response) {
  1074.  
  1075. let json = JSON.parse(response.response);
  1076. if (json.code == 0) {
  1077.  
  1078. console.log("硬币记录" + json);
  1079. BiliData.CoinLog = json.data;
  1080.  
  1081. return true;
  1082. }
  1083.  
  1084. }
  1085. });
  1086. },
  1087. /**
  1088. * 未读消息数 http://api.bilibili.com/x/msgfeed/unread
  1089. * @returns {Promise<void>}
  1090. */
  1091. getUnread: async function () {
  1092.  
  1093. Ajax.get({
  1094. url: BILIBLI_PATH.api + "/x/msgfeed/unread",
  1095. hasCookies: true,
  1096. success: responseText => {
  1097.  
  1098. let json = JSON.parse(responseText);
  1099. if (chackCode(json.code) && json.code == 0) {
  1100.  
  1101. BiliData.Unread = json.data;
  1102. showUnreadMsg();
  1103. }
  1104.  
  1105. return false;
  1106.  
  1107. }
  1108. })
  1109.  
  1110. /* GM_xmlhttpRequest({
  1111. method: "GET",
  1112. url: BILIBLI_PATH.api + "/x/msgfeed/unread",
  1113. onload: function (response) {
  1114.  
  1115. let json = JSON.parse(response.response);
  1116.  
  1117. if (chackCode(json.code) && json.code == 0) {
  1118.  
  1119. BiliData.Unread = json.code;
  1120. showUnreadMsg();
  1121.  
  1122. }
  1123.  
  1124. }
  1125. });*/
  1126. },
  1127. /**
  1128. *
  1129. * @param type reply:回复我的 at @我的
  1130. */
  1131. getMsgfeed: function (type) {
  1132. Ajax.get({
  1133. url: BILIBLI_PATH.api + '/x/msgfeed/' + type,
  1134. queryStringsObj: {
  1135. build: 0,
  1136. mobi_app: 'web'
  1137. },
  1138. hasCookies: true,
  1139. success: responseText => {
  1140.  
  1141. var json = JSON.parse(responseText);
  1142.  
  1143. if (json.code != 0 || !json.data.cursor.is_end || json.data.items == null) {
  1144. return false;
  1145. } else {
  1146. chackAtMsg(json.data.items);
  1147.  
  1148. return true;
  1149. }
  1150.  
  1151.  
  1152. }
  1153. })
  1154.  
  1155. },
  1156.  
  1157. /** 未读消息数 http://api.bilibili.com/x/space/arc/search?mid=99439379&pn=1&ps=5
  1158. * @returns {Promise<void>}
  1159. */
  1160. getUnrewad: async function () {
  1161.  
  1162. GM_xmlhttpRequest({
  1163. method: "GET",
  1164. data: {
  1165. "mid": 99439379,
  1166. "pn": 1,
  1167. "ps": 5,
  1168. },
  1169. url: BILIBLI_PATH.api + "/x/space/arc/search",
  1170. onload: function (response) {
  1171.  
  1172. let json = JSON.parse(response.response);
  1173. if (json.code == 0) {
  1174.  
  1175. BiliData.Video = json.data;
  1176. console.log("未读消息数" + json);
  1177. }
  1178.  
  1179. }
  1180. });
  1181. },
  1182.  
  1183. /** 动态评论区点赞 https://api.bilibili.com/x/v2/reply/action
  1184. * @param {number} oid
  1185. * @param {nbumber} rpid
  1186. * @param {number} action
  1187. * @returns {Promise<void>}
  1188. * type: 11
  1189. * ordering: heat
  1190. * jsonp: jsonp
  1191. * csrf: 54a******* CSRF Token(位于cookie)
  1192. */
  1193.  
  1194. likeInDynamicComments: async function (oid, rpid, action) {
  1195.  
  1196.  
  1197. Ajax.post({
  1198. url: BILIBLI_PATH.api + "/x/v2/reply/action",
  1199. hasCookies: true,
  1200. dataType: 'application/x-www-form-urlencoded; charset=UTF-8',
  1201. data: {
  1202. "oid": oid,
  1203. "type": 11,
  1204. "rpid": rpid,
  1205. "action": action,
  1206. "ordering": "heat",
  1207. "jsonp": "jsonp",
  1208. "csrf": Live_info.csrf_token,
  1209. },
  1210. success: responseText => {
  1211.  
  1212. if (/^{"code":0/.test(responseText)) {
  1213.  
  1214. BiliData.replyAction = JSON.parse(responseText);
  1215. console.log("点赞成功!" + responseText);
  1216. }
  1217. }
  1218. })
  1219.  
  1220. },
  1221.  
  1222.  
  1223. }
  1224.  
  1225. /*-----------------------------------------------常用工具函数--------------------------------------------------*/
  1226.  
  1227.  
  1228. let HNTOOL = {
  1229.  
  1230. /**
  1231. * 获取cookie 中某个键对应的值
  1232. * @param name
  1233. */
  1234.  
  1235. getCookie: function (name) {
  1236. let arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  1237. if (arr != null) return unescape(arr[2]);
  1238. return false;
  1239. },
  1240. /**
  1241. * 得到一个字符串数组的中随机一个
  1242. * @param {List} list
  1243. */
  1244. getRandomWordOfList: function (list) {
  1245. var L = list.length;
  1246. var i = Math.ceil(Math.random() * L);
  1247.  
  1248. return list[i];
  1249. },
  1250. random: function (min, max) {
  1251.  
  1252. return Math.floor(Math.random() * (max - min)) + min;
  1253. },
  1254.  
  1255. Sleep: function (ms) {
  1256. return new Promise(resolve => setTimeout(() => resolve('sleep'), ms));
  1257. }
  1258.  
  1259. }
  1260.  
  1261. let errorMsg = {
  1262. "-1": "应用程序不存在或已被封禁!",
  1263. "-2": "Access Key错误!",
  1264. "-3": "API校验密匙错误!",
  1265. "-4": "调用方对该Method没有权限!",
  1266. "-101": "账号未登录!",
  1267. "-102": "账号被封停!",
  1268. "-103": "积分不足!",
  1269. "-104": "硬币不足!",
  1270. "-105": "验证码错误!",
  1271. "-106": "账号非正式会员或在适应期!",
  1272. "-107": "应用不存在或者被封禁!",
  1273. "-108": "未绑定手机!",
  1274. "-109": "未知!",
  1275. "-110": "未绑定手机!",
  1276. "-111": "csrf 校验失败!",
  1277. "-112": "系统升级中!",
  1278. "-113": "账号尚未实名认证!",
  1279. "-114": "请先绑定手机!",
  1280. "-115": "请先完成实名认证!",
  1281. "-304": "木有改动!",
  1282. "-307": "撞车跳转!",
  1283. "-400": "请求错误!",
  1284. "-401": "未认证!",
  1285. "-403": "访问权限不足!",
  1286. "-404": "啥都木有!",
  1287. }
  1288.  
  1289. /*-----------------------------------------------CODE 统一检查--------------------------------------------------*/
  1290. /**
  1291. *
  1292. * @param code
  1293. * @constructor
  1294. */
  1295. let chackCode = function (code) {
  1296.  
  1297. let emsg = errorMsg[code + ""];
  1298. if (emsg != null) {
  1299. javaex.tip({content: emsg, type: "error"});
  1300.  
  1301. return false;
  1302. }
  1303.  
  1304. return true;
  1305. }
  1306.  
  1307.  
  1308. /*-----------------------------------------------localStorage封装 (本地存储信息)--------------------------------------------------*/
  1309. //javaex.setLocalStorage(key, value); var str = javaex.getLocalStorage(key); javaex.deleteLocalStorage(key);
  1310.  
  1311. /*window.onbeforeunload = function() {
  1312. if (!canLeavePage()) {
  1313. return ('确认离开当前页面吗?未保存的数据将会丢失!');
  1314. }
  1315. };*/
  1316.  
  1317.  
  1318. /**
  1319. * Ajax请求对象
  1320. */
  1321. const Ajax = (() => {
  1322. /**
  1323. * 检查options是否符合要求
  1324. * @param {object} options
  1325. * @returns {boolean}
  1326. */
  1327. function checkOptions(options) {
  1328. let result = false;
  1329. if (typeof options !== 'object') {
  1330. console.warn('类型错误: typeof Options !== Object');
  1331. return result;
  1332. } else {
  1333. if (typeof options.url !== 'string') {
  1334. console.warn('类型错误: typeof Link !== Strings');
  1335. return result;
  1336. } else {
  1337. const reg = /^https?:\/\/(?:\w+\.?)+(?:\/.*)*\/?$/i;
  1338. if (!reg.test(options.url)) {
  1339. console.warn('url字符串须为完整http链接');
  1340. return result;
  1341. }
  1342. result = true;
  1343. }
  1344. }
  1345. return result;
  1346. }
  1347.  
  1348. /**
  1349. * 对象转URL编码
  1350. * @param {object} data
  1351. */
  1352. function objToURLCode(data) {
  1353. var _result = [];
  1354. for (var key in data) {
  1355. var value = data[key];
  1356. if (value instanceof Array) {
  1357. value.forEach(function (_value) {
  1358. _result.push(key + "=" + _value);
  1359. });
  1360. } else {
  1361. _result.push(key + '=' + value);
  1362. }
  1363. }
  1364. return _result.join('&');
  1365. }
  1366.  
  1367. /**
  1368. * 请求
  1369. * @param {string} method
  1370. * @param {object} options
  1371. */
  1372. function request(method, options) {
  1373. if (checkOptions(options)) {
  1374. let xhr = new XMLHttpRequest();
  1375. const {url: _url, queryStringsObj, data, dataType, hasCookies} = options
  1376. , url = typeof queryStringsObj === 'object' ?
  1377. _url + '?' + objToURLCode(queryStringsObj) : _url;
  1378. switch (method) {
  1379. case 'GET':
  1380. xhr.open("GET", url);
  1381. break;
  1382. case 'POST':
  1383. xhr.open("POST", url);
  1384. xhr.setRequestHeader('Content-Type', dataType);
  1385. break;
  1386. default:
  1387. break;
  1388. }
  1389. if (hasCookies) xhr.withCredentials = true;
  1390. xhr.timeout = 3000;
  1391. xhr.addEventListener('load', () => {
  1392. if (xhr.status === 200) {
  1393. options.success(xhr.responseText)
  1394. } else {
  1395. console.error(`status:${xhr.status}`);
  1396. options.success(`{"code":${xhr.status},"msg":"频繁访问"}`);
  1397. }
  1398. })
  1399. xhr.addEventListener('error', () => {
  1400. console.error('ajax请求出错')
  1401. options.success('{"code":-1,"msg":"ajax请求出错"}');
  1402. })
  1403. xhr.addEventListener('timeout', () => {
  1404. console.error('请求超时')
  1405. options.success('{"code":-1,"msg":"请求超时"}');
  1406. })
  1407. switch (method) {
  1408. case 'GET':
  1409. xhr.send()
  1410. break;
  1411. case 'POST':
  1412. xhr.send((/urlencoded/.test(dataType)) ? objToURLCode(data) : data)
  1413. break;
  1414. default:
  1415. break;
  1416. }
  1417. }
  1418. }
  1419.  
  1420. return {
  1421. /**
  1422. * 发送Get请求
  1423. * @param {Object} options
  1424. */
  1425. get(options) {
  1426. request("GET", options);
  1427. },
  1428. /**
  1429. * 发送Post请求
  1430. * @param {object} options
  1431. */
  1432. post(options) {
  1433. request("POST", options);
  1434. }
  1435. }
  1436. })()