ehAutoStar

EH画廊自动/批量评分脚本

2023-01-02 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name ehAutoStar
  3. // @namespace moe.cangku.mengzonefire
  4. // @version 1.1.2
  5. // @description EH画廊自动/批量评分脚本
  6. // @author mengzonefire
  7. // @require https://cdn.jsdelivr.net/npm/sweetalert2@9
  8. // @require https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js
  9. // @require https://cdn.jsdelivr.net/npm/jquery-migrate@3.4.0/dist/jquery-migrate.min.js
  10. // @match *://exhentai.org/g*
  11. // @match *://e-hentai.org/g*
  12. // @match *://exhentai.org/uploader/*
  13. // @match *://e-hentai.org/uploader/*
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @run-at document-end
  17. // ==/UserScript==
  18. const href = window.location.href;
  19. const defRateMap = "BlossomPlus 5";
  20. !(function () {
  21. "use strict";
  22. var rating_img;
  23. var uploader;
  24. var my_apikey;
  25. var my_apiuid;
  26. var g_flag;
  27. var link_ele;
  28. var star_ele;
  29. var GaInfo;
  30. var count = 0;
  31. var fail_count = 0;
  32. var domain = href.indexOf("exhentai") != -1 ? "exhentai" : "e-hentai";
  33. var rateMapText = GM_getValue("rateMap");
  34. var rateMap = [];
  35. var icon_url =
  36. domain == "exhentai"
  37. ? "https://exhentai.org/img/mr.gif"
  38. : "https://ehgt.org/g/mr.gif";
  39. var html_btn = '<div style="float:left">&nbsp; <a id="rateSetting" href="#">';
  40. html_btn += `<img src="${icon_url}"> 自动评分设置`;
  41. html_btn += "</a></div>";
  42. var html_input =
  43. '<div style="position: absolute;margin-left: 5px;"><input type="button" value="一键评分" id="autoRate"><input type="text" id="inputRate" value="5" size="4" maxlength="3" style="width:22px"></div>';
  44. if (!rateMapText) {
  45. rateMapText = defRateMap;
  46. GM_setValue("rateMap", rateMapText);
  47. }
  48. readRateMap();
  49.  
  50. if (href.indexOf("/uploader/") != -1) {
  51. g_flag = false;
  52. let loop = setInterval(() => {
  53. var html_tag2 = $("div.searchnav:first");
  54. if (!html_tag2.length) return false;
  55. html_tag2.before(html_input);
  56. $("#autoRate").click(function () {
  57. batchRating();
  58. });
  59. clearInterval(loop);
  60. }, 500);
  61. } else if (href.indexOf("/g/") != -1) {
  62. var style;
  63. g_flag = true;
  64. my_apikey = apikey;
  65. my_apiuid = apiuid;
  66. if (domain == "exhentai") {
  67. style =
  68. "<style>.swal2-textarea{color:#ffffff;background-color:#43464e}</style>";
  69. } else {
  70. style =
  71. "<style>.swal2-textarea{color:#000000;background-color:#f3f0e0}</style>";
  72. }
  73. var ele = document.createElement("div");
  74. ele.innerHTML = style;
  75. document.getElementsByTagName("head")[0].appendChild(ele.firstElementChild);
  76.  
  77. let loop1 = setInterval(() => {
  78. var html_tag = $("#gdr");
  79. if (!html_tag.length) return false;
  80. html_tag.append(html_btn);
  81. $("#rateSetting").click(function () {
  82. rateSetting();
  83. });
  84. clearInterval(loop1);
  85. }, 500);
  86.  
  87. let loop2 = setInterval(() => {
  88. uploader = $("#gdn>a")[0].innerText;
  89. rating_img = $("#rating_image")[0];
  90. if (!uploader || !rating_img) return false;
  91. else if (
  92. rating_img.className == "ir" &&
  93. rateMap.hasOwnProperty(uploader)
  94. ) {
  95. rating(rateMap[uploader], -1);
  96. }
  97. clearInterval(loop2);
  98. }, 500);
  99.  
  100. if (apikey != GM_getValue("apikey")) {
  101. GM_setValue("apikey", apikey);
  102. }
  103. if (apiuid != GM_getValue("apiuid")) {
  104. GM_setValue("apiuid", apiuid);
  105. }
  106. }
  107.  
  108. function rating(rate, recu) {
  109. if (recu == -1) GaInfo = href.match(/\/g\/([\d]+)\/([\da-f]+)\//);
  110. else {
  111. if (recu == star_ele.length) {
  112. Swal.fire(`一键评分完成,共评分${count}个画廊,失败${fail_count}个`);
  113. return true;
  114. }
  115. if (star_ele[recu].className == "ir") {
  116. rating_img = star_ele[recu];
  117. GaInfo = link_ele[recu].href.match(/\/g\/([\d]+)\/([\da-f]+)\//);
  118. } else {
  119. rating(rate, recu + 1);
  120. return false;
  121. }
  122. }
  123. $.ajax({
  124. url: "/api.php",
  125. type: "POST",
  126. contentType: "application/json",
  127. dataType: "json",
  128. data: JSON.stringify({
  129. apikey: my_apikey,
  130. apiuid: my_apiuid,
  131. gid: Number(GaInfo[1]),
  132. method: "rategallery",
  133. rating: rate * 2,
  134. token: GaInfo[2],
  135. }),
  136. })
  137. .success(function (r) {
  138. console.log(r);
  139. if (!r["error"]) {
  140. var n1 = Math.ceil(rate - 5) * 16;
  141. var n2 = rate % 1 ? -21 : -1;
  142. rating_img.setAttribute(
  143. "style",
  144. `background-position: ${n1}px ${n2}px;`
  145. );
  146. rating_img.className = r["rating_cls"];
  147. if (recu == -1) {
  148. $("#rating_label")[0].innerText = `平均值:${r["rating_avg"]}`;
  149. $("#rating_count")[0].innerText = r["rating_cnt"];
  150. }
  151. }
  152. })
  153. .fail(function (r) {
  154. console.log(r);
  155. fail_count += 1;
  156. })
  157. .always(function () {
  158. count += 1;
  159. if (recu != -1) rating(rate, recu + 1);
  160. });
  161. }
  162.  
  163. function rateSetting() {
  164. Swal.fire({
  165. title: "输入自动评分配置\n格式:上传者 分数(0.5-5)",
  166. input: "textarea",
  167. inputValue: GM_getValue("rateMap"),
  168. showCancelButton: true,
  169. confirmButtonText: "确定",
  170. cancelButtonText: "取消",
  171. inputValidator: (value) => {
  172. rateMapText = value;
  173. var errNum = readRateMap();
  174. if (errNum != -1) {
  175. return `第{${errNum + 1}行格式错误,分数必须为0.5的整数倍`;
  176. }
  177. },
  178. }).then((result) => {
  179. if (result.dismiss) return;
  180. GM_setValue("rateMap", rateMapText);
  181. });
  182. }
  183.  
  184. function readRateMap() {
  185. var mapLine = rateMapText.split("\n");
  186. for (var line of mapLine) {
  187. var result = line.match(/([\s\S]+) ((?:[0-4].5)?[1-5]?)$/);
  188. if (result) rateMap[result[1]] = Number(result[2]);
  189. else return mapLine.indexOf(line);
  190. }
  191. return -1;
  192. }
  193.  
  194. function batchRating() {
  195. if (GM_getValue("apiuid")) {
  196. my_apikey = GM_getValue("apikey");
  197. my_apiuid = GM_getValue("apiuid");
  198. } else {
  199. Swal.fire(
  200. "api参数未记录, 请先登录并进入任意画廊, 脚本会自动记录参数, 然后再使用此功能"
  201. );
  202. return false;
  203. }
  204. if (my_apiuid != Number(getCookie("ipb_member_id"))) {
  205. Swal.fire(
  206. "api参数不匹配, 请先进入任意画廊, 脚本会自动刷新参数, 然后再使用此功能"
  207. );
  208. return false;
  209. }
  210. var select_ele = $("div.searchnav:first > div > select > option"); // 列表右上的模式选择框 (最小化...缩略图...扩展)
  211. var select_flag = getSelect(select_ele);
  212.  
  213. switch (select_flag) {
  214. case "m":
  215. case "p":
  216. star_ele = $("td.gl4m > div");
  217. link_ele = $("td.gl3m.glname > a");
  218. break;
  219. case "l":
  220. star_ele = $("td.gl2c > div:nth-child(3) > div.ir");
  221. link_ele = $("td.gl3c.glname > a");
  222. break;
  223. case "e":
  224. star_ele = $("td.gl2e > div > div > div.ir");
  225. link_ele = $("td.gl2e > div > a");
  226. break;
  227. case "t":
  228. star_ele = $("div.gl5t > div:nth-child(2) > div.ir");
  229. link_ele = $("div.itg.gld > div > a");
  230. break;
  231. default:
  232. Swal.fire("一键评分失败: select元素错误");
  233. }
  234.  
  235. if (link_ele.length != star_ele.length) {
  236. Swal.fire("一键评分失败:html元素错误");
  237. } else {
  238. var my_rate = Number($("#inputRate")[0].value);
  239. if (my_rate > 0 && my_rate <= 5 && my_rate % 0.5 == 0) {
  240. rating(my_rate, 0);
  241. } else {
  242. Swal.fire("分数错误:数值必须在0.5-5之间且为0.5的整数倍");
  243. }
  244. }
  245. }
  246.  
  247. function getCookie(cookie_name) {
  248. var allcookies = document.cookie;
  249. var cookie_pos = allcookies.indexOf(cookie_name);
  250. if (cookie_pos != -1) {
  251. cookie_pos = cookie_pos + cookie_name.length + 1;
  252. var cookie_end = allcookies.indexOf(";", cookie_pos);
  253. if (cookie_end == -1) cookie_end = allcookies.length;
  254. var value = unescape(allcookies.substring(cookie_pos, cookie_end));
  255. }
  256. return value;
  257. }
  258.  
  259. function getSelect(select_ele) {
  260. for (var i = 0; i < select_ele.length; i++) {
  261. if (select_ele[i].getAttribute("selected")) {
  262. return select_ele[i].getAttribute("value");
  263. }
  264. }
  265. return null;
  266. }
  267. })();