// ==UserScript==
// @name Familiar Checker
// @namespace http://caigawalker.plus.com/
// @description Version 1.1 - Provides a report on which familiars you have available.
// @include *127.0.0.1:*/main.php
// @include *127.0.0.1:*/familiar.php*
// @include *kingdomofloathing.com/main.php
// @include *kingdomofloathing.com/familiar.php*
// ==/UserScript==
// The latest version should be available from http://www.mobius-bandits.org/~jik/scripts/familiar_checker.user.js
// Change history
// 1.0 - Original release
// 1.1 - Permalink location is now in server-side configuration. Added the header "Cache-Control: no-cache" to the HTTP request because we are doing our own caching.
var data_url_list = ["http://www.mobius-bandits.org/~jik/scripts/familiar_checker_data.json"];
var script_version = "1.1";
var statuses = [];
var all_familiars = {};
var highest_familiar = 0;
var seen_items = {};
function version_compare(a, b) {
a_split = a.split(".");
b_split = b.split(".");
var comp_max = a_split.length > b_split.length ? b_split.length : a_split.length;
for(var i = 0; i < comp_max; i++) {
var lhs = parseInt(a_split[i]);
var rhs = parseInt(b_split[i]);
if(lhs > rhs) {
return 1;
} else if(lhs < rhs) {
return -1;
}
}
if(a_split.length > comp_max) {
return 1;
} else if(b_split.length > comp_max) {
return -1;
} else {
return 0;
}
}
function show_message(color, message) {
var warning_table = document.createElement("center");
warning_table.innerHTML = "
";
var page_body = document.getElementsByTagName("body")[0];
page_body.insertBefore(warning_table, page_body.firstChild);
}
function json_parse(json_data) {
if(typeof(JSON) != "undefined" && typeof(JSON.parse) != "undefined") {
parsed_data = JSON.parse(json_data);
} else {
// Oh noes! We're going to have to use eval()...
parsed_data = eval("(" + json_data + ")");
}
return parsed_data;
}
function update_data(url_list, callback) {
var data_url = url_list[0];
var data_modified_date = GM_getValue("data_modified_date", "");
var request_headers = { "User-agent": "Mozilla/4.0 (compatible) Greasemonkey", "Accept": "application/json", "Cache-Control": "no-cache" };
if(data_modified_date) {
request_headers["If-Modified-Since"] = data_modified_date;
}
GM_xmlhttpRequest({
method: "GET",
url: data_url,
headers: request_headers,
onload: function(resp) {
if(resp.status == 200) {
var match = /^Last-Modified:\s+(.*)/im.exec(resp.responseHeaders);
if(match) {
GM_setValue("data_modified_date", match[1]);
} else {
GM_log("The remote server returned no Last-Modified so our caching mechanism is broken, but the script should still work.");
}
GM_setValue("json_data", resp.responseText);
} else if(resp.status == 304) {
// Our cached data is still fresh. Yay!
} else {
GM_log(data_url + " failed, trying next URL");
// Failover to the next data URL.
url_list.shift();
if(url_list.length > 0) {
update_data(url_list, callback);
return;
} else {
show_message("red", "The script was unable to update the layout data needed to create a familiar report, so it is possible that new familiars will not be included in the report. The script attempts to get this data when you load main.php, so reloading this page may fix it. This is probably caused by a remote server failure.");
}
}
var json_data = GM_getValue("json_data", "");
if(json_data == "") {
show_message("red", "The script was unable to obtain the layout data needed to create a familiar report, so it will not work. The script attempts to get this data when you load main.php, so reloading this page may fix it. This is probably caused by a remote server failure.");
return;
} else {
var parsed_data = json_parse(json_data);
var available_version = parsed_data.version;
if(version_compare(script_version, available_version) == -1) {
show_message("orange", "A new version of Familiar Checker is available. Click here to download it.");
}
if(callback) {
callback();
}
}
}
});
}
function setupActionLink()
{
var report_table = document.createElement("center");
report_table.innerHTML = "