A Chrome extension that gives you finer control over MyAnimeList.net scores
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

197 řádky
6.6 KiB

  1. /* Constants */
  2. var MAX_BUCKETS = 256;
  3. var LOADING_IMG = '<img src="http://cdn.myanimelist.net/images/xmlhttp-loader.gif" align="center">';
  4. /* Miscellaneous functions */
  5. function get_anime_id_from_href(href) {
  6. var anime_id = href.substr(href.indexOf("/anime/") + "/anime/".length);
  7. if (anime_id.indexOf("/") != -1)
  8. anime_id = anime_id.substr(0, anime_id.indexOf("/"));
  9. return anime_id;
  10. }
  11. function get_scores_from_element(elem) {
  12. var score_100 = parseInt(elem.val());
  13. var score_10 = Math.round(score_100 / 10.);
  14. if (isNaN(score_100) || score_100 < 1 || score_100 > 100) {
  15. alert("Invalid score: must be an integer between 1 and 100.");
  16. return null;
  17. }
  18. return [score_100, score_10];
  19. }
  20. /* Storage functions */
  21. function get_scores(anime_id, callback) {
  22. var bucket_id = null;
  23. if (anime_id !== null)
  24. bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
  25. chrome.storage.sync.get(bucket_id, function(data) {
  26. if (anime_id !== null) {
  27. var bucket = data[bucket_id];
  28. if (bucket !== undefined && bucket[anime_id] !== undefined)
  29. callback(bucket[anime_id]);
  30. else
  31. callback(null);
  32. }
  33. else
  34. callback(data);
  35. });
  36. }
  37. function set_score(anime_id, score) {
  38. var bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
  39. chrome.storage.sync.get(bucket_id, function(data) {
  40. var bucket = data[bucket_id];
  41. if (bucket === undefined)
  42. bucket = data[bucket_id] = {};
  43. bucket[anime_id] = score;
  44. chrome.storage.sync.set(data);
  45. });
  46. }
  47. /* Event patches */
  48. function update_list_score(anime_id) {
  49. var new_scores = get_scores_from_element($("#scoretext" + anime_id));
  50. if (new_scores === null)
  51. return;
  52. var new_score_100 = new_scores[0], new_score_10 = new_scores[1];
  53. var payload = {id: anime_id, score: new_score_10};
  54. $("#scorebutton" + anime_id).prop("disabled", true);
  55. $.post("/includes/ajax.inc.php?t=63", payload, function(data) {
  56. $("#scoreval" + anime_id).text(new_score_100);
  57. $("#scoretext" + anime_id).val("");
  58. $("#scorediv" + anime_id).css("display", "none");
  59. $("#scorebutton" + anime_id).prop("disabled", false);
  60. });
  61. set_score(anime_id, new_score_100);
  62. }
  63. function update_anime_score(anime_id, is_new) {
  64. var new_scores = get_scores_from_element($("#myinfo_score"));
  65. if (new_scores === null)
  66. return;
  67. var new_score_100 = new_scores[0], new_score_10 = new_scores[1];
  68. var t_id, payload = {score: new_score_10};
  69. payload["status"] = $("#myinfo_status").val();
  70. payload["epsseen"] = $("#myinfo_watchedeps").val();
  71. if (is_new) {
  72. payload["aid"] = anime_id;
  73. t_id = "61";
  74. }
  75. else {
  76. payload["alistid"] = anime_id;
  77. payload["aid"] = $("#myinfo_anime_id").val();
  78. payload["astatus"] = $("#myinfo_curstatus").val();
  79. t_id = "62";
  80. }
  81. $("#myinfoDisplay").html(LOADING_IMG);
  82. $.post("/includes/ajax.inc.php?t=" + t_id, payload, function(data) {
  83. if (is_new) {
  84. document.getElementById("myinfoDisplay").innerHTML = "";
  85. document.getElementById("addtolist").innerHTML = data;
  86. }
  87. else
  88. document.getElementById("myinfoDisplay").innerHTML = data;
  89. });
  90. set_score(anime_id, new_score_100);
  91. }
  92. /* Extension hooks */
  93. function hook_list() {
  94. get_scores(null, function(data) {
  95. $("span[id^='scoreval']").each(function(i, elem) {
  96. var anime_id = elem.id.split("scoreval")[1];
  97. var bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
  98. var bucket = data[bucket_id];
  99. if (bucket !== undefined && bucket[anime_id] !== undefined)
  100. $(elem).text(bucket[anime_id]);
  101. else {
  102. var current = parseInt($(elem).text());
  103. if (!isNaN(current))
  104. $(elem).text(current * 10);
  105. }
  106. $("#scorediv" + anime_id)
  107. .after($("<div>")
  108. .attr("id", "scorediv" + anime_id)
  109. .css("display", "none")
  110. .append($('<input>')
  111. .attr("type", "text")
  112. .attr("id", "scoretext" + anime_id)
  113. .attr("size", "2")
  114. .keydown(function(a_id) {
  115. return function(ev) {
  116. if ((window.event ? window.event.keyCode : ev.which) == 13)
  117. update_list_score(a_id);
  118. else
  119. return true;
  120. }
  121. }(anime_id)))
  122. .append($("<input>")
  123. .attr("type", "button")
  124. .attr("id", "scorebutton" + anime_id)
  125. .attr("value", "Go")
  126. .click(function(a_id) {
  127. return function() { return update_list_score(a_id); }
  128. }(anime_id))))
  129. .remove();
  130. });
  131. });
  132. }
  133. function hook_anime(anime_id) {
  134. get_scores(anime_id, function(score) {
  135. var old_input = $("#myinfo_score");
  136. var old_button = $("input[name='myinfo_submit']");
  137. var is_new = old_button.attr("value") == "Add";
  138. if (!is_new && score === null) {
  139. var old_score = parseInt(old_input.val());
  140. score = old_score == 0 ? "" : old_score * 10;
  141. }
  142. old_input.after($("<span> / 100</span>"))
  143. .after($("<input>")
  144. .attr("type", "text")
  145. .attr("id", "myinfo_score")
  146. .attr("name", "myinfo_score")
  147. .attr("class", "inputtext")
  148. .attr("value", (score === null) ? "" : score)
  149. .attr("size", "3"))
  150. .remove();
  151. old_button.after($("<input>")
  152. .attr("type", "button")
  153. .attr("name", "myinfo_submit")
  154. .attr("value", old_button.attr("value"))
  155. .attr("class", "inputButton")
  156. .click(function(a_id, is_new) {
  157. return function() { return update_anime_score(a_id, is_new); }
  158. }(anime_id, is_new)))
  159. .remove();
  160. });
  161. }
  162. /* Main extension hook */
  163. $(document).ready(function() {
  164. var href = window.location.href;
  165. if (href.indexOf("/animelist/") != -1)
  166. hook_list();
  167. else if (href.indexOf("/anime/") != -1)
  168. hook_anime(get_anime_id_from_href(href));
  169. });