A Chrome extension that gives you finer control over MyAnimeList.net scores
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

390 Zeilen
14 KiB

  1. /* -------------------------------- Globals -------------------------------- */
  2. var MAX_BUCKETS = 256;
  3. var LOADING_IMG = '<img src="http://cdn.myanimelist.net/images/xmlhttp-loader.gif" align="center">';
  4. var should_sort = window.location.href.indexOf("order=4") != -1;
  5. /* ------------------------ Miscellaneous functions ------------------------ */
  6. function get_anime_id_from_href(href) {
  7. var anime_id;
  8. if (href.indexOf("/anime/") != -1)
  9. anime_id = href.substr(href.indexOf("/anime/") + "/anime/".length);
  10. else
  11. anime_id = href.substr(href.indexOf("id=") + "id=".length);
  12. if (anime_id.indexOf("/") != -1)
  13. anime_id = anime_id.substr(0, anime_id.indexOf("/"));
  14. if (anime_id.indexOf("&") != -1)
  15. anime_id = anime_id.substr(0, anime_id.indexOf("&"));
  16. return anime_id;
  17. }
  18. function get_edit_id_from_href(href) {
  19. var anime_id = href.substr(href.indexOf("id=") + "id=".length);
  20. if (anime_id.indexOf("&") != -1)
  21. anime_id = anime_id.substr(0, anime_id.indexOf("&"));
  22. return anime_id;
  23. }
  24. function get_scores_from_element(elem) {
  25. var score_100 = parseInt(elem.val());
  26. var score_10 = Math.round(score_100 / 10.);
  27. if (isNaN(score_100) || score_100 < 1 || score_100 > 100) {
  28. alert("Invalid score: must be an integer between 1 and 100.");
  29. return null;
  30. }
  31. return [score_100, score_10];
  32. }
  33. /* --------------------------- Storage functions --------------------------- */
  34. function save_score(anime_id, score) {
  35. var bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
  36. chrome.storage.sync.get(bucket_id, function(data) {
  37. var bucket = data[bucket_id];
  38. if (bucket === undefined)
  39. bucket = data[bucket_id] = {};
  40. bucket[anime_id] = score;
  41. chrome.storage.sync.set(data);
  42. });
  43. }
  44. function retrieve_scores(anime_id, callback) {
  45. var bucket_id = null;
  46. if (anime_id !== null)
  47. bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
  48. chrome.storage.sync.get(bucket_id, function(data) {
  49. if (anime_id !== null) {
  50. var bucket = data[bucket_id];
  51. if (bucket !== undefined && bucket[anime_id] !== undefined)
  52. callback(bucket[anime_id]);
  53. else
  54. callback(null);
  55. }
  56. else
  57. callback(data);
  58. });
  59. }
  60. function remove_score(anime_id) {
  61. var bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
  62. chrome.storage.sync.get(bucket_id, function(data) {
  63. var bucket = data[bucket_id];
  64. if (bucket === undefined || bucket[anime_id] === undefined)
  65. return;
  66. delete bucket[anime_id];
  67. if ($.isEmptyObject(bucket))
  68. chrome.storage.sync.remove(bucket_id);
  69. else
  70. chrome.storage.sync.set(data);
  71. });
  72. }
  73. /* ----------------------- Event patches/injections ------------------------ */
  74. function update_list_score(anime_id) {
  75. var new_scores = get_scores_from_element($("#scoretext" + anime_id));
  76. if (new_scores === null)
  77. return;
  78. var new_score_100 = new_scores[0], new_score_10 = new_scores[1];
  79. var payload = {id: anime_id, score: new_score_10};
  80. $("#scorebutton" + anime_id).prop("disabled", true);
  81. $.post("/includes/ajax.inc.php?t=63", payload, function(data) {
  82. $("#scoreval" + anime_id).text(new_score_100);
  83. $("#scoretext" + anime_id).val("");
  84. $("#scorediv" + anime_id).css("display", "none");
  85. $("#scorebutton" + anime_id).prop("disabled", false);
  86. if (should_sort)
  87. sort_list();
  88. });
  89. save_score(anime_id, new_score_100);
  90. }
  91. function update_anime_score(anime_id, is_new) {
  92. var new_scores = get_scores_from_element($("#myinfo_score"));
  93. if (new_scores === null)
  94. return;
  95. var new_score_100 = new_scores[0], new_score_10 = new_scores[1];
  96. var t_id, payload = {score: new_score_10};
  97. payload["status"] = $("#myinfo_status").val();
  98. payload["epsseen"] = $("#myinfo_watchedeps").val();
  99. if (is_new) {
  100. payload["aid"] = anime_id;
  101. t_id = "61";
  102. }
  103. else {
  104. payload["alistid"] = anime_id;
  105. payload["aid"] = $("#myinfo_anime_id").val();
  106. payload["astatus"] = $("#myinfo_curstatus").val();
  107. t_id = "62";
  108. }
  109. $("#myinfoDisplay").html(LOADING_IMG);
  110. $.post("/includes/ajax.inc.php?t=" + t_id, payload, function(data) {
  111. if (is_new) {
  112. document.getElementById("myinfoDisplay").innerHTML = "";
  113. document.getElementById("addtolist").innerHTML = data;
  114. }
  115. else
  116. document.getElementById("myinfoDisplay").innerHTML = data;
  117. });
  118. save_score(anime_id, new_score_100);
  119. }
  120. function submit_add_form(submit_button) {
  121. var anime_id = $("input[name='series_title']").val();
  122. if (!anime_id)
  123. return submit_button[0].click();
  124. var new_scores = get_scores_from_element($("#score_input"));
  125. if (new_scores === null)
  126. return;
  127. var new_score_100 = new_scores[0], new_score_10 = new_scores[1];
  128. $("select[name='score']").val(new_score_10);
  129. save_score(anime_id, new_score_100);
  130. submit_button[0].click();
  131. }
  132. function submit_edit_form(anime_id, submit_type, submit_button) {
  133. if (submit_type == 2) {
  134. var new_scores = get_scores_from_element($("#score_input"));
  135. if (new_scores === null)
  136. return;
  137. var new_score_100 = new_scores[0], new_score_10 = new_scores[1];
  138. $("select[name='score']").val(new_score_10);
  139. save_score(anime_id, new_score_100);
  140. }
  141. else if (submit_type == 3)
  142. remove_score(anime_id);
  143. submit_button[0].click();
  144. }
  145. /* -------------------------------- Sorting -------------------------------- */
  146. function compare(row1, row2) {
  147. var r1 = $(row1).find("span[id^='scoreval']").text(),
  148. r2 = $(row2).find("span[id^='scoreval']").text();
  149. if (r1 == r2) {
  150. r1 = $(row1).find("a.animetitle span").text();
  151. r2 = $(row2).find("a.animetitle span").text();
  152. return r1 > r2 ? 1 : -1;
  153. }
  154. if (r1 == "-")
  155. return 1;
  156. if (r2 == "-")
  157. return -1;
  158. return r2 - r1;
  159. }
  160. function setup_sortable_list() {
  161. var headers = [".header_cw", ".header_completed", ".header_onhold",
  162. ".header_dropped", ".header_ptw"];
  163. $.each(headers, function(i, header) {
  164. $(header).next()
  165. .nextUntil($(".category_totals").closest("table"))
  166. .wrapAll('<div class="list-chart-group"/>');
  167. });
  168. $(".list-chart-group table").each(function(i, row) {
  169. $(row).add($(row).next())
  170. .wrapAll('<div class="list-chart-row"/>');
  171. });
  172. }
  173. function sort_list() {
  174. $(".list-chart-group").each(function(i, group) {
  175. $(group).find(".list-chart-row").sort(compare).each(function(i, row) {
  176. $(group).append(row);
  177. });
  178. $(group).find(".list-chart-row").each(function(i, row) {
  179. $(row).find("tr").first().children().first().text(i + 1);
  180. $(row).find((i % 2) ? ".td1" : ".td2").toggleClass("td1 td2");
  181. });
  182. });
  183. }
  184. /* ---------------------------- Extension hooks ---------------------------- */
  185. function hook_list() {
  186. retrieve_scores(null, function(data) {
  187. $("span[id^='scoreval']").each(function(i, elem) {
  188. var anime_id = elem.id.split("scoreval")[1];
  189. var bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
  190. var bucket = data[bucket_id];
  191. if (bucket !== undefined && bucket[anime_id] !== undefined)
  192. $(elem).text(bucket[anime_id]);
  193. else {
  194. var current = parseInt($(elem).text());
  195. if (!isNaN(current))
  196. $(elem).text(current * 10);
  197. }
  198. $("#scorediv" + anime_id)
  199. .after($("<div>")
  200. .attr("id", "scorediv" + anime_id)
  201. .css("display", "none")
  202. .append($('<input>')
  203. .attr("type", "text")
  204. .attr("id", "scoretext" + anime_id)
  205. .attr("size", "2")
  206. .keydown(function(a_id) {
  207. return function(ev) {
  208. if ((window.event ? window.event.keyCode : ev.which) == 13)
  209. update_list_score(a_id);
  210. else
  211. return true;
  212. }
  213. }(anime_id)))
  214. .append($("<input>")
  215. .attr("type", "button")
  216. .attr("id", "scorebutton" + anime_id)
  217. .attr("value", "Go")
  218. .click(function(a_id) {
  219. return function() { return update_list_score(a_id); }
  220. }(anime_id))))
  221. .remove();
  222. });
  223. if (should_sort) {
  224. setup_sortable_list();
  225. sort_list();
  226. }
  227. });
  228. }
  229. function hook_anime(anime_id) {
  230. retrieve_scores(anime_id, function(score) {
  231. var old_input = $("#myinfo_score");
  232. var old_button = $("input[name='myinfo_submit']");
  233. var is_new = old_button.attr("value") == "Add";
  234. if (!is_new && score === null) {
  235. var old_score = parseInt(old_input.val());
  236. score = old_score == 0 ? "" : old_score * 10;
  237. }
  238. old_input.after($("<span> / 100</span>"))
  239. .after($("<input>")
  240. .attr("type", "text")
  241. .attr("id", "myinfo_score")
  242. .attr("name", "myinfo_score")
  243. .attr("class", "inputtext")
  244. .attr("value", (score === null) ? "" : score)
  245. .attr("size", "3"))
  246. .remove();
  247. old_button.after($("<input>")
  248. .attr("type", "button")
  249. .attr("name", "myinfo_submit")
  250. .attr("value", old_button.attr("value"))
  251. .attr("class", "inputButton")
  252. .click(function(a_id, is_new) {
  253. return function() { return update_anime_score(a_id, is_new); }
  254. }(anime_id, is_new)))
  255. .remove();
  256. });
  257. }
  258. function hook_add() {
  259. var old_input = $("select[name='score']");
  260. var old_submit = $("input[type='button'][onclick='checkValidSubmit(1)']");
  261. old_input.after($("<span> / 100</span>"))
  262. .after($("<input>")
  263. .attr("type", "text")
  264. .attr("id", "score_input")
  265. .attr("class", "inputtext")
  266. .attr("size", "3"))
  267. .hide();
  268. old_submit.after($("<input>")
  269. .attr("type", "button")
  270. .attr("class", "inputButton")
  271. .attr("style", old_submit.attr("style"))
  272. .attr("value", old_submit.attr("value"))
  273. .click(function(button) {
  274. return function() { return submit_add_form(button); }
  275. }(old_submit)))
  276. .hide();
  277. }
  278. function hook_edit(anime_id) {
  279. retrieve_scores(anime_id, function(score) {
  280. var old_input = $("select[name='score']");
  281. var old_edit = $("input[type='button'][onclick='checkValidSubmit(2)']");
  282. var old_delete = $("input[type='button'][onclick='checkValidSubmit(3)']");
  283. if (score === null) {
  284. var old_score = parseInt(old_input.val());
  285. score = old_score == 0 ? "" : old_score * 10;
  286. }
  287. old_input.after($("<span> / 100</span>"))
  288. .after($("<input>")
  289. .attr("type", "text")
  290. .attr("id", "score_input")
  291. .attr("class", "inputtext")
  292. .attr("value", score)
  293. .attr("size", "3"))
  294. .hide();
  295. old_edit.after($("<input>")
  296. .attr("type", "button")
  297. .attr("class", "inputButton")
  298. .attr("style", old_edit.attr("style"))
  299. .attr("value", old_edit.attr("value"))
  300. .click(function(a_id, button) {
  301. return function() { return submit_edit_form(a_id, 2, button); }
  302. }(anime_id, old_edit)))
  303. .hide();
  304. old_delete.after($("<input>")
  305. .attr("type", "button")
  306. .attr("class", "inputButton")
  307. .attr("value", old_delete.attr("value"))
  308. .click(function(a_id, button) {
  309. return function() { return submit_edit_form(a_id, 3, button); }
  310. }(anime_id, old_delete)))
  311. .hide();
  312. });
  313. }
  314. function hook_addtolist() {
  315. /* TODO: this entry point is unimplemented - it's rarely used and difficult
  316. to inject into, so I'm avoiding it for now. */
  317. $("<p><b>Note:</b> For the time being, anime added through this " +
  318. "interface cannot be given scores on the 100-point scale (the old " +
  319. "10-point system is used).</p><p>To give a more specific number, " +
  320. "simply add the anime here, then go to its own page or to your list " +
  321. "page, and update the score.</p>").insertAfter($("#stype").parent());
  322. }
  323. /* ------------------------------- Main hook ------------------------------- */
  324. $(document).ready(function() {
  325. var href = window.location.href;
  326. if (href.indexOf("/animelist/") != -1)
  327. hook_list();
  328. else if (href.indexOf("/anime/") != -1 || href.indexOf("/anime.php") != -1)
  329. hook_anime(get_anime_id_from_href(href));
  330. else if (href.indexOf("/panel.php") != -1 && href.indexOf("go=add") != -1)
  331. hook_add();
  332. else if (href.indexOf("/editlist.php") != -1 && href.indexOf("type=anime") != -1)
  333. hook_edit(get_edit_id_from_href(href));
  334. else if (href.indexOf("/addtolist.php") != -1)
  335. hook_addtolist();
  336. });