")
.attr("id", "scorediv" + anime_id)
.hide()
.append($('
')
.attr("type", "text")
.attr("id", "scoretext" + anime_id)
.attr("size", "2")
.keydown(function(a_id) {
return function(ev) {
if ((window.event ? window.event.keyCode : ev.which) == 13)
update_list_score(a_id);
else
return true;
}
}(anime_id)))
.append($("
")
.attr("type", "button")
.attr("id", "scorebutton" + anime_id)
.attr("value", "Go")
.click(function(a_id) {
return function() { return update_list_score(a_id); }
}(anime_id))))
.remove();
});
prepare_list();
if (should_sort)
sort_list();
update_list_stats();
});
}
function hook_anime(anime_id) {
retrieve_scores(anime_id, function(score) {
var old_input = $("#myinfo_score");
var old_button = $("input[name='myinfo_submit']");
var is_new = old_button.attr("value") == "Add";
if (!is_new && score === null)
score = parseInt(old_input.val()) + ".0";
old_input.after($("
/ 10.0"))
.after($("
")
.attr("type", "text")
.attr("id", "myinfo_score")
.attr("name", "myinfo_score")
.attr("class", "inputtext")
.attr("value", (score === null || score == 0) ? "" : score)
.attr("size", "3"))
.remove();
old_button.after($("
")
.attr("type", "button")
.attr("name", "myinfo_submit")
.attr("value", old_button.attr("value"))
.attr("class", "inputButton")
.click(function(a_id, is_new) {
return function() { return update_anime_score(a_id, is_new); }
}(anime_id, is_new)))
.remove();
});
}
function hook_add() {
var old_input = $("select[name='score']");
var old_submit = $("input[type='button'][onclick='checkValidSubmit(1)']");
old_input.after($("
/ 10.0"))
.after($("
")
.attr("type", "text")
.attr("id", "score_input")
.attr("class", "inputtext")
.attr("size", "3"))
.hide();
old_submit.after($("
")
.attr("type", "button")
.attr("class", "inputButton")
.attr("style", old_submit.attr("style"))
.attr("value", old_submit.attr("value"))
.click(function(button) {
return function() { return submit_add_form(button); }
}(old_submit)))
.hide();
}
function hook_edit(anime_id) {
retrieve_scores(anime_id, function(score) {
var old_input = $("select[name='score']");
var old_edit = $("input[type='button'][onclick='checkValidSubmit(2)']");
var old_delete = $("input[type='button'][onclick='checkValidSubmit(3)']");
if (score === null)
score = parseInt(old_input.val()) + ".0";
old_input.after($("
/ 10.0"))
.after($("
")
.attr("type", "text")
.attr("id", "score_input")
.attr("class", "inputtext")
.attr("value", score == 0 ? "" : score)
.attr("size", "3"))
.hide();
old_edit.after($("
")
.attr("type", "button")
.attr("class", "inputButton")
.attr("style", old_edit.attr("style"))
.attr("value", old_edit.attr("value"))
.click(function(a_id, button) {
return function() { return submit_edit_form(a_id, 2, button); }
}(anime_id, old_edit)))
.hide();
old_delete.after($("
")
.attr("type", "button")
.attr("class", "inputButton")
.attr("value", old_delete.attr("value"))
.click(function(a_id, button) {
return function() { return submit_edit_form(a_id, 3, button); }
}(anime_id, old_delete)))
.hide();
});
}
function hook_shared() {
var our_profile = $("#nav a:first").attr("href"), our_pos;
var profile_links = $("#content h2:first").find("a").slice(1);
var shared_table = $("#content h2:first").next(), unique_table;
var shared_means = shared_table.find("tr:nth-last-child(2)");
var mean_score, mean_diff;
if ($(profile_links[0]).attr("href") == our_profile)
our_pos = 1;
else if ($(profile_links[1]).attr("href") == our_profile)
our_pos = 2;
else
return;
retrieve_scores(null, function(data) {
var score_sum = 0, diff_sum = 0, score_nums = 0, diff_nums = 0;
shared_table.find("tr").slice(1, -2).each(function(i, row) {
var anime_id = $(row).find("a").attr("href").cut("/anime/", "/");
var our_cell = $($(row).find("td")[our_pos]).find("span");
var their_cell = $($(row).find("td")[our_pos == 1 ? 2 : 1]);
var diff_cell = $($(row).find("td")[3]);
load_score_into_element(data, anime_id, our_cell);
if (our_cell.text() != "-") {
score_sum += parseFloat(our_cell.text());
score_nums++;
}
if (our_cell.text() != "-" && their_cell.text() != "-") {
var diff = Math.abs(our_cell.text() - their_cell.text());
diff_sum += diff;
diff_cell.text(round_score(diff));
diff_nums++;
update_shared_row_colors($(row), our_pos);
}
});
unique_table = $($("#content h2")[our_pos]).next();
unique_table.find("tr").slice(1, -1).each(function(i, row) {
var anime_id = $(row).find("a").attr("href").cut("/anime/", "/");
var cell = $(row).find("td:nth(1)").find("span");
load_score_into_element(data, anime_id, cell);
});
mean_score = round_score(score_sum / score_nums);
if (!isNaN(mean_score)) {
$(shared_means.find("td")[our_pos]).find("span").text(mean_score);
update_shared_row_colors(shared_means, our_pos);
}
mean_diff = Math.round(diff_sum / diff_nums * 100) / 100;
if (!isNaN(mean_diff))
$(shared_means.find("td")[3]).text(mean_diff);
});
}
function hook_addtolist() {
/* TODO: this entry point is unimplemented - it's rarely used and difficult
to inject into, so I'm avoiding it for now. */
$("
Note: For the time being, anime added through this " +
"interface cannot be given scores on the 10.0-point scale (the old " +
"10-point system is used).
To give a more specific number, " +
"simply add the anime here, then go to its own page or to your list " +
"page, and update the score.
").insertAfter($("#stype").parent());
}
function hook_export() {
chrome.storage.sync.getBytesInUse(null, function(usage) {
usage = Math.round(usage / 1024 * 10) / 10;
usage += " kB / " + chrome.storage.sync.QUOTA_BYTES / 1024 + " kB";
$("#dialog td")
.append($("
")
.css("border", "none")
.css("background-color", "#AAA")
.css("height", "1px"))
.append($("
")
.html("The regular list export above will only include " +
"rounded scores. You can export your decimal scores " +
"separately and " +
'import ' +
"them later."))
.append($("
")
.attr("class", "spaceit")
.html("Chrome Sync usage: " + usage))
.append($("
")
.attr("type", "submit")
.attr("value", "Export Decimal Scores")
.attr("class", "inputButton")
.click(export_scores));
});
}
function hook_import() {
$("#content").append($("
")
.css("border", "none")
.css("background-color", "#AAA")
.css("height", "1px"))
.append($("
")
.html("You can also import decimal scores here. Doing so will " +
"erase any existing decimal scores."))
.append($("
")
.attr("class", "spaceit")
.append($("
")
.attr("id", "decimal-file")
.attr("size", "60")
.attr("type", "file")
.attr("class", "inputtext")))
.append($("
")
.attr("id", "decimal-submit")
.attr("type", "submit")
.attr("value", "Import Decimal Scores")
.attr("class", "inputButton")
.click(function() {
var filelist = $("#decimal-file")[0].files, file, reader;
if (filelist.length != 1)
return;
file = filelist[0];
if (file.type != "application/json") {
alert("Invalid file type: must be .json.");
return;
}
reader = new FileReader();
reader.onload = function() {
try {
import_scores(JSON.parse(reader.result), function() {
$("#decimal-file").after("
Success!
")
.remove();
$("#decimal-submit").remove();
});
} catch (exc) {
if (typeof exc === "object")
alert("The file could not be parsed as JSON.");
else
alert("Error validating data: " + exc);
}
};
reader.readAsText(file);
}));
}
/* ------------------------------- Main hook ------------------------------- */
$(document).ready(function() {
var href = window.location.href;
if (href.contains("/animelist/")) {
var list_info = $("#mal_cs_otherlinks div:first");
if (list_info.text() == "You are viewing your anime list")
hook_list();
}
else if ($("#malLogin").length == 0) {
if (href.contains("/anime/") || href.contains("/anime.php"))
hook_anime(get_anime_id_from_href(href));
else if (href.contains("/panel.php") && href.contains("go=add"))
hook_add();
else if (href.contains("/editlist.php") && href.contains("type=anime"))
hook_edit(get_edit_id_from_href(href));
else if (href.contains("/shared.php") && !href.contains("type=manga"))
hook_shared();
else if (href.contains("/addtolist.php"))
hook_addtolist();
else if (href.contains("/panel.php") && href.contains("go=export"))
hook_export();
else if (href.contains("/import.php"))
hook_import();
}
});