InteLStaT / SteamCommunity more menu items

// ==UserScript==
// @name         SteamCommunity more menu items
// @namespace    intelstat
// @version      2
// @description  Adds more menu items to the "More" drop-down list on all Steam profile pages
// @author       InteLStaT
// @license      MIT
// @updateURL    https://openuserjs.org/meta/InteLStaT/SteamCommunity_more_menu_items.meta.js
// @include      /^http(s)*\://steamcommunity\.com/(profiles|id)/[a-zA-Z0-9]+(/)*$/
// @run-at       document-idle
// @require      http://peterolson.github.com/BigInteger.js/BigInteger.min.js
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
// ==/UserScript==

/* === update notes ===
 * added Rep.tf, Backpack.tf, TF2 Outpost, Bazaar.tf pages
 * added an icon for the Show SteamIDs menu item
 * UPCOMING: similar menu items grouped
 */

"use strict";

var itemCt = 0;
var curSteamId64str = g_rgProfileData.steamid;

/*
 * Adds a menu item which opens a website
 */
function addMenuItemUrl(id, text, icon, url, target){
    if(url === undefined) {
        url = "https://steamcommunity.com";
    }

    var openSiteFunc = function() {
        window.open(url, target);
    };

    addMenuItemFunction(id, text, icon, openSiteFunc);
}

/*
 * Adds a menu item which calls a JS function
 */
function addMenuItemFunction(id, text, icon, func) {
    if(id === undefined) {
        id = "-ilst-custom-item-" + itemCt;
    }
    if(text === undefined) {
        text = "New menu item";
    }
    if(func === undefined){
        func = function() {
            alert("This Menu Item Does Nothing™");
        };
    }

    itemCt++;

    var iconelem = icon === undefined ? "" : "<img src='" + icon + "' style='max-width:16px;max-height:16px' />&nbsp; ";

    $("#profile_action_dropdown").find(".popup_body").append("<a id='" + id + "' class='popup_menu_item'>" + iconelem + text + "</a>");
    $("#"+id).on("click", func);
    $("#"+id).attr("onclick", "HideMenu( 'profile_action_dropdown_link', 'profile_action_dropdown' ); return false;");

    console.log("ilst: menu item number " + itemCt + " with id '" + id + "' added.");
}

/* Takes SteamID64 as string, gives SteamID32 */
function toSteamId32(steamId64str) {
    var steamId64 = bigInt(steamId64str);
    var Y = steamId64.and(1);
    var Z = steamId64.minus(bigInt("76561197960265728")).minus(Y).over(2);
    return "STEAM_0:" + Y + ":" + Z;
}

/* Takes SteamID64 as string, gives SteamID3 */
function toSteamId3(steamId64str) {
    var steamId64 = bigInt(steamId64str);
    var steamId3 = steamId64.minus(bigInt("76561197960265728"));
    return "[U:1:" + steamId3 + "]";
}


