Saturday, January 22, 2011

Adds MafiaWars specific links to the standard Facebook profile menu.


// ==UserScript==
// @name           Add MafiaWars Links to Facebook Profiles
// @copyright      2010 - Julio Napuri
// @version        0.1
// @description    Adds MafiaWars specific links to the standard Facebook profile menu.
// @license        GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @include        http://*.facebook.com/*
// @exclude        http://apps.facebook.com/*
// @exclude        http://www.facebook.com/*viewas*
// ==/UserScript==

// https://github.com/julionc/mafiawars
const DEBUG_MODE = false;

// GLOBAL        
var global = {
    USER_ID: 0,
    USER_ID_64: 0,
 Mafia: {
  Profile: 'http://apps.facebook.com/inthemafia/profile.php?id=%7B%22user%22%3A%22USER_ID%22%7D',
  Gift: 'http://apps.facebook.com/inthemafia/track.php?next_controller=gift&next_action=view&next_params=%7B%22target_id%22%3A%22USER_ID%22%7D',
  Promote: 'http://apps.facebook.com/inthemafia/track.php?next_controller=group&next_action=view&next_params={"promote":"yes","pid":"p|USER_ID"}',
  Add: "http://apps.facebook.com/inthemafia/status_invite.php?from=USER_ID",
  //Add: "http://apps.facebook.com/inthemafia/track.php?next_controller=recruit&next_action=view&zy_track=feed&sendkey=SEND_KEY&next_params={"f_id":"USER_ID","from":"w2w"}&sendtime=SEND_TIME&friend=p|USER_PID&feed_target=USER_FRIEND_ID&ref=nf"
 },
    /**
    * Base64 encode / decode
    * http://www.webtoolkit.info/
    */ 
    Base64: {
        // private property
        _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
        
        /**
         * Public method for encode
         * @param {Object} input
         */
        encode: function(input) {
            var output = "";
            var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
            var i = 0;
            
            input = this._utf8_encode(input);
            
            while (i < input.length) {
            
                chr1 = input.charCodeAt(i++);
                chr2 = input.charCodeAt(i++);
                chr3 = input.charCodeAt(i++);
                
                enc1 = chr1 >> 2;
                enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                enc4 = chr3 & 63;
                
                if (isNaN(chr2)) {
                    enc3 = enc4 = 64;
                }
                else if (isNaN(chr3)) {
                    enc4 = 64;
                }
                
                output = output +
                this._keyStr.charAt(enc1) +
                this._keyStr.charAt(enc2) +
                this._keyStr.charAt(enc3) +
                this._keyStr.charAt(enc4);
                
            }
            return output;
        },
        /**
         * Public method for decode
         * @param {String} input
         */
        decode: function(input) {
            var output = "";
            var chr1, chr2, chr3;
            var enc1, enc2, enc3, enc4;
            var i = 0;
            
            input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
            
            while (i < input.length) {
            
                enc1 = this._keyStr.indexOf(input.charAt(i++));
                enc2 = this._keyStr.indexOf(input.charAt(i++));
                enc3 = this._keyStr.indexOf(input.charAt(i++));
                enc4 = this._keyStr.indexOf(input.charAt(i++));
                
                chr1 = (enc1 << 2) | (enc2 >> 4);
                chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                chr3 = ((enc3 & 3) << 6) | enc4;
                
                output = output + String.fromCharCode(chr1);
                
                if (enc3 != 64) {
                    output = output + String.fromCharCode(chr2);
                }
                if (enc4 != 64) {
                    output = output + String.fromCharCode(chr3);
                }
                
            }
            output = this._utf8_decode(output);
            return output;
        },
        /**
         * private method for UTF-8 encoding
         * @param {String} string
         * @return {String}
         */
        _utf8_encode: function(string) {
            string = string.replace(/\r\n/g, "\n");
            var utftext = "";
            
            for (var n = 0; n < string.length; n++) {
            
                var c = string.charCodeAt(n);
                
                if (c < 128) {
                    utftext += String.fromCharCode(c);
                }
                else if ((c > 127) && (c < 2048)) {
                    utftext += String.fromCharCode((c >> 6) | 192);
                    utftext += String.fromCharCode((c & 63) | 128);
                }
                else {
                    utftext += String.fromCharCode((c >> 12) | 224);
                    utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                    utftext += String.fromCharCode((c & 63) | 128);
                }
            }
            return utftext;
        },
        /**
         * private method for UTF-8 decoding
         * @param {String} utftext
         * @return {String}
         */
        _utf8_decode: function(utftext) {
            var string = "";
            var i = 0;
            var c = c1 = c2 = 0;
            
            while (i < utftext.length) {
            
                c = utftext.charCodeAt(i);
                
                if (c < 128) {
                    string += String.fromCharCode(c);
                    i++;
                }
                else if ((c > 191) && (c < 224)) {
                    c2 = utftext.charCodeAt(i + 1);
                    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                    i += 2;
                }
                else {
                    c2 = utftext.charCodeAt(i + 1);
                    c3 = utftext.charCodeAt(i + 2);
                    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                    i += 3;
                }
            }
            return string;
        }
    }
};

