Просмотр исходного кода

Support edit popup/page; deleting.

master
Ben Kurtovic 10 лет назад
Родитель
Сommit
4e96775d69
2 измененных файлов: 94 добавлений и 12 удалений
  1. +92
    -11
      main.js
  2. +2
    -1
      manifest.json

+ 92
- 11
main.js Просмотреть файл

@@ -12,6 +12,13 @@ function get_anime_id_from_href(href) {
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.);
@@ -24,7 +31,19 @@ function get_scores_from_element(elem) {

/* Storage functions */

function get_scores(anime_id, callback) {
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();
@@ -42,19 +61,22 @@ function get_scores(anime_id, callback) {
});
}

function set_score(anime_id, score) {
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 = data[bucket_id] = {};
bucket[anime_id] = score;
chrome.storage.sync.set(data);
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 */
/* Event patches/injections */

function update_list_score(anime_id) {
var new_scores = get_scores_from_element($("#scoretext" + anime_id));
@@ -71,7 +93,7 @@ function update_list_score(anime_id) {
$("#scorediv" + anime_id).css("display", "none");
$("#scorebutton" + anime_id).prop("disabled", false);
});
set_score(anime_id, new_score_100);
save_score(anime_id, new_score_100);
}

function update_anime_score(anime_id, is_new) {
@@ -104,13 +126,29 @@ function update_anime_score(anime_id, is_new) {
else
document.getElementById("myinfoDisplay").innerHTML = data;
});
set_score(anime_id, new_score_100);
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() {
get_scores(null, function(data) {
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();
@@ -153,7 +191,7 @@ function hook_list() {
}

function hook_anime(anime_id) {
get_scores(anime_id, function(score) {
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";
@@ -185,6 +223,47 @@ function hook_anime(anime_id) {
});
}

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) {
var old_score = parseInt(old_input.val());
score = old_score == 0 ? "" : old_score * 10;
}

old_input.after($("<span> / 100</span>"))
.after($("<input>")
.attr("type", "text")
.attr("id", "score_input")
.attr("class", "inputtext")
.attr("value", score)
.attr("size", "3"))
.hide();

old_edit.after($("<input>")
.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, old_edit); }
}(anime_id, old_edit)))
.hide();

old_delete.after($("<input>")
.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, old_delete); }
}(anime_id, old_delete)))
.hide();
});
}

/* Main extension hook */

$(document).ready(function() {
@@ -193,4 +272,6 @@ $(document).ready(function() {
hook_list();
else if (href.indexOf("/anime/") != -1)
hook_anime(get_anime_id_from_href(href));
else if (href.indexOf("/editlist.php") != -1 && href.indexOf("type=anime") != -1)
hook_edit(get_edit_id_from_href(href));
});

+ 2
- 1
manifest.json Просмотреть файл

@@ -9,7 +9,8 @@

"content_scripts": [{
"matches": ["http://myanimelist.net/*"],
"js": ["jquery.min.js", "main.js"]
"js": ["jquery.min.js", "main.js"],
"all_frames": true
}],
"permissions": ["storage"]
}

Загрузка…
Отмена
Сохранить