A Chrome extension that gives you finer control over MyAnimeList.net scores
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

529 line
18 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. /* Note: complaints about modifying objects we don't own are ignored since
  7. these changes are only executed within the context of our own extension. */
  8. String.prototype.contains = function(substr) {
  9. return this.indexOf(substr) != -1;
  10. };
  11. String.prototype.cut_after = function(substr) {
  12. return this.substr(this.indexOf(substr) + substr.length)
  13. };
  14. String.prototype.cut_before = function(substr) {
  15. return this.substr(0, this.indexOf(substr));
  16. };
  17. String.prototype.cut = function(after, before) {
  18. return this.cut_after(after).cut_before(before);
  19. }
  20. function round_score(num) {
  21. num = Math.round(num * 10) / 10;
  22. if (isNaN(num))
  23. return num;
  24. if (num == Math.round(num))
  25. num += ".0";
  26. return num;
  27. }
  28. function get_anime_id_from_href(href) {
  29. var anime_id;
  30. if (href.contains("/anime/"))
  31. anime_id = href.cut_after("/anime/");
  32. else
  33. anime_id = href.cut_after("id=");
  34. if (anime_id.contains("/"))
  35. anime_id = anime_id.cut_before("/");
  36. if (anime_id.contains("&"))
  37. anime_id = anime_id.cut_before("&");
  38. return anime_id;
  39. }
  40. function get_edit_id_from_href(href) {
  41. var anime_id = href.cut_after("id=");
  42. if (anime_id.contains("&"))
  43. anime_id = anime_id.cut_before("&");
  44. return anime_id;
  45. }
  46. function get_score_from_element(elem) {
  47. var score = round_score(elem.val());
  48. if (isNaN(score) || ((score < 1 || score > 10) && score != 0)) {
  49. alert("Invalid score: must be a number between 1.0 and 10.0, or 0.");
  50. return null;
  51. }
  52. return score;
  53. }
  54. function load_score_into_element(data, anime_id, elem) {
  55. var bucket = data[(parseInt(anime_id) % MAX_BUCKETS).toString()];
  56. if (bucket !== undefined && bucket[anime_id] !== undefined)
  57. elem.text(bucket[anime_id] == 0 ? "-" : bucket[anime_id]);
  58. else {
  59. var current = parseInt(elem.text());
  60. if (!isNaN(current))
  61. elem.text(current + ".0");
  62. }
  63. }
  64. function update_shared_row_colors(row, our_pos) {
  65. var our_cell = $(row.find("td")[our_pos]);
  66. var their_cell = $(row.find("td")[our_pos == 1 ? 2 : 1]);
  67. var diff = our_cell.text() - their_cell.text();
  68. if (!diff) {
  69. row.find("td").css("background-color", "#f6f6f6");
  70. our_cell.add(their_cell).find("span").css("color", "");
  71. }
  72. else {
  73. row.find("td").css("background-color", "");
  74. our_cell.css("color", diff > 0 ? "#FF0000" : "#0000FF");
  75. their_cell.css("color", diff > 0 ? "#0000FF" : "#FF0000");
  76. }
  77. }
  78. /* --------------------------- Storage functions --------------------------- */
  79. function save_score(anime_id, score) {
  80. var bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
  81. chrome.storage.sync.get(bucket_id, function(data) {
  82. var bucket = data[bucket_id];
  83. if (bucket === undefined)
  84. bucket = data[bucket_id] = {};
  85. bucket[anime_id] = score;
  86. chrome.storage.sync.set(data);
  87. });
  88. }
  89. function retrieve_scores(anime_id, callback) {
  90. var bucket_id = null;
  91. if (anime_id !== null)
  92. bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
  93. chrome.storage.sync.get(bucket_id, function(data) {
  94. if (anime_id !== null) {
  95. var bucket = data[bucket_id];
  96. if (bucket !== undefined && bucket[anime_id] !== undefined)
  97. callback(bucket[anime_id]);
  98. else
  99. callback(null);
  100. }
  101. else
  102. callback(data);
  103. });
  104. }
  105. function remove_score(anime_id) {
  106. var bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
  107. chrome.storage.sync.get(bucket_id, function(data) {
  108. var bucket = data[bucket_id];
  109. if (bucket === undefined || bucket[anime_id] === undefined)
  110. return;
  111. delete bucket[anime_id];
  112. if ($.isEmptyObject(bucket))
  113. chrome.storage.sync.remove(bucket_id);
  114. else
  115. chrome.storage.sync.set(data);
  116. });
  117. }
  118. /* ----------------------- Event patches/injections ------------------------ */
  119. function update_list_score(anime_id) {
  120. var new_score = get_score_from_element($("#scoretext" + anime_id));
  121. if (new_score === null)
  122. return;
  123. var payload = {id: anime_id, score: Math.round(new_score)};
  124. $("#scorebutton" + anime_id).prop("disabled", true);
  125. $.post("/includes/ajax.inc.php?t=63", payload, function(data) {
  126. $("#scoreval" + anime_id).text(new_score == 0 ? "-" : new_score);
  127. $("#scoretext" + anime_id).val("");
  128. $("#scorediv" + anime_id).css("display", "none");
  129. $("#scorebutton" + anime_id).prop("disabled", false);
  130. if (should_sort)
  131. sort_list();
  132. update_list_stats();
  133. });
  134. save_score(anime_id, new_score);
  135. }
  136. function update_anime_score(anime_id, is_new) {
  137. var new_score = get_score_from_element($("#myinfo_score"));
  138. if (new_score === null)
  139. return;
  140. var t_id, payload = {score: Math.round(new_score)};
  141. payload["status"] = $("#myinfo_status").val();
  142. payload["epsseen"] = $("#myinfo_watchedeps").val();
  143. if (is_new) {
  144. payload["aid"] = anime_id;
  145. t_id = "61";
  146. }
  147. else {
  148. payload["alistid"] = anime_id;
  149. payload["aid"] = $("#myinfo_anime_id").val();
  150. payload["astatus"] = $("#myinfo_curstatus").val();
  151. t_id = "62";
  152. }
  153. $("#myinfoDisplay").html(LOADING_IMG);
  154. $.post("/includes/ajax.inc.php?t=" + t_id, payload, function(data) {
  155. if (is_new) {
  156. $("#myinfoDisplay").html("");
  157. $("#addtolist").html(data);
  158. }
  159. else
  160. $("#myinfoDisplay").html(data);
  161. });
  162. save_score(anime_id, new_score);
  163. }
  164. function submit_add_form(submit_button) {
  165. var anime_id = $("input[name='series_title']").val();
  166. if (!anime_id)
  167. return submit_button[0].click();
  168. var new_score = get_score_from_element($("#score_input"));
  169. if (new_score === null)
  170. return;
  171. $("select[name='score']").val(Math.round(new_score));
  172. save_score(anime_id, new_score);
  173. submit_button[0].click();
  174. }
  175. function submit_edit_form(anime_id, submit_type, submit_button) {
  176. if (submit_type == 2) {
  177. var new_score = get_score_from_element($("#score_input"));
  178. if (new_score === null)
  179. return;
  180. $("select[name='score']").val(Math.round(new_score));
  181. save_score(anime_id, new_score);
  182. }
  183. else if (submit_type == 3)
  184. remove_score(anime_id);
  185. submit_button[0].click();
  186. }
  187. /* ------------------------ List stats and sorting ------------------------- */
  188. function compare(row1, row2) {
  189. var r1 = $(row1).find("span[id^='scoreval']").text(),
  190. r2 = $(row2).find("span[id^='scoreval']").text();
  191. if (r1 == r2) {
  192. r1 = $(row1).find("a.animetitle span").text();
  193. r2 = $(row2).find("a.animetitle span").text();
  194. return r1 > r2 ? 1 : -1;
  195. }
  196. if (r1 == "-")
  197. return 1;
  198. if (r2 == "-")
  199. return -1;
  200. return r2 - r1;
  201. }
  202. function prepare_list() {
  203. var headers = [".header_cw", ".header_completed", ".header_onhold",
  204. ".header_dropped", ".header_ptw"];
  205. $.each(headers, function(i, header) {
  206. $(header).next()
  207. .nextUntil($(".category_totals").closest("table"))
  208. .wrapAll('<div class="list-chart-group"/>');
  209. });
  210. $(".list-chart-group table").each(function(i, row) {
  211. $(row).add($(row).next())
  212. .wrapAll('<div class="list-chart-row"/>');
  213. });
  214. }
  215. function sort_list() {
  216. $(".list-chart-group").each(function(i, group) {
  217. $(group).find(".list-chart-row").sort(compare).each(function(i, row) {
  218. $(group).append(row);
  219. });
  220. $(group).find(".list-chart-row").each(function(i, row) {
  221. $(row).find("tr").first().children().first().text(i + 1);
  222. $(row).find((i % 2) ? ".td1" : ".td2").toggleClass("td1 td2");
  223. });
  224. });
  225. }
  226. function apply_stats(elem, old_sum, new_sum, nums) {
  227. var old_score = elem.text().cut("Mean Score: ", ",");
  228. var old_dev = elem.text().cut("Score Dev.: ", "\n");
  229. var mean = round_score(new_sum / nums) || "0.0";
  230. var deviation = (new_sum - old_sum) / nums + parseFloat(old_dev) || 0;
  231. deviation = Math.round(deviation * 100) / 100;
  232. elem.text(elem.text()
  233. .replace("Mean Score: " + old_score, "Mean Score: " + mean)
  234. .replace("Score Dev.: " + old_dev, "Score Dev.: " + deviation));
  235. }
  236. function update_list_stats() {
  237. var old_sum_all = 0, new_sum_all = 0, nums_all = 0;
  238. $(".category_totals").each(function(i, totals) {
  239. var group = $(totals).closest("table").prev();
  240. var old_sum = 0, new_sum = 0, nums = 0;
  241. group.find("span[id^='scoreval']").each(function(j, elem) {
  242. if ($(elem).text() != "-") {
  243. old_sum += parseFloat($(elem).next().text());
  244. new_sum += parseFloat($(elem).text());
  245. nums++;
  246. }
  247. });
  248. apply_stats($(totals), old_sum, new_sum, nums);
  249. old_sum_all += old_sum;
  250. new_sum_all += new_sum;
  251. nums_all += nums;
  252. });
  253. if ($("#grand_totals").length > 0)
  254. apply_stats($("#grand_totals"), old_sum_all, new_sum_all, nums_all);
  255. }
  256. /* ---------------------------- Extension hooks ---------------------------- */
  257. function hook_list() {
  258. retrieve_scores(null, function(data) {
  259. $("span[id^='scoreval']").each(function(i, elem) {
  260. var anime_id = elem.id.split("scoreval")[1];
  261. $(elem).after($("<span>")
  262. .css("display", "none").text($(elem).text()));
  263. load_score_into_element(data, anime_id, $(elem));
  264. $("#scorediv" + anime_id)
  265. .after($("<div>")
  266. .attr("id", "scorediv" + anime_id)
  267. .css("display", "none")
  268. .append($('<input>')
  269. .attr("type", "text")
  270. .attr("id", "scoretext" + anime_id)
  271. .attr("size", "2")
  272. .keydown(function(a_id) {
  273. return function(ev) {
  274. if ((window.event ? window.event.keyCode : ev.which) == 13)
  275. update_list_score(a_id);
  276. else
  277. return true;
  278. }
  279. }(anime_id)))
  280. .append($("<input>")
  281. .attr("type", "button")
  282. .attr("id", "scorebutton" + anime_id)
  283. .attr("value", "Go")
  284. .click(function(a_id) {
  285. return function() { return update_list_score(a_id); }
  286. }(anime_id))))
  287. .remove();
  288. });
  289. prepare_list();
  290. if (should_sort)
  291. sort_list();
  292. update_list_stats();
  293. });
  294. }
  295. function hook_anime(anime_id) {
  296. retrieve_scores(anime_id, function(score) {
  297. var old_input = $("#myinfo_score");
  298. var old_button = $("input[name='myinfo_submit']");
  299. var is_new = old_button.attr("value") == "Add";
  300. if (!is_new && score === null)
  301. score = parseInt(old_input.val()) + ".0";
  302. old_input.after($("<span> / 10.0</span>"))
  303. .after($("<input>")
  304. .attr("type", "text")
  305. .attr("id", "myinfo_score")
  306. .attr("name", "myinfo_score")
  307. .attr("class", "inputtext")
  308. .attr("value", (score === null || score == 0) ? "" : score)
  309. .attr("size", "3"))
  310. .remove();
  311. old_button.after($("<input>")
  312. .attr("type", "button")
  313. .attr("name", "myinfo_submit")
  314. .attr("value", old_button.attr("value"))
  315. .attr("class", "inputButton")
  316. .click(function(a_id, is_new) {
  317. return function() { return update_anime_score(a_id, is_new); }
  318. }(anime_id, is_new)))
  319. .remove();
  320. });
  321. }
  322. function hook_add() {
  323. var old_input = $("select[name='score']");
  324. var old_submit = $("input[type='button'][onclick='checkValidSubmit(1)']");
  325. old_input.after($("<span> / 10.0</span>"))
  326. .after($("<input>")
  327. .attr("type", "text")
  328. .attr("id", "score_input")
  329. .attr("class", "inputtext")
  330. .attr("size", "3"))
  331. .hide();
  332. old_submit.after($("<input>")
  333. .attr("type", "button")
  334. .attr("class", "inputButton")
  335. .attr("style", old_submit.attr("style"))
  336. .attr("value", old_submit.attr("value"))
  337. .click(function(button) {
  338. return function() { return submit_add_form(button); }
  339. }(old_submit)))
  340. .hide();
  341. }
  342. function hook_edit(anime_id) {
  343. retrieve_scores(anime_id, function(score) {
  344. var old_input = $("select[name='score']");
  345. var old_edit = $("input[type='button'][onclick='checkValidSubmit(2)']");
  346. var old_delete = $("input[type='button'][onclick='checkValidSubmit(3)']");
  347. if (score === null)
  348. score = parseInt(old_input.val()) + ".0";
  349. old_input.after($("<span> / 10.0</span>"))
  350. .after($("<input>")
  351. .attr("type", "text")
  352. .attr("id", "score_input")
  353. .attr("class", "inputtext")
  354. .attr("value", score == 0 ? "" : score)
  355. .attr("size", "3"))
  356. .hide();
  357. old_edit.after($("<input>")
  358. .attr("type", "button")
  359. .attr("class", "inputButton")
  360. .attr("style", old_edit.attr("style"))
  361. .attr("value", old_edit.attr("value"))
  362. .click(function(a_id, button) {
  363. return function() { return submit_edit_form(a_id, 2, button); }
  364. }(anime_id, old_edit)))
  365. .hide();
  366. old_delete.after($("<input>")
  367. .attr("type", "button")
  368. .attr("class", "inputButton")
  369. .attr("value", old_delete.attr("value"))
  370. .click(function(a_id, button) {
  371. return function() { return submit_edit_form(a_id, 3, button); }
  372. }(anime_id, old_delete)))
  373. .hide();
  374. });
  375. }
  376. function hook_shared() {
  377. var our_profile = $("#nav a:first").attr("href"), our_pos;
  378. var profile_links = $("#content h2:first").find("a").slice(1);
  379. var shared_table = $("#content h2:first").next(), unique_table;
  380. var shared_means = shared_table.find("tr:nth-last-child(2)");
  381. var mean_score, mean_diff;
  382. if ($(profile_links[0]).attr("href") == our_profile)
  383. our_pos = 1;
  384. else if ($(profile_links[1]).attr("href") == our_profile)
  385. our_pos = 2;
  386. else
  387. return;
  388. retrieve_scores(null, function(data) {
  389. var score_sum = 0, diff_sum = 0, score_nums = 0, diff_nums = 0;
  390. shared_table.find("tr").slice(1, -2).each(function(i, row) {
  391. var anime_id = $(row).find("a").attr("href").cut("/anime/", "/");
  392. var our_cell = $($(row).find("td")[our_pos]).find("span");
  393. var their_cell = $($(row).find("td")[our_pos == 1 ? 2 : 1]);
  394. var diff_cell = $($(row).find("td")[3]);
  395. load_score_into_element(data, anime_id, our_cell);
  396. if (our_cell.text() != "-") {
  397. score_sum += parseFloat(our_cell.text());
  398. score_nums++;
  399. }
  400. if (our_cell.text() != "-" && their_cell.text() != "-") {
  401. var diff = Math.abs(our_cell.text() - their_cell.text());
  402. diff_sum += diff;
  403. diff_cell.text(round_score(diff));
  404. diff_nums++;
  405. update_shared_row_colors($(row), our_pos);
  406. }
  407. });
  408. unique_table = $($("#content h2")[our_pos]).next();
  409. unique_table.find("tr").slice(1, -1).each(function(i, row) {
  410. var anime_id = $(row).find("a").attr("href").cut("/anime/", "/");
  411. var cell = $(row).find("td:nth(1)").find("span");
  412. load_score_into_element(data, anime_id, cell);
  413. });
  414. mean_score = round_score(score_sum / score_nums);
  415. if (!isNaN(mean_score)) {
  416. $(shared_means.find("td")[our_pos]).find("span").text(mean_score);
  417. update_shared_row_colors(shared_means, our_pos);
  418. }
  419. mean_diff = Math.round(diff_sum / diff_nums * 100) / 100;
  420. if (!isNaN(mean_diff))
  421. $(shared_means.find("td")[3]).text(mean_diff);
  422. });
  423. }
  424. function hook_addtolist() {
  425. /* TODO: this entry point is unimplemented - it's rarely used and difficult
  426. to inject into, so I'm avoiding it for now. */
  427. $("<p><b>Note:</b> For the time being, anime added through this " +
  428. "interface cannot be given scores on the 10.0-point scale (the old " +
  429. "10-point system is used).</p><p>To give a more specific number, " +
  430. "simply add the anime here, then go to its own page or to your list " +
  431. "page, and update the score.</p>").insertAfter($("#stype").parent());
  432. }
  433. /* ------------------------------- Main hook ------------------------------- */
  434. $(document).ready(function() {
  435. var href = window.location.href;
  436. if (href.contains("/animelist/")) {
  437. var list_info = $("#mal_cs_otherlinks div:first");
  438. if (list_info.text() == "You are viewing your anime list")
  439. hook_list();
  440. }
  441. else if ($("#malLogin").length == 0) {
  442. if (href.contains("/anime/") || href.contains("/anime.php"))
  443. hook_anime(get_anime_id_from_href(href));
  444. else if (href.contains("/panel.php") && href.contains("go=add"))
  445. hook_add();
  446. else if (href.contains("/editlist.php") && href.contains("type=anime"))
  447. hook_edit(get_edit_id_from_href(href));
  448. else if (href.contains("/shared.php") && !href.contains("type=manga"))
  449. hook_shared();
  450. else if (href.contains("/addtolist.php"))
  451. hook_addtolist();
  452. }
  453. });