A Chrome extension that gives you finer control over MyAnimeList.net scores
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

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