/* -------------------------------- Globals -------------------------------- */
var MAX_BUCKETS = 256;
var LOADING_IMG = '';
var should_sort = window.location.href.indexOf("order=4") != -1;
/* ------------------------ Miscellaneous functions ------------------------ */
/* Note: complaints about modifying objects we don't own are ignored since
these changes are only executed within the context of our own extension. */
String.prototype.contains = function(substr) {
return this.indexOf(substr) != -1;
};
String.prototype.cut_after = function(substr) {
return this.substr(this.indexOf(substr) + substr.length)
};
String.prototype.cut_before = function(substr) {
return this.substr(0, this.indexOf(substr));
};
String.prototype.cut = function(after, before) {
return this.cut_after(after).cut_before(before);
}
function round_score(num) {
num = Math.round(num * 10) / 10;
if (isNaN(num))
return num;
if (num == Math.round(num))
num += ".0";
return num;
}
function get_anime_id_from_href(href) {
var anime_id;
if (href.contains("/anime/"))
anime_id = href.cut_after("/anime/");
else
anime_id = href.cut_after("id=");
if (anime_id.contains("/"))
anime_id = anime_id.cut_before("/");
if (anime_id.contains("&"))
anime_id = anime_id.cut_before("&");
return anime_id;
}
function get_edit_id_from_href(href) {
var anime_id = href.cut_after("id=");
if (anime_id.contains("&"))
anime_id = anime_id.cut_before("&");
return anime_id;
}
function get_score_from_element(elem) {
var score = round_score(elem.val());
if (isNaN(score) || ((score < 1 || score > 10) && score != 0)) {
alert("Invalid score: must be a number between 1.0 and 10.0, or 0.");
return null;
}
return score;
}
function load_score_into_element(data, anime_id, elem) {
var bucket = data[(parseInt(anime_id) % MAX_BUCKETS).toString()];
if (bucket !== undefined && bucket[anime_id] !== undefined)
elem.text(bucket[anime_id] == 0 ? "-" : bucket[anime_id]);
else {
var current = parseInt(elem.text());
if (!isNaN(current))
elem.text(current + ".0");
}
}
/* --------------------------- Storage functions --------------------------- */
function save_score(anime_id, score) {
var bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
chrome.storage.sync.get(bucket_id, function(data) {
var bucket = data[bucket_id];
if (bucket === undefined)
bucket = data[bucket_id] = {};
bucket[anime_id] = score;
chrome.storage.sync.set(data);
});
}
function retrieve_scores(anime_id, callback) {
var bucket_id = null;
if (anime_id !== null)
bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
chrome.storage.sync.get(bucket_id, function(data) {
if (anime_id !== null) {
var bucket = data[bucket_id];
if (bucket !== undefined && bucket[anime_id] !== undefined)
callback(bucket[anime_id]);
else
callback(null);
}
else
callback(data);
});
}
function remove_score(anime_id) {
var bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString();
chrome.storage.sync.get(bucket_id, function(data) {
var bucket = data[bucket_id];
if (bucket === undefined || bucket[anime_id] === undefined)
return;
delete bucket[anime_id];
if ($.isEmptyObject(bucket))
chrome.storage.sync.remove(bucket_id);
else
chrome.storage.sync.set(data);
});
}
/* ----------------------- Event patches/injections ------------------------ */
function update_list_score(anime_id) {
var new_score = get_score_from_element($("#scoretext" + anime_id));
if (new_score === null)
return;
var payload = {id: anime_id, score: Math.round(new_score)};
$("#scorebutton" + anime_id).prop("disabled", true);
$.post("/includes/ajax.inc.php?t=63", payload, function(data) {
$("#scoreval" + anime_id).text(new_score == 0 ? "-" : new_score);
$("#scoretext" + anime_id).val("");
$("#scorediv" + anime_id).css("display", "none");
$("#scorebutton" + anime_id).prop("disabled", false);
if (should_sort)
sort_list();
update_list_stats();
});
save_score(anime_id, new_score);
}
function update_anime_score(anime_id, is_new) {
var new_score = get_score_from_element($("#myinfo_score"));
if (new_score === null)
return;
var t_id, payload = {score: Math.round(new_score)};
payload["status"] = $("#myinfo_status").val();
payload["epsseen"] = $("#myinfo_watchedeps").val();
if (is_new) {
payload["aid"] = anime_id;
t_id = "61";
}
else {
payload["alistid"] = anime_id;
payload["aid"] = $("#myinfo_anime_id").val();
payload["astatus"] = $("#myinfo_curstatus").val();
t_id = "62";
}
$("#myinfoDisplay").html(LOADING_IMG);
$.post("/includes/ajax.inc.php?t=" + t_id, payload, function(data) {
if (is_new) {
$("#myinfoDisplay").html("");
$("#addtolist").html(data);
}
else
$("#myinfoDisplay").html(data);
});
save_score(anime_id, new_score);
}
function submit_add_form(submit_button) {
var anime_id = $("input[name='series_title']").val();
if (!anime_id)
return submit_button[0].click();
var new_score = get_score_from_element($("#score_input"));
if (new_score === null)
return;
$("select[name='score']").val(Math.round(new_score));
save_score(anime_id, new_score);
submit_button[0].click();
}
function submit_edit_form(anime_id, submit_type, submit_button) {
if (submit_type == 2) {
var new_score = get_score_from_element($("#score_input"));
if (new_score === null)
return;
$("select[name='score']").val(Math.round(new_score));
save_score(anime_id, new_score);
}
else if (submit_type == 3)
remove_score(anime_id);
submit_button[0].click();
}
/* ------------------------ List stats and sorting ------------------------- */
function compare(row1, row2) {
var r1 = $(row1).find("span[id^='scoreval']").text(),
r2 = $(row2).find("span[id^='scoreval']").text();
if (r1 == r2) {
r1 = $(row1).find("a.animetitle span").text();
r2 = $(row2).find("a.animetitle span").text();
return r1 > r2 ? 1 : -1;
}
if (r1 == "-")
return 1;
if (r2 == "-")
return -1;
return r2 - r1;
}
function prepare_list() {
var headers = [".header_cw", ".header_completed", ".header_onhold",
".header_dropped", ".header_ptw"];
$.each(headers, function(i, header) {
$(header).next()
.nextUntil($(".category_totals").closest("table"))
.wrapAll('
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()); } /* ------------------------------- 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(); } });