// RESOURCES
var resources = {
 mafia_icon : 'data:image/gif;base64,' +
 'R0lGODlhEAAQAPUAAAAAAAYGBgkJCRERERcXFxsbGx4eHiQkJCoqKi4uLjIyMjQ0NDk5OT4+PkBAQEdHR0pKSlBQUFdXV15eXmJiYmZmZmpqanNzc3R0dH9/f4CAgIWFhYuLi42NjZGRkZeXl5ubm56enqampqqqqqysrLa2trq6ur+/v8HBwcXFxcjIyM/Pz9PT09fX19ra2t/f3+Pj4+Xl5evr6/Hx8fT09Pn5+f39/QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAAAAAAALAAAAAAQABAAAAbQQIOjsVg4DAAAwcFYMByHgCM0In2QSs5oBIIIAAfSaoVaJBUklWqUSIZbLNclWRmvSoekoQR7xUwFAyIvLi4nCHonMjGLEhAtMTAxKmYABik0MzU1IBuaMzMtDW4tNjUnNS4qNCo1NjIPSQguphYsNjYuGDKvDkkLMKYZJbgmFzOvowAOMcIduB4XNDYxygzNNhcSMzQVFdOwSQm0NhYHKy4KFK4yELKl5QIiJQIT0zG+AAzBNhgAFv4sTKMRAUADDB88fODQgEHDDSEianAQBAA7'
}

// =========================================================
// CONSOLE LOG (DEBUG)
// =========================================================
/**
 * Send text to window console.
 * @autor David Cabrera - FB MafiaWars Addon
 * @param {String} msg
 * @see http://userscripts.org/scripts/show/90615 
 */
function log$(msg, force) {
    if (DEBUG_MODE === false && !force)
        return;
    try {
        (console||unsafeWindow.console).log('FBMW ADDON DEBUG: ' + msg);
    } 
    catch (e) {
        // EMPTY
    }
}

function getFileName(url,extension){
 var f = /([^\/\\]+)$/.exec(url) || ['',''];
 f = /^([^\.]+)(\.\w+)?/.exec(f[1]);
 if(extension) f[1]+= (f[2] || '');
 return f[1];
}

function addItemMWList(options) {
 var item = {
  url: {
   profile: global.Mafia.Profile.replace("USER_ID", global.USER_ID),
   gift: global.Mafia.Gift.replace("USER_ID", global.USER_ID),
   promote: global.Mafia.Promote.replace("USER_ID", global.USER_ID),
   add: global.Mafia.Add.replace("USER_ID", global.USER_ID),
  },
  icon: {
   profile: resources.mafia_icon,
   gift: resources.mafia_icon,
   promote: resources.mafia_icon,
   add: resources.mafia_icon
  }
    }
 var _nav = document.createElement("li");
 _nav.setAttribute('id', 'navItemM_' + options.className);
 _nav.setAttribute('class', 'key-mafia-' + options.className);
 _nav.innerHTML = "MAFIA";
 _nav.innerHTML = _nav.innerHTML.replace("MAFIA",
  "<a class='item' href='" + item.url[options.className]  + "' target='_blank'>"+
  "<span class='imgWrap'>"+
  "<i class='img sp_1a0aga' style='background-image: url(" + item.icon[options.icon] + ");background-repeat: no-repeat;display: inline-block;height: 16px;width: 16px;'></i></span>"+
  "</span><span class='linkWrap'>" + options.title +"</span></a>"
 );
 var _list = document.getElementById("mafia_list");
 _list.appendChild(_nav);
}

// Main
function addMafiaProfile(){
 if(!document.getElementById("FBUserID")) {
  var _profile_pic = document.getElementById("profile_pic");
  if (_profile_pic) {
   log$('Facebook Profile detected.');
   var _image_name = getFileName(_profile_pic.getAttribute("src"));
   global.USER_ID = _image_name.split("_")[1];
   log$("Get User ID from Facebook : " + global.USER_ID);
   
   global.USER_ID_64 = global.Base64.encode(global.USER_ID);
   log$("Set User ID Base64 : " + global.USER_ID_64);
   
   var sideNav = document.getElementById("sideNav");
   var sideMafia = document.createElement("div");
   sideMafia.setAttribute("id", "FBUserID");
   sideMafia.setAttribute("class", "expandableSideNav");
   sideMafia.innerHTML = "<ul id='mafia_list' class='uiSideNav'></ul>";
   sideNav.appendChild(sideMafia);  
   // Items
   addItemMWList({icon: 'profile', className: 'profile', title: 'Mafia Profile'});
   addItemMWList({icon: 'gift', className: 'gift', title: 'Send Gift'});
   //addItemMWList({icon: 'promote', className: 'promote', title: 'Promote'});
   //addItemMWList({icon: 'add', className: 'add', title: 'Add To Mafia'});
  }
 }
    return false;
};

addMafiaProfile();
window.setInterval(function (){addMafiaProfile()}, 5000);

0 comments:

Post a Comment