/* Constants */ var MAX_BUCKETS = 256; var LOADING_IMG = ''; /* Miscellaneous functions */ function get_anime_id_from_href(href) { var anime_id = href.substr(href.indexOf("/anime/") + "/anime/".length); 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_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(); } /* 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($("