A Chrome extension that gives you finer control over MyAnimeList.net scores
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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