/* ------------------------------- Constants ------------------------------- */ var MAX_BUCKETS = 256; var LOADING_IMG = ''; /* ------------------------ Miscellaneous functions ------------------------ */ function get_anime_id_from_href(href) { var anime_id; if (href.indexOf("/anime/") != -1) anime_id = href.substr(href.indexOf("/anime/") + "/anime/".length); else anime_id = href.substr(href.indexOf("id=") + "id=".length); if (anime_id.indexOf("/") != -1) anime_id = anime_id.substr(0, anime_id.indexOf("/")); if (anime_id.indexOf("&") != -1) anime_id = anime_id.substr(0, anime_id.indexOf("&")); return anime_id; } function get_edit_id_from_href(href) { var anime_id = href.substr(href.indexOf("id=") + "id=".length); if (anime_id.indexOf("&") != -1) anime_id = anime_id.substr(0, anime_id.indexOf("&")); return anime_id; } function get_scores_from_element(elem) { var score_100 = parseInt(elem.val()); var score_10 = Math.round(score_100 / 10.); if (isNaN(score_100) || score_100 < 1 || score_100 > 100) { alert("Invalid score: must be an integer between 1 and 100."); return null; } return [score_100, score_10]; } /* --------------------------- 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_scores = get_scores_from_element($("#scoretext" + anime_id)); if (new_scores === null) return; var new_score_100 = new_scores[0], new_score_10 = new_scores[1]; var payload = {id: anime_id, score: new_score_10}; $("#scorebutton" + anime_id).prop("disabled", true); $.post("/includes/ajax.inc.php?t=63", payload, function(data) { $("#scoreval" + anime_id).text(new_score_100); $("#scoretext" + anime_id).val(""); $("#scorediv" + anime_id).css("display", "none"); $("#scorebutton" + anime_id).prop("disabled", false); }); save_score(anime_id, new_score_100); } function update_anime_score(anime_id, is_new) { var new_scores = get_scores_from_element($("#myinfo_score")); if (new_scores === null) return; var new_score_100 = new_scores[0], new_score_10 = new_scores[1]; var t_id, payload = {score: new_score_10}; 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) { document.getElementById("myinfoDisplay").innerHTML = ""; document.getElementById("addtolist").innerHTML = data; } else document.getElementById("myinfoDisplay").innerHTML = data; }); save_score(anime_id, new_score_100); } function submit_add_form(submit_button) { var anime_id = $("input[name='series_title']").val(); if (!anime_id) return submit_button[0].click(); var new_scores = get_scores_from_element($("#score_input")); if (new_scores === null) return; var new_score_100 = new_scores[0], new_score_10 = new_scores[1]; $("select[name='score']").val(new_score_10); save_score(anime_id, new_score_100); submit_button[0].click(); } function submit_edit_form(anime_id, submit_type, submit_button) { if (submit_type == 2) { var new_scores = get_scores_from_element($("#score_input")); if (new_scores === null) return; var new_score_100 = new_scores[0], new_score_10 = new_scores[1]; $("select[name='score']").val(new_score_10); save_score(anime_id, new_score_100); } else if (submit_type == 3) remove_score(anime_id); submit_button[0].click(); } /* -------------------------------- Sorting -------------------------------- */ function sort_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('
'); }); $(".list-chart-group table").each(function(i, row) { $(row).add($(row).next()) .wrapAll(''); }); $(".list-chart-group").each(function(i, group) { $(group).find(".list-chart-row").sort(function (a, b) { return $(b).find("span[id^='scoreval']").text() - $(a).find("span[id^='scoreval']").text(); }).each(function(i, row) { $(group).append(row); }); $(group).find(".list-chart-row").each(function(i, row) { $(row).find("tr").first().children().first().text(i + 1); $(row).find((i % 2) ? ".td1" : ".td2").toggleClass("td1 td2"); }); }); } /* ---------------------------- Extension hooks ---------------------------- */ function hook_list() { retrieve_scores(null, function(data) { $("span[id^='scoreval']").each(function(i, elem) { var anime_id = elem.id.split("scoreval")[1]; var bucket_id = (parseInt(anime_id) % MAX_BUCKETS).toString(); var bucket = data[bucket_id]; if (bucket !== undefined && bucket[anime_id] !== undefined) $(elem).text(bucket[anime_id]); else { var current = parseInt($(elem).text()); if (!isNaN(current)) $(elem).text(current * 10); } $("#scorediv" + anime_id) .after($("Note: For the time being, anime added through this " + "interface cannot be given scores on the 100-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.indexOf("/animelist/") != -1) hook_list(); else if (href.indexOf("/anime/") != -1 || href.indexOf("/anime.php") != -1) hook_anime(get_anime_id_from_href(href)); else if (href.indexOf("/panel.php") != -1 && href.indexOf("go=add") != -1) hook_add(); else if (href.indexOf("/editlist.php") != -1 && href.indexOf("type=anime") != -1) hook_edit(get_edit_id_from_href(href)); else if (href.indexOf("/addtolist.php") != -1) hook_addtolist(); });