try to take over the world!
当前为
// ==UserScript==
// @name Imperia Online Espionage Extensions Firefox
// @namespace tar
// @version 1.0
// @description try to take over the world!
// @author ChoMPi
// @match http://*.imperiaonline.org/imperia/game_v6/game/village.php*
// ==/UserScript==
/* jshint -W097 */
'use strict';
function addXMLRequestCallback(callback)
{
XMLHttpRequest.newCallback = callback;
XMLHttpRequest.oldSend = XMLHttpRequest.prototype.send;
// override the native send()
XMLHttpRequest.prototype.send = function()
{
var self = this;
this.addEventListener("readystatechange", function()
{
if (self.readyState == 4 && self.status == 200) {
XMLHttpRequest.newCallback(self);
}
}, false);
this.requestData = (arguments.length > 0) ? decodeURIComponent(arguments[0]) : null;
// call the native send()
XMLHttpRequest.oldSend.apply(this, arguments);
}
}
function timeSince(date)
{
var seconds = Math.floor((new Date() - date) / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(seconds / 3600);
var days = Math.floor(seconds / 86400);
var months = Math.floor(seconds / 2592000);
var years = Math.floor(seconds / 31536000);
if (seconds < 60) {
return Math.floor(seconds) + " seconds";
}
else if (minutes < 60) {
return minutes + ' minute' + (minutes > 1 ? 's' : '');
}
else if (hours < 24) {
return hours + ' hour' + (hours > 1 ? 's' : '');
}
else if (days < 30) {
return days + ' day' + (days > 1 ? 's' : '');
}
else if (months < 12) {
return months + ' month' + (months > 1 ? 's' : '');
}
else {
return years + ' year' + (years > 1 ? 's' : '');
}
}
function GetSpyRowID(element)
{
return $(element).next().attr('id').replace('hidden-tr-', '');
}
function GetSpyNotes()
{
if (typeof localStorage.imperia_spy_notes == 'undefined')
return {};
return JSON.parse(localStorage.imperia_spy_notes);
}
function GetSpyNote(id)
{
var notes = GetSpyNotes();
if (typeof notes[id] != 'undefined') {
return notes[id];
}
return '';
}
function SaveSpyNote(id, note)
{
var notes = GetSpyNotes();
notes[id] = note;
localStorage.imperia_spy_notes = JSON.stringify(notes);
}
function OnWindowEspionage()
{
var opCenter = $('#operation-center .spy-wrapper');
var dataGrid = $('.data-grid', opCenter);
if (dataGrid.length > 0)
{
dataGrid.find('tr.stripe').each(function(i, e)
{
var id = GetSpyRowID(e);
var noteText = GetSpyNote(id);
var lastAttack = GetLastAttackString(id);
if (lastAttack != '')
{
var attackField = $('<div style="font-size:12px; padding-top:3px; color: #632626;">Last attack order: ' + lastAttack + ' ago</div>');
$('td:eq(1)', e).append(attackField);
}
var noteField = $('<div style="font-size:12px; padding-top:3px; color: #675E49; ' + (noteText == '' ? 'display:none;' : '') + '">' + noteText + '</div>');
$('td:eq(1)', e).append(noteField);
var form = $('<form onSubmit="return false;" style="display:none;"><textarea rows="2" cols="40"></textarea><br><input class="button" type="submit" value="Save" /></form>');
$('td:eq(1)', e).append(form);
var button = $('<a href="javascript:void(0);" id="' + id + '" class="button icons rename-bookmark" title="Set Note"><span></span></a>');
$('td.actions div', e).append(button);
form.submit(function()
{
SaveSpyNote(id, $('textarea', form).val());
noteField.html($('textarea', form).val());
form.fadeOut('fast', function()
{
if (noteField.html() != '')
noteField.fadeIn('fast');
});
return false;
});
button.click(function()
{
if (form.is(':visible')) {
form.fadeOut('fast', function() {
if (noteField.html() != '') {
noteField.fadeIn('fast');
}
});
} else {
$('textarea', form).val(GetSpyNote(id));
noteField.fadeOut('fast', function() {
form.fadeIn('fast');
});
}
});
});
}
}
function GetLastAttacks()
{
if (typeof localStorage.imperia_last_attacks == 'undefined')
return {};
return JSON.parse(localStorage.imperia_last_attacks);
}
function GetLastAttack(id)
{
var attacks = GetLastAttacks();
if (typeof attacks[id] != 'undefined') {
return parseInt(attacks[id]);
}
return 0;
}
function GetLastAttackString(id)
{
var attackTime = GetLastAttack(id);
if (attackTime > 0) {
return timeSince(attackTime);
}
return '';
}
function SaveLastAttack(id, time)
{
var attacks = GetLastAttacks();
attacks[id] = time;
localStorage.imperia_last_attacks = JSON.stringify(attacks);
}
function OnDoAttack(requestData, response)
{
var id;
var userid = requestData.match(/<e><k>dUserId<\/k><v>S(\d+)<\/v><\/e>/)[1];
var provinceid = requestData.match(/<e><k>dProvinceId<\/k><v>S(\d+)<\/v><\/e>/)[1];
var name = requestData.match(/<e><k>dName<\/k><v>S([\_\-\.a-zA-Z0-9]*)<\/v><\/e>/)[1];
// Check if it's NPC or player
if (name == '' || name.substring(0, 3) == 'NPC') {
id = userid + '-' + provinceid;
} else {
id = userid + '-' + name;
}
// Check the response for error
if (response.match(/\<cmd cmd\=\"as\" id\=\"errorContainerOperationCenter\" prop\=\"innerHTML\"\>/)) {
return;
}
SaveLastAttack(id, new Date().getTime());
}
function Init()
{
addXMLRequestCallback(function(xhr) {
if (xhr.requestData != null) {
// Check for the espionage window
if (xhr.requestData.indexOf('xjxfun=viewOperationCenter') > -1) {
if (xhr.response.indexOf('tab-espionage active') > -1) {
OnWindowEspionage();
}
}
// Check for attack
if (xhr.requestData.indexOf('xjxfun=doAttack') > -1) {
if (xhr.requestData != null) {
OnDoAttack(xhr.requestData, xhr.response);
}
}
}
});
}
$(document).ready(function()
{
function InitCheck()
{
if (!$('body').hasClass('loading'))
{
Init();
}
else
{
setTimeout(InitCheck, 500);
}
}
InitCheck();
});