Greasy Fork is available in English.

巴哈姆特之刪除動畫瘋中獎信件

一鍵刪除惱人的中獎信件,本腳本雖經過測試,但無法保證不會誤刪,請理解風險後再使用

  1. // ==UserScript==
  2. // @name 巴哈姆特之刪除動畫瘋中獎信件
  3. // @description 一鍵刪除惱人的中獎信件,本腳本雖經過測試,但無法保證不會誤刪,請理解風險後再使用
  4. // @namespace nathan60107
  5. // @version 1.2
  6. // @author nathan60107(貝果)
  7. // @homepage https://home.gamer.com.tw/homeindex.php?owner=nathan60107
  8. // @match https://mailbox.gamer.com.tw/?l=1
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. var myMailBox = Array(), myDate = Array();//暫存信件編號與日期的變數
  13.  
  14. function isAniBabi(mail) {//檢查是否是巴幣中獎信
  15. return null != mail.match(/恭喜您於[\d\/]+答對了動畫瘋的問答遊戲!\n本次獎勵:[\d]+ 巴幣\n目前已完成巴幣的發送!\n\n--\n本信件為系統通知信件,請勿回覆!/);
  16. }
  17.  
  18. function strip(html) {//去除html成份 只留下純文字
  19. var tmp = document.createElement("DIV");
  20. tmp.innerHTML = html;
  21. return tmp.textContent || tmp.innerText;
  22. }
  23.  
  24. function myReload() {
  25. location.reload();
  26. }
  27.  
  28. function changeButtonText(text, isActive, func) {//改變按鈕文字與屬性
  29. jQuery(".delAniBabi").html("\n\t<a class=\"not-active\">" + text + "</a>\n\t");
  30. jQuery(".delAniBabi")[0].childNodes[1].attributes[0].nodeValue = isActive ? "is-active" : "not-active";
  31. if (isActive) {
  32. jQuery(".delAniBabi").click(func)
  33. }
  34. }
  35.  
  36. function delMail(mailSn, date, mailContent, mailNumber) {//刪除指定信件並通知
  37. var t = jQuery, i = window.mailbox, e = mailSn;
  38.  
  39. t.post("/ajax/" + i.boxType + "Del.php", "csrfToken=" + t("input[name=csrfToken]").val() + "&del[]=" + e, function (a) {
  40. if (a.code != 0) {
  41. toastr.warning("刪除第" + mailNumber + "封信ERROR:" + a.message);
  42. console.log(a);
  43. } else {
  44. console.log(mailSn, "已刪除" + date + "的中獎通知:" + mailContent.replaceAll("\n", ""))
  45. toastr.success("已刪除" + date + "的中獎通知");
  46. }
  47. }, "json");
  48. }
  49.  
  50. function getMail(mailNumber) {//取得信件並決定是否刪除
  51. var t = jQuery, i = window.mailbox, e = String(myMailBox[mailNumber].substr(9, 19)), date = myDate[mailNumber];
  52. changeButtonText("正在檢查第" + String(mailNumber) + "封信...", false)
  53.  
  54. t.post({
  55. url: "/ajax/" + window.mailbox.boxType + "Read.php",
  56. data: { sn: e },//用jsonText會出現信件編號錯誤 jsonText=JSON.stringify(json)
  57. dataType: "json",
  58. success: function (a) {
  59. if (a.code != 0) {
  60. toastr.warning("檢查第" + e + "封信ERROR:" + a.message);
  61. console.log(a);
  62. } else {
  63. var mailContent = strip(window.mailbox.changetxt(a.data));
  64.  
  65. if (isAniBabi(mailContent)) {
  66. delMail(e, date, mailContent, mailNumber);
  67. }
  68. }
  69.  
  70. if (mailNumber + 1 < myMailBox.length) {//遞迴讀取下一封信,不一封封依序讀取的話會有SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"。
  71. setTimeout(function () { getMail(mailNumber + 1); }, 150);//兩封信之間的間隔太短會有JSON error
  72. } else {//所有信件檢查完畢,要求使用者重整。
  73. changeButtonText("刪除完畢 請點此重整頁面", true, myReload);
  74. }
  75. },
  76. error: function (XMLHttpRequest, textStatus, errorThrown) {
  77. changeButtonText("未知的錯誤 程序已停止", true, myReload);
  78. console.log("fail")
  79. console.log(e)
  80. console.log(mailNumber, e, date, XMLHttpRequest.statusText, textStatus, errorThrown);
  81. console.log({ sn: e }, JSON.stringify({ sn: e }))
  82. }
  83. });
  84. }
  85.  
  86. function getMailList(page) {//讀取第page頁的信件列表
  87. var e = page;
  88. changeButtonText("正在讀第" + String(page) + "頁列表...", false)
  89.  
  90. jQuery.post("/ajax/inboxList.php", {
  91. p: e,
  92. s: window.mailbox.searchUser
  93. }, function (t) {
  94. if (t.code != 0) {
  95. toastr.warning("檢查第" + e + "封信ERROR:" + t.message);
  96. console.log(t);
  97. } else {
  98. var result = t.data.match(/data-sn=\"[0-9]{10}\_[0-9]{8}/g)
  99. var date = t.data.match(/[\d]{4}-[\d]{2}-[\d]{2}/g)
  100. console.log(t, jQuery(t.data).find('#BH-master .sticky-nav .row:nth-child(2)'))
  101. var [, curMailNumber, maxMailNumber] =
  102. jQuery(t.data).find('.sticky-nav .row:nth-child(2)').text()
  103. .match(/- (\d+) 封(共 (\d+) 封)/)
  104.  
  105. myMailBox = myMailBox.concat(result)
  106. myDate = myDate.concat(date)
  107.  
  108. if (curMailNumber >= maxMailNumber) {//列表全數讀取完畢,開始讀取信件內容。
  109. changeButtonText("正在刪除信件...", false)
  110. getMail(0);
  111. } else {//遞迴讀取下一頁的列表,不一頁頁依序讀取的話會有錯誤。
  112. setTimeout(function () { getMailList(page + 1); }, 100);
  113. }
  114. }
  115. })
  116. }
  117.  
  118. function startDelAniBabi() {//查看所有信件
  119. toastr.options = {
  120. positionClass: 'toast-top-center',
  121. showDuration: 60,
  122. hideDuration: 120,
  123. tapToDismiss: false
  124. };
  125.  
  126. var text = jQuery("#list_div").text();
  127.  
  128. jQuery(".delAniBabi").off("click")
  129. getMailList(1);
  130. };
  131.  
  132. //初始化toastr通知
  133. jQuery('head').append('<script src="https://i2.bahamut.com.tw/js/plugins/toastr-2.1.3.min.js?v=1498617831"></script>');
  134. jQuery('head').append('<link href="https://i2.bahamut.com.tw/css/plugins/toastr.min.css?v=1498617831" rel="stylesheet" type="text/css">');
  135. jQuery('head').append('<style> .toast-top-center { top: 80px; right: 0; width: 100%; } </style>');
  136.  
  137. (function () {
  138. //設置刪除中獎信按鈕
  139. var writeMail = jQuery(".BH-menu-forumA-back");
  140. writeMail.after(`<li class="BH-menu-forumA-back delAniBabi">
  141. <a class="is-active">刪除巴幣中獎信</a>
  142. </li>`);
  143. jQuery(".delAniBabi")[0].style.marginRight = "5px";
  144. jQuery(".delAniBabi").click(startDelAniBabi)
  145.  
  146. })();
  147.  
  148. /*reference
  149. 等元素載入完畢: https://www.itread01.com/content/1545263124.html
  150. jQuery: https://ithelp.ithome.com.tw/articles/10095237
  151. getType: http://iambigd.blogspot.com/2012/10/javascript.html
  152. html2text: https://stackoverflow.com/questions/822452/strip-html-from-text-javascript
  153. promise: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Promise
  154. https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Guide/Using_promises
  155. 正則: http://ccckmit.wikidot.com/regularexpression
  156. run-at: https://codertw.com/程式語言/709052/#outline__1_9
  157. 插入在某元素之後: https://www.w3school.com.cn/jquery/jquery_dom_add.asp
  158. 在按鈕之間插入空格: https://stackoverflow.com/questions/19769033/jquery-beginner-how-to-insert-spaces-between-append
  159. 幫元素添加css: http://www.aaronlife.com/v1/notes/html_set_css_styles_using_javascript.html
  160. 正則的global用法與match: https://blog.csdn.net/vajoy/article/details/17020423
  161. jQuery post:https://api.jquery.com/jquery.post/
  162. https://stackoverflow.com/questions/2833951/how-do-i-catch-an-ajax-query-post-error
  163. 串接array: https://www.w3schools.com/jsref/jsref_concat_array.asp
  164. 取消按鈕效果: https://api.jquery.com/click/
  165. 頁面重新整理: https://ithelp.ithome.com.tw/articles/10190061
  166. */