Free javascript Hosting


jobhelper.js

Uploaded on Oct 26 2009 19:15 by acridcola

/*
Credits:
Vern for a lot of code and inspiration, http://vern.com/mwtools/
Francisco Moraes for code to handle mw-friends only.
Max Power for the loot logging code.
*/

javascript:(function (){
if (navigator.appName == 'Microsoft Internet Explorer') {
alert('You are using Internet Explorer, this bookmarklet will not work.\nUse Firefox or Chrome instead.');
return;
}
var version='Job Helper v1.04',run=1,xmlHTTP,content=$('app10979261223_content_row'),current=null,debug=false,
mafia_size=$("app10979261223_user_group_size").parentNode.parentNode,second=false,cuba=0,ny=0,moscow=0,moscowonly=false,
last_url=null,retries=0,tmpkey=false,totalfriends=0,combinedloot='',done=0,both=false,do_level_boost=true;
mw_url='http://apps.facebook.com/inthemafia/remote/html_server.php?xw_controller=stats&xw_action=view&user=',
fb_url='http://www.facebook.com/profile.php?id=',cuba_gained=0,ny_gained=0,moscow_gained=0,exp_gained=0,friends_helped=0;
mafia_size=$("app10979261223_user_group_size").parentNode.parentNode;
mafia_size.innerHTML='<nobr>'+mafia_size.innerHTML.replace(/&nbsp;/g,'')+'</nobr>';
if (k=(/FBIntern.CacheUtil.CacheUtilServer.userfriends = \[(.*)\]/.exec(document.body.innerHTML))) {
friendarray=k[1];
}
else { alert('Could not load the friend list, are you on a Mafia Wars Page'); return false; }
friends=[],savedfriends=[];
if (!friendarray) {
alert('Could not load friend list, are you on a Mafia Wars page?');
}
else {
friends=friendarray.split(',');
}
if (typeof(a10979261223_groups_levels) == "undefined"){
specialmsg='<tr><td colspan="3">Running the bookmarklet from your gift page will remove non-mw playing friends from the list</td></tr>';
}
else {
newfriends = [];
c = 0;
for(i = 0; i < friends.length; i++) {
if(friends[i] in a10979261223_groups_levels)
newfriends[c++] = friends[i];
}
specialmsg = '<tr><td colspan="3">Found '+newfriends.length+' MW friends out of '+friends.length+' total friends.</td></tr>';
friends = newfriends;
}
wait = readCookie('spockjobs_wait');
if (wait) wait=wait.replace(/[^0-9]/g,'');
if ((wait == null) || (wait.length == 0)) wait = 2;

skip = readCookie('spockjobs_skip');
if (skip) skip=skip.replace(/[^0-9]/g,'');
if ((skip == null) || (skip.length == 0)) skip = 0;

redo="";

var config_html =
'<style type="text/css">'+
'.messages img{margin:0 3px;vertical-align:top};'+
'#close{display:inline};'+
'</style>'+
'<form name="spockform">'+
'<table class="messages">'+
'<tr><td colspan="2">Configuration</td><td align="right" style="text-align:right;">'+version+' - <a href="http://www.spockholm.com/mafia/bookmarklets.php" target="_top">Spockholm Mafia Tools</a> - <a href="#" id="close"><img src="http://www.spockholm.com/mafia/stop.gif" title="Close" width="16" height="16"></a></td></tr>'+
specialmsg+
'<tr><td>Delay:</td><td><input type="text" name="wait" id="wait" value="'+wait+'"></td><td>Delay in seconds between sends.</td></tr>'+
'<tr><td>Skip:</td><td><input type="text" name="skip" id="skip" value="'+skip+'"></td><td>Friends to skip</td></tr>'+
'<tr><td valign="top">Cities:</td><td><input type="checkbox" name="ny" id="ny" checked />&nbsp;New York<br />'+
'<input type="checkbox" name="cuba" id="cuba" checked />&nbsp;Cuba<br />'+
'<input type="checkbox" name="moscow" id="moscow" checked />&nbsp;Moscow<br />'+
'<input type="checkbox" name="moscowboss" id="moscowboss" checked />&nbsp;Moscow Boss</td><td valign="top">Select cities to check for jobs.</td></tr>'+
'<tr><td>Restart: </td><td><input type="checkbox" name="redo" id="redo" '+redo+'/></td><td colspan="2"> Restart processing from the beginning when we are done.</td>'+
//'<tr><td>Debug: </td><td><input type="checkbox" name="debug" id="debug" /></td><td colspan="2"> Output a lot to console for debugging?</td></tr>'+
'<tr><td colspan="3"><a class="sexy_button" id="start">Start</a></td></tr>'+
'</table>'+
'</form>';

var running_html =
'<style type="text/css">'+
'.messages img{margin:0 3px;vertical-align:top}'+
'#play{display:none}'+
'#pause{display:inline}'+
'#close{display:inline}'+
'</style>'+
'<table class="messages">'+
'<tr>'+
'<td colspan="3" style="text-align:right;">'+version+' - <a href="http://www.spockholm.com/mafia/bookmarklets.php" target="_top">Spockholm Mafia Tools</a> - <a href="#" id="play"><img src="http://www.spockholm.com/mafia/play.gif" title="Play" width="16" height="16" /></a> <a href="#" id="pause"><img src="http://www.spockholm.com/mafia/pause.gif" title="Pause" width="16" height="16" /></a> <a href="#" id="close"><img src="http://www.spockholm.com/mafia/stop.gif" title="Close" width="16" height="16" /></a></td>'+
'</tr>'+
'<tr>'+
'<td width="100">Progress:</td>'+
'<td id="progress"></td>'+
'<td align="right" style="text-align:right;">Delay: <span id="delay">'+wait+'</span> sec (<a href="#" id="increase">+</a> / <a href="#" id="decrease">-</a>)</td>'+
'</tr>'+
'<tr>'+
'<td>Friends helped:</td>'+
'<td colspan="2" id="friends_helped"></td>'+
'</tr>'+
'<tr>'+
'<td>Exp gained:</td>'+
'<td colspan="2" id="exp_gained"></td>'+
'</tr>'+
'<tr>'+
'<td>Money gained:</td>'+
'<td colspan="2" id="money_gained"></td>'+
'</tr>'+
'<tr>'+
'<td>Status:</td>'+
'<td colspan="2" id="status"></td>'+
'</tr>'+
'<tr>'+
'<td valign="top"><a href="#" id="logshow">Showing</a> Log:</td>'+
'<td colspan="2" id="log"></td>'+
'</tr>'+
'</table>';
create_div();
$("close").onclick=close;
function close(){
run = 0;
delete xmlHTTP['onreadystatechange'];
$("app10979261223_content_row").removeChild($("spockdiv"));
return false;
}
$("start").onclick=start;
function start(){
run = 1;
wait = parseInt(document.spockform.wait.value);
createCookie("spockjobs_wait",wait);
skip = parseInt(document.spockform.skip.value);
createCookie("spockjobs_skip",skip);

if (document.spockform.redo.checked) { redo = "checked"; }
else { redo = ""; }
//if (document.spockform.debug.checked) { debug = true; }
//make array with the valid cities
cities = [];
if (document.spockform.ny.checked) { cities.push('New York'); }
if (document.spockform.cuba.checked) { cities.push('Cuba'); }
if (document.spockform.moscow.checked) { cities.push('Moscow'); }
if (document.spockform.moscowboss.checked) { cities.push('Boss Moscow'); }
if (cities.length == 0) { cities = ['New York','Cuba','Moscow','Boss Moscow']; }
validcities = cities;

$('spockdiv').innerHTML = running_html;
$("pause").onclick=pause;
function pause(){
run = 0;
$("pause").style.display = 'none';
$("play").style.display = 'inline';
return false;
}
$("play").onclick=play;
function play(){
run = 1;
$("play").style.display = 'none';
$("pause").style.display = 'inline';
msg('Resuming job searching... (<a href="'+last_url+'">url</a>)');
retries = 0;
request(last_url);
return false;
}
$("close").onclick=close;
function close(){
run = 0;
delete xmlHTTP['onreadystatechange'];
$("app10979261223_content_row").removeChild($("spockdiv"));
return false;
}
$("increase").onclick=increase;
function increase(){
wait++;
$('delay').innerHTML = wait;
createCookie("spockjobs_wait",wait);
return false;
}
$("decrease").onclick=decrease;
function decrease(){
if(wait <= 0) { wait = 0; }
else { wait--; }
$('delay').innerHTML = wait;
createCookie("spockjobs_wait",wait);
return false;
}
$("logshow").onclick=log_toggle;
function log_toggle() {
var row = document.getElementById("log");
if (row.style.display == '') {
row.style.display = 'none';
$("logshow").innerHTML = 'Hiding';
}
else {
row.style.display = '';
$("logshow").innerHTML = 'Showing';
}
return false;
}
savedfriends = friends;
if (skip >= friends.length -1) { skip=0; }
if (skip > 0) {
log('Skipping '+skip+' friends of '+friends.length+'.');
friends=friends.slice(skip);
}
totalfriends=friends.length;
$('progress').innerHTML = done+' of '+totalfriends+' &nbsp; <span class="more_in">('+(done/totalfriends*100).toFixed(1)+'%)</span>';
search_job();
}
function create_div() {
if($('spockdiv')) { $('spockdiv').innerHTML = config_html; }
else {
var spock_div=document.createElement("div");
spock_div.id = 'spockdiv';
content.insertBefore(spock_div, content.firstChild);
$('spockdiv').innerHTML = config_html;
}
}
function get_xmlHTTP () {
if (window.XMLHttpRequest)
return new XMLHttpRequest();
if (window.ActiveXObject)
return new ActiveXObject('Microsoft.XMLHTTP');
return null;
}
function request(url) {
if (run == 1) {
xmlHTTP.onreadystatechange=state_change;
xmlHTTP.open('GET',url,true);
xmlHTTP.send(null);
} else {
msg('Paused job searching.');
}
last_url=url;
}
function search_job() {
if (friends.length == 0 && redo == "") {
msg('Done searching for jobs.');
$("play").style.display = 'none';
$("pause").style.display = 'none';
createCookie("spockjobs_skip",0);
}
else if (friends.length == 0 && redo == "checked") {
friends=savedfriends;
totalfriends=friends.length;
done=0;
skip=0;
createCookie("spockjobs_skip",skip);
cities=validcities;
log('<span class="money">Starting again from the beginning.</span>');
search_job();
}
else {
function f() {
city = cities[0];
msg('Checking for '+city+' job by '+friends[0]+' (<a href="'+last_url+'">url</a>)');
request(help_url(city));
}
msg('Waiting for ' + wait + ' second'+(wait==1?"":"s")+'...');
setTimeout(f, wait * 1000);
}
}
function log(s) {
$('progress').innerHTML = done+' of '+totalfriends+' &nbsp; <span class="more_in">('+(done/totalfriends*100).toFixed(1)+'%)</span>';
$('exp_gained').innerHTML = exp_gained;
$('friends_helped').innerHTML = (ny>0?"New York: "+ny+"&nbsp;&nbsp;":"")+(cuba>0?"Cuba: "+cuba+"&nbsp;&nbsp;":"")+(moscow>0?"Moscow: "+moscow+"&nbsp;&nbsp;":"")+'Total: '+friends_helped+'';
$('delay').innerHTML = wait;
money_gained='';
if (ny_gained >= 0) { money_gained += '<span class="good">$'+commas(ny_gained)+'</span> '; }
if (cuba_gained >= 0) { money_gained += ' &nbsp; <span class="good">C$'+commas(cuba_gained)+'</span> '; }
if (moscow_gained >= 0) { money_gained += ' &nbsp; <span class="good">R$'+commas(moscow_gained)+'</span> '; }
$('money_gained').innerHTML = money_gained;
var l=$('log');
if (s) {
l.innerHTML=s+'<br />'+l.innerHTML;
}
//$('loot').innerHTML=combinedloot;
}
var Loots=new Array();
function add_loot(s){
var f=-1;
for(var i=0;i<Loots.length&&f==-1;++i) {
if(Loots[i][0]==s) { f=i; }
}
if(f!=-1) { Loots[f][1]++; }
else { Loots[Loots.length]=new Array(s,1); }
var t='';
Loots.sort();
for(var i=0;i<Loots.length;++i) {
t+='<span class="good">'+Loots[i][1]+'x</span> '+Loots[i][0]+'<br />';
}
combinedloot=t;
}
function msg(s) {
$('status').innerHTML=s;
}
function commas(s) {
while (d=/(\d+)(\d{3}.*)/.exec(s)) {
s = d[1] + ',' + d[2];
}
return s;
}
function retry(s) {
if (retries > 5) {
msg(s + ', not retrying any more.');
}
else {
setTimeout(function(){
retries++;
msg(s+'; retry #'+retries+'...');
request(last_url);
},5000);
}
}
function mwlink() {
return '<span class="more_in">[<a href="'+mw_url+friends[0]+'">MW</a>]</span>';
}
function fblink() {
return '<span class="more_in">[<a href="'+fb_url+friends[0]+'">FB</a>]</span>';
}
function fblink2() {
return '<a href="'+fb_url+friends[0]+'">'+friends[0]+'</a>';
}
function joblink() {
return '<span class="more_in">[<a href="'+last_url+'">Job link</a>]</span>';
}
function help_url(c) {
if (c == "New York") return 'http://apps.facebook.com/inthemafia/remote/html_server.php?skip_interstitial=1&xw_controller=job&xw_action=give_help&target_id='+friends[0]+'&job_city=1&ajax=1&skip_interstitial=1';
if (c == "Cuba") return 'http://apps.facebook.com/inthemafia/remote/html_server.php?skip_interstitial=1&xw_controller=job&xw_action=give_help&target_id='+friends[0]+'&job_city=2&ajax=1&skip_interstitial=1';
if (c == "Moscow") return 'http://apps.facebook.com/inthemafia/remote/html_server.php?xw_controller=episode&xw_action=give_help_moscow_social&target_id='+friends[0]+'&job_city=3&ajax=1&skip_interstitial=1';
// if (c == "Boss Moscow") return 'http://apps.facebook.com/inthemafia/index.php?xw_controller=episode&xw_action=give_help_moscow_boss&target_id='+friends[0]+'&job_city=3&skip_interstitial=1&ajax=1';
if (c == "Boss Moscow") return 'http://apps.facebook.com/inthemafia/track.php?&next_controller=index&next_action=ach_celeb&next_params={"sharer":"'+friends[0]+'","aid":"6","skip_interstitial":"1"}&ref=nf
}';
function state_change() {
if (xmlHTTP.readyState == 4) {
if (xmlHTTP.status == 200) {
s='';
m=[];
xp=0;
ca=0;
loot='';
createCookie("spockjobs_skip",parseInt(skip+done));
s = xmlHTTP.responseText;
delete xmlHTTP['onreadystatechange'];
if (m=/app10979261223_user_cash[^>]*>([^<]*)</m.exec(s)) {
uc=$('app10979261223_user_cash_nyc');
if(uc) uc.innerHTML=m[1];
uc=$('app10979261223_user_cash_cuba');
if(uc) uc.innerHTML=m[1];
uc=$('app10979261223_user_cash_moscow');
if(uc) uc.innerHTML=m[1];
}
if (m=/app10979261223_user_health[^>]*>([^<]*)</.exec(s)) {
cur_health = m[1];
$('app10979261223_user_health').innerHTML=cur_health;
}
if (m=/app10979261223_user_max_health[^>]*>([^<]*)</.exec(s)) {
max_health = m[1];
$('app10979261223_user_max_health').innerHTML=max_health;
}
if (m=/app10979261223_user_energy[^>]*>([^<]*)</m.exec(s)) {
cur_en = m[1];
$('app10979261223_user_energy').innerHTML=cur_en;
}
if (m=/app10979261223_user_stamina[^>]*>([^<]*)</m.exec(s)) { $('app10979261223_user_stamina').innerHTML=m[1]; }
if (m=/app10979261223_user_experience[^>]*>([^<]*)</m.exec(s)) {
expnow=m[1];
$('app10979261223_user_experience').innerHTML=expnow;
}
if (m=/app10979261223_exp_for_next_level[^>]*>([^<]*)</m.exec(s)) {
expnext=m[1];
$('app10979261223_exp_for_next_level').innerHTML=expnext;
expneed=parseInt(expnext) - parseInt(expnow);
ratiolvl=eval(expneed/cur_en);
if (Math.abs(ratiolvl)<10) { d=2; }
else { d=0; }
$('app10979261223_user_stats').getElementsByClassName('stat_title')[0].innerHTML='Exp Need: <span class="energy_highlight">'+expneed+'</span><br> &nbsp; (<span class="energy_highlight">'+(ratiolvl).toFixed(d)+'</span> xp/energy)';
}
if (m=/<div[^>]*app10979261223_level_bar[^>]* width:\s*([0-9%]*)/.exec(s)) { $('app10979261223_level_bar').style.width=m[1]; }
if(m=/Special Bonus:.*?You gained ([^<]*).<span/.exec(s)) {
loot+=m[1];
//add_loot(m[1]);
}
if (/You received an? (.*) from/.test(s)) {
loot='';
if (/You received an? (.*) from/.test(s)) {
if (!loot) { loot = ''; }
s.match(/You received an? (.*) from/g).each(function(i) {
loot += i.match(/You received an? (.*) from/)[1].replace('{ITEM}','')+', ';
//add_loot(i.match(/You received an? (.*) from/)[1].replace('{ITEM}',''));
});
loot=loot.slice(0,loot.length-2);
}
//msg(loot+bonus+' ');
log();
}
if(m=/You received (.*?) experience point.*? and (.*?) for helping (.*?) complete the job/.exec(s)) {
friends_helped++;
xp=m[1].replace(/[^0-9]/g,'');
exp_gained+=parseInt(xp);
ca=m[2].replace(/[^0-9]/g,'');
if(/C\$/.test(m[2])) {
sign="C$";
cuba_gained+=parseInt(ca);
cuba++;
}
else if(/R\$/.test(m[2])) {
sign="R$";
moscow_gained+=parseInt(ca);
moscow++;
}
else {
sign='$';
ny_gained+=parseInt(ca);
ny++;
}
log('Helped '+m[3]+' '+(city == 'Boss Moscow'?"with ":"in ")+city+'. Received <span class="good">'+xp+' xp</span> and <strong class="money">'+sign+commas(ca)+'</strong>. '+loot);
}
// else if (/In Mother Russia/.test(s)) {
// if (!moscowonly) {
// moscowonly = true;
// msg('In Moscow, switching to those jobs only...');
// log('Checking New York or Cuba jobs does not work while in Moscow.');
// log('<span class="good">Switching to Moscow jobs only</span>. To fix, travel to Cuba or New York and restart.');
// }
// validcities = ['Moscow','Boss Moscow'];
// cities = ['Moscow','Boss Moscow'];
// }
else if (/You have already helped (.+?) on the/.test(s)) {
if (debug) { console.log('You have already helped '+joblink()); }
log();
}
else if (/You are too late/.test(s)) {
if (debug) { console.log('You are too late '+joblink()); }
log();
}
else if (/you can only help 25 friends per day per city/.test(s)) {
validcities.splice(validcities.indexOf(city),1);
log('<span class="bad">'+city+' limit reached</span>, not checking for more jobs there. '+joblink());
}
else if (/Stay tuned for more Moscow action/.test(s)) {
if (debug) { console.log('Moscow job page randomly loaded. '+joblink()); }
done++;
friends=friends.slice(1);
}
else if (/Boss Fight/.test(s)) {
if (debug) { console.log('Boss fight page loaded for some reason, skipping friend. '+joblink()); }
done++;
friends=friends.slice(1);
}
else if (/Here on the island they only use Cuban/.test(s)) {
if (debug) { console.log('Cuba job page loaded for some reason, skipping friend. '+joblink()); }
done++;
friends=friends.slice(1);
}
else if (/You cannot help people who are not your friends/.test(s)) {
log('Not a Facebook friend or not a MW player. '+joblink());
done++;
friends=friends.slice(1);
}
else if (/You are too late to help on this job, but Bill thanks you for your offer with $1000./.test(s)) {
log('$1000. '+joblink());
done++;
friends=friends.slice(1);
}
else if (/You need to be friends with (.+?) to provide help/.test(s)) {
log('Not a Facebook friend or not a MW player. '+joblink());
friends=friends.slice(1);
done++;
}
else if (/Error while loading page from/.test(s)) {
if (debug) { console.log('Kinks error: '+last_url); }
msg('<span class="bad">[Kinks error]</span> Mafia Wars overloaded, waiting 30 seconds... (<a href="'+last_url+'">url</a>)');
setTimeout(function(){ request(last_url); },30000);
return;
}
else if (/<h2 class="main_message">500<\/h2>/.test(s)) {
if (debug) { console.log('Error 500: '+last_url); }
msg('<span class="bad">[Error 500]</span> Mafia Wars overloaded, waiting 30 seconds... (<a href="'+last_url+'">url</a>)');
setTimeout(function(){ request(last_url); },30000);
return;
}
else if (/404 Error/.test(s)) {
if (debug) { console.log('404 response when helping '+friends[0]+' in '+city+'. '+joblink()); }
retry('404 response when helping '+friends[0]+' in '+city+'. (<a href="'+last_url+'">url</a>)');
//done++;
//friends=friends.slice(1);
return;
}
else {
if (debug) { console.log('Unknown response when helping '+friends[0]+' in '+city+'. '+joblink()); }
retry('Unknown response when helping '+friends[0]+' in '+city+'. (<a href="'+last_url+'">url</a>)');
//done++;
//friends=friends.slice(1);
return;
}
retries = 0;
if (cities.length > 1) {
cities=cities.slice(1);
}
else {
cities=validcities;
done++;
friends=friends.slice(1);
}
log();
search_job();
}
else {
retry('Could not load page properly (<a href="'+last_url+'">url</a>)');
if (debug) { log('Could not load page properly (<a href="'+last_url+'">url</a>)'); }
return;
}
}
}

// createCookie from Vern's Toolkit http://vern.com/mwtools/
function createCookie(name,value) {
// expire one month from now
var expires = new Date();
expires.setDate(expires.getDate()+30);
document.cookie = name+"="+value+";expires="+expires.toGMTString()+"; path=/";
}

// readCookie from Vern's Toolkit http://vern.com/mwtools/
function readCookie(name) {
var i,
cookie,
nameEQ = name+"=",
cookieArray = document.cookie.split(";");
for (i=0; i< cookieArray.length; i++) {
cookie = cookieArray[i];
while (cookie.charAt(0)==' ')
cookie = cookie.substring(1,cookie.length);
if (cookie.indexOf(nameEQ) == 0)
return cookie.substring(nameEQ.length,cookie.length);
}
return null;
}

xmlHTTP = get_xmlHTTP();
if (!xmlHTTP) {
alert('Your browser does not support XMLHTTP.');
return;
}
}())

Back to list