$(document).ready(function() {
    if($(".actual_persona_name").length) {
        if(!$("#profile_action_dropdown").length){
            $('head').children().last().after("<style>.shadow_content{margin: 12px;}.shadow_ul, .shadow_ur, .shadow_bl, .shadow_br{position: absolute;height: 12px;width: 12px;background-image: url( http://community.edgecast.steamstatic.com/public/images/header/shadow_corners.png );background-image: url( data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAVRJREFUeNq0VlGyAiEMo8Dq/a+rCzj66E7IS/FLZjqgQtq0oWjpb1hah4k17xk08/rz2TbgOPs6z7kD4Ng4TFWAMzCb/zbIFLvFQQSYaXaATvNQ6aoBOIKyeYrcjJwtTAocQkBflxmE2zG/Y0ZcP2diRUTuVsHBAVYEuIk6hDXIAHIQA99/TvMzz3nGozesgWJQKC0HOEuCxSDZuhNZZOXgBvN7PAh8QJE7YI1K2s6ChUd/Bwcs17c1OOtqsgo3lBkgi9u0O+ncgdvc3win5fTjoe6BumyYljZr8JjqeYKqGl3CD32UVaecnoFaGJyBL1XVQAlNNDhsBecm+qUvlaA9q6vv1gQ4s7j2F9GGo+s/KHVoXTBIzCCJlCTRnhtEjNbVG1EEMDNR9WFgfhuWdm0bGXOPYZX1IPKLgW1StHsyQ8BvNYgYMJseAP979O3Xf1teAgwAvqvFROfF+RQAAAAASUVORK5CYII=);}.shadow_ul{top: 0;left: 0;background-position: top left;}.shadow_ur{top: 0;right: 0;background-position: top right;}.shadow_bl{bottom: 0;left: 0;background-position: bottom left;}.shadow_br{bottom: 0;right: 0;background-position: bottom right;}.shadow_top, .shadow_bottom{position: absolute;margin: 0 12px;height: 12px;left: 0;right: 0;background-image: url( http://community.edgecast.steamstatic.com/public/images/header/shadow_topbottom.png );background-image: url( data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAaCAYAAAB2BDbRAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAD9JREFUeNo0zFEKQFAQAMB9u0hSEvc/IUk5gEb5mc+JiGj5kyh0GDBiwowFKzbsCBw4ceHGUwL0KCQa4uMVYAAMZQVwOy2LWAAAAABJRU5ErkJggg==);background-repeat: repeat-x;}.shadow_top{background-position: top;top: 0;}.shadow_bottom{background-position: bottom;bottom: 0;}.shadow_left, .shadow_right{position: absolute;top: 0;bottom: 0;margin: 12px 0;width: 12px;background-image: url( http://community.edgecast.steamstatic.com/public/images/header/shadow_leftright.png );background-image: url( data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAABCAYAAADAW76WAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAD5JREFUeNosyssKQFAUAMBzkSQl8f9fKFIsyGuUxewmRUT6ZeQUlFTUNLR09Aw8jEzMLKxs7BycXNzffwUYAFWiDQPUjcVGAAAAAElFTkSuQmCC );background-repeat: repeat-y;}.shadow_left{background-position: left;left: 0;}.shadow_right{background-position: right;right: 0;}.suppress_shadow .shadow_ul,.suppress_shadow .shadow_ur, .suppress_shadow .shadow_bl, .suppress_shadow .shadow_br,.suppress_shadow .shadow_top, .suppress_shadow .shadow_bottom, .suppress_shadow .shadow_left, .suppress_shadow .shadow_right{background-image: none;}.popup_block{z-index: 200;position: absolute;}.popup_block .popup_body{position: relative;background-color: #171a21;}</style>");
            $(".btn_profile_action.btn_medium").after("&nbsp;<span class=\"btn_profile_action btn_medium\" id=\"profile_action_dropdown_link\" onclick=\"ShowMenu( this, 'profile_action_dropdown', 'right' );\"><span>More <img src=\"https://steamcommunity-a.akamaihd.net/public/images/profile/profile_action_dropdown.png\"></span></span><div class=\"popup_block\" id=\"profile_action_dropdown\" style=\"display: none;\"> <div class=\"shadow_ul\"/> <div class=\"shadow_top\"/> <div class=\"shadow_ur\"/> <div class=\"shadow_left\"/> <div class=\"shadow_right\"/> <div class=\"shadow_bl\"/> <div class=\"shadow_bottom\"/> <div class=\"shadow_br\"/> <div class=\"popup_body popup_menu shadow_content\"> </div></div>");
            $("#profile_action_dropdown_link").attr("onclick", "ShowMenu( this, 'profile_action_dropdown', 'right' ); $('#profile_action_dropdown').css(\"top\", \"168px\");");
        }

        // SteamRep page
        addMenuItemUrl("-ilst-steamrep-page-item",
                       "SteamRep Page",
                       "https://www.google.com/s2/favicons?domain=steamrep.com",
                       "https://steamrep.com/search?q=" + curSteamId64str,
                       "_blank");

        // Rep.tf page
        addMenuItemUrl("-ilst-reptf-page-item",
                       "Rep.tf page",
                       "https://www.google.com/s2/favicons?domain=rep.tf",
                       "https://rep.tf/" + curSteamId64str,
                       "_blank");

        // Backpack.tf page
        addMenuItemUrl("-ilst-backpacktf-page-item",
                       "Backpack.tf page",
                       "https://www.google.com/s2/favicons?domain=backpack.tf",
                       "https://backpack.tf/u/" + curSteamId64str,
                       "_blank");

        // TF2 Outpost page
        addMenuItemUrl("-ilst-tf2outpost-page-item",
                       "TF2 Outpost page",
                       "https://www.google.com/s2/favicons?domain=tf2outpost.com",
                       "https://tf2outpost.com/user/" + curSteamId64str,
                       "_blank");

        // Bazaar.tf page
        addMenuItemUrl("-ilst-bazaartf-page-item",
                       "Bazaar.tf page",
                       "https://www.google.com/s2/favicons?domain=bazaar.tf",
                       "https://bazaar.tf/profiles/" + curSteamId64str,
                       "_blank");

        // SteamIDs
        addMenuItemFunction("-ilst-show-steamids-item",
                            "Show SteamIDs",
                            "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAAAz0lEQVRIid3WYQ2DMBAF4CcBCUh4ec8AEpAwCZOAg0pBAhIqYRKQwH6sTcjCFrLQBkZy4U97X9pe2gMAkGwkBUmz7eWIkDRLCiQbZMR2PArYiEiygaRQEMmrCzhyu75tI0ojOc4DSXrYntJ/a8zkHcW0BxoAQNKwhaTK7f4CirbHklAk2WL1FYFIdgBgu7fdF4EkzSlxW/qM3hNfH4opMUtDS644SXeSLFneY37MSFLSbbXC36EP+OzXHbd7znlu7+tB1Z7yas1JtXarVgP5BH9fzywZxfeOAAAAAElFTkSuQmCC",
                            function() {
            $.get(window.location.href + "?xml=1", function(xml){
                var customUrl = $("customURL", xml);
                ShowAlertDialog("SteamIDs of " + g_rgProfileData.personaname,
                                "<p>SteamID32: " + toSteamId32(curSteamId64str) + "<br>"
                                + "SteamID64: " + curSteamId64str + "<br>"
                                + "Steam3ID: " + toSteamId3(curSteamId64str) + "<br>"
                                + "Custom URL: " + customUrl.text()
                                + "</p>");
            });
        });

        // Profile page in the Steam client
        addMenuItemUrl("-ilst-open-client-item",
                       "Open Profile in Steam Client",
                       "https://www.google.com/s2/favicons?domain=steamcommunity.com",
                       "steam://url/SteamIDPage/"+curSteamId64str,
                       "_self");
    }
});