Advertisement
Guest User

Untitled

a guest
Jan 31st, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Baldy Beaver
  3. // @namespace   roc.baldy.beaver
  4. // @description SPLoP API, aka splopi but better known as Baldy Beaver (Name was the collective effort of Sux0r and Whishy)
  5. // @include     https://ruinsofchaos.com/*
  6. // @exclude     https://ruinsofchaos.com/index.php*
  7. // @exclude     https://ruinsofchaos.com/register.php*
  8. // @exclude     https://ruinsofchaos.com/forgotpass.php*
  9. // @version     1.02
  10. // @grant         GM_xmlhttpRequest
  11. // @grant         GM_setValue
  12. // @grant         GM_getValue
  13. // @grant         GM_deleteValue
  14. // @grant         GM_openInTab
  15. // @grant         GM_addStyle
  16. // @grant         GM_log
  17. // @require     https://code.jquery.com/jquery-2.2.4.min.js
  18. // ==/UserScript==
  19.  
  20. (function () {
  21.     var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  22.     if (isChrome) {
  23.         this.GM_getValue = function (key, def) {
  24.             return localStorage[key] || def;
  25.         };
  26.         this.GM_setValue = function (key, value) {
  27.             return localStorage[key] = value;
  28.         };
  29.         this.GM_deleteValue = function (key) {
  30.             return delete localStorage[key];
  31.         };
  32.     }
  33.  
  34.     if (DetectRunningInstance() == true) {
  35.         alert("You are running multiple instances of the BB script!\nManually remove the duplicates from Greasemonkey menu.");
  36.     }
  37.  
  38.     if (document.body.innerHTML.indexOf('href="logout.php"') < 0 && document.body.innerHTML.indexOf('Rank') < 0 && document.body.innerHTML.indexOf('Turns') < 0) {
  39.         //User not logged in. No need to run the script.
  40.         return;
  41.     } else {
  42.         addMenuPages();
  43.     }
  44.  
  45.     var bbScriptServer = "http://52.10.254.235:8080";
  46.     //var bbScriptServer = "http://127.0.0.1:8080";
  47.     var scriptName = "Baldy Beaver";
  48.     var url = document.location.toString();
  49.     var BB_version = 1;
  50.     var BB_server = "NA";
  51.  
  52.     var BB_username = GM_getValue("BB_username", "");
  53.     var BB_password = GM_getValue("BB_password", "");
  54.     var BB_statid = GM_getValue("BB_statid", "");
  55.     var loggedIn = (GM_getValue("BB_SESSION_TYPE", "") === "") ? "NODATA" : GM_getValue("BB_SESSION_TYPE", "");
  56.  
  57.     if (BB_statid === "") {
  58.         var userIdTok = document.getElementById("s_username").href.match(/stats\.php\?id=([0-9]*)/);
  59.         BB_statid = userIdTok[1]
  60.             GM_setValue("BB_statid", BB_statid);
  61.     }
  62.  
  63.     if (BB_username === "") {
  64.         BB_username = document.getElementById("s_username").innerHTML;
  65.         BB_username = BB_username.trim();
  66.         GM_setValue("BB_username", BB_username);
  67.     }
  68.  
  69.     //Check if user is logged in before loading the scripts
  70.     loadBBPage()
  71.  
  72.     function addMenuPages() {
  73.         var bbMenu = $("<a class=\"bbMenu\" alt=\"Baldy Beaver\" href=\"base.php?bbpage=profile\"><span>VERY UGLY BUTTON</span></a>");
  74.         var intelMenu = $("#menubar .menu7");
  75.         intelMenu.after(bbMenu);
  76.         bbMenu.css("display", "block");
  77.         bbMenu.css("height", "20px");
  78.         bbMenu.css("background-color", "#432D12");
  79.         bbMenu.css("padding", "4px 0 0 0");
  80.         bbMenu.css("color", "#C3BC9F");
  81.         bbMenu.css("font-weight", "bold");
  82.     }
  83.  
  84.     function loadBBPage() {
  85.         if (url.indexOf("/base.php?bbpage=profile") > 0) {
  86.             attemptLogin(bbBasePage);
  87.         } else if (url.indexOf("/inteldetail.php") > 0) {
  88.             inteldetail();
  89.         } else if (url.indexOf("/stats.php") > 0) {
  90.             stats();
  91.         }
  92.     }
  93.  
  94.     function bbBasePage() {
  95.         var lolcontent = document.getElementById("lolcontent");
  96.         lolcontent.innerHTML = "GETTING DATA, please hold on. Baldy script is balding stuff...";
  97.         if (loggedIn === "NODATA") {
  98.             var pageBody = "<div class=\"th topcap\">" + scriptName + "</div>";
  99.             pageBody += "<table id=\"profileInfo\" class=\"sep\" width=\"100%\" cellspacing=\"0\" border=\"0\">";
  100.             pageBody += "<tr class=\"odd\"><td colspan=\"3\">Please log in to continue. If you are not register, just chose a new password, and enter a random word for the challenge. Then message the challenge word in game to either Sux0r or Jellybean to be approved</td></tr>";
  101.             pageBody += "<tr class=\"odd\"><td colspan=\"3\">You do not need to enter the challenge if you are already registered</td></tr>";
  102.             pageBody += "<tr class=\"even\"><td><input type=\"text\" placeholder=\"Password\" id=\"inputPassword\" value=\"" + BB_password + "\" /></td><td><input type=\"text\" placeholder=\"Challenge\" id=\"inputChallenge\"></td><td><button id=\"doLoginButton\">Login / Register</button></td></tr>";
  103.             pageBody += "</table>";
  104.             lolcontent.innerHTML = pageBody;
  105.             var btn = document.getElementById("doLoginButton");
  106.             btn.addEventListener('click', doLogin);
  107.         } else if (loggedIn === "WRONGPASS") {
  108.             var pageBody = "<div class=\"th topcap\">" + scriptName + "</div>";
  109.             pageBody += "<table id=\"profileInfo\" class=\"sep\" width=\"100%\" cellspacing=\"0\" border=\"0\">";
  110.             pageBody += "<tr class=\"odd\"><td colspan=\"3\">You either forgot your password or have fat fingers, you decide. Try again.</td></tr>";
  111.             pageBody += "<tr class=\"even\"><td><input type=\"text\" placeholder=\"Password\" id=\"inputPassword\" value=\"" + BB_password + "\" /></td><td><input type=\"text\" placeholder=\"Challenge\" id=\"inputChallenge\"></td><td><button id=\"doLoginButton\">Login / Register</button></td></tr>";
  112.             pageBody += "</table>";
  113.             lolcontent.innerHTML = pageBody;
  114.             var btn = document.getElementById("doLoginButton");
  115.             btn.addEventListener('click', doLogin);
  116.         } else if (loggedIn === "LOGGEDIN") {
  117.             getProfilePage();
  118.         }
  119.     }
  120.  
  121.     function getProfilePage() {
  122.         GM_xmlhttpRequest({
  123.             method: "POST",
  124.             headers: {
  125.                 'Content-type': 'application/x-www-form-urlencoded'
  126.             },
  127.             data: encodeURI("password=" + BB_password + "&external_id=" + BB_statid),
  128.             url: bbScriptServer + "/roc/getprofile",
  129.             onload: function (r) {
  130.                 if (r.status == 200) {
  131.                     var rspObj = JSON.parse(r.responseText);
  132.                     loadUserMessage(rspObj);
  133.                     if (rspObj.TypeID === 4) {
  134.                         loadNonAdminUsers(rspObj.UserEntries);
  135.                     }
  136.                 }
  137.             }
  138.         });
  139.     }
  140.  
  141.     function doLogin() {
  142.         var inputPassword = document.getElementById("inputPassword").value;
  143.         var inputChallenge = document.getElementById("inputChallenge").value;
  144.  
  145.         GM_xmlhttpRequest({
  146.             method: "POST",
  147.             headers: {
  148.                 'Content-type': 'application/x-www-form-urlencoded'
  149.             },
  150.             data: encodeURI("username=" + BB_username + "&password=" + inputPassword + "&external_id=" + BB_statid + "&challenge=" + inputChallenge),
  151.             url: bbScriptServer + "/roc/sign",
  152.             onload: function (r) {
  153.                 if (r.status == 200) {
  154.                     GM_setValue("BB_SESSION_TYPE", r.responseText);
  155.                     if (r.responseText === "LOGGEDIN") {
  156.                         GM_setValue("BB_password", inputPassword);
  157.                         BB_password = GM_getValue("BB_password", "");
  158.                         getProfilePage();
  159.                     }
  160.                 }
  161.             }
  162.         });
  163.  
  164.     }
  165.  
  166.     function attemptLogin(callBack) {
  167.         //GM_log("Attempting to log in");
  168.         GM_xmlhttpRequest({
  169.             method: "POST",
  170.             headers: {
  171.                 'Content-type': 'application/x-www-form-urlencoded'
  172.             },
  173.             data: encodeURI("username=" + BB_username + "&password=" + BB_password + "&external_id=" + BB_statid + "&challenge"),
  174.             url: bbScriptServer + "/roc/sign",
  175.             onload: function (r) {
  176.                 if (r.status == 200) {
  177.                     loggedIn = r.responseText;
  178.                     callBack();
  179.                 }
  180.             }
  181.         });
  182.     }
  183.  
  184.     function changeStatus(span, id, newStatus) {
  185.         GM_xmlhttpRequest({
  186.             method: "POST",
  187.             headers: {
  188.                 'Content-type': 'application/x-www-form-urlencoded'
  189.             },
  190.             data: encodeURI("external_id=" + BB_statid + "&user_id=" + id + "&type_id=" + newStatus),
  191.             url: bbScriptServer + "/roc/changetypeid",
  192.             onload: function (r) {
  193.                 if (r.status == 200) {
  194.                     console.log(r.status, r.responseText);
  195.                     span.innerHTML = "[STATUS CHANGED - RELOAD PAGE]";
  196.                     span.setAttribute("style", "color:#FFFFFF ; font-weight : bold; font-size : 14px ; cursor : default;");
  197.                 } else {
  198.                     document.getElementById("lolcontent").innerHTML = "STOP TRYING TO BREAK MY SCRIPT!!!, or might need to login again. I can't really tell";
  199.                 }
  200.             }
  201.         });
  202.     }
  203.  
  204.     function DetectRunningInstance() {
  205.         if (document.getElementById('InstanceBB')) {
  206.             return true;
  207.         }
  208.  
  209.         var instanceDiv = document.createElement('div');
  210.         instanceDiv.style.display = 'none';
  211.         instanceDiv.setAttribute('id', "InstanceBB");
  212.         document.body.appendChild(instanceDiv);
  213.  
  214.         return false;
  215.     }
  216.  
  217.     function loadUserMessage(obj) {
  218.         //Loads the message from the server about the user type
  219.         var lolcontent = document.getElementById("lolcontent");
  220.         var loginStatus = (obj.Logged === true) ? "<span style=\"color : green\">Logged in</span>" : "<span style=\"color : red\">Not Logged</span><span style=\"font-size : 8px;font-weight:bold\">(Please contact bold_ally)</span>";
  221.         var pageBody = "<div class=\"th topcap\">" + scriptName + "</div>";
  222.         pageBody += "<table id=\"profileInfo\" class=\"sep\" width=\"100%\" cellspacing=\"0\" border=\"0\">";
  223.         pageBody += "<tr class=\"odd\"><td width=\"50%\">" + loginStatus + "</td><td width=\"50%\">User status : " + obj.Type + "</td></tr>";
  224.         pageBody += "</table>";
  225.         lolcontent.innerHTML = pageBody;
  226.     }
  227.  
  228.     function loadNonAdminUsers(userArray) {
  229.         var lolcontent = document.getElementById("lolcontent");
  230.         var approvedTable = document.createElement("table");
  231.         approvedTable.setAttribute("width", "100%");
  232.         approvedTable.setAttribute("cellspacing", "0");
  233.         approvedTable.setAttribute("border", "0");
  234.         var approvedTr = document.createElement("tr");
  235.         var approvedTd = document.createElement("td");
  236.         approvedTd.innerHTML = "APPROVED LIST - Use button to remove approval";
  237.         approvedTd.setAttribute("colspan", "4");
  238.         approvedTd.setAttribute("class", "th topcap");
  239.         approvedTr.appendChild(approvedTd);
  240.         approvedTable.appendChild(approvedTr);
  241.  
  242.         var pendingTable = document.createElement("table");
  243.         pendingTable.setAttribute("width", "100%");
  244.         pendingTable.setAttribute("cellspacing", "0");
  245.         pendingTable.setAttribute("border", "0");
  246.         var pendingTr = document.createElement("tr");
  247.         var pendingTd = document.createElement("td");
  248.         pendingTd.innerHTML = "PENDING LIST - Use button to approve - Don't forget to check challenge first";
  249.         pendingTd.setAttribute("colspan", "4");
  250.         pendingTd.setAttribute("class", "th topcap");
  251.         pendingTr.appendChild(pendingTd);
  252.         pendingTable.appendChild(pendingTr);
  253.  
  254.         var approvedCounter = 0;
  255.         var pendingCounter = 0;
  256.  
  257.         for (var index = 0; index < userArray.length; index++) {
  258.             userObj = userArray[index];
  259.             var tr = document.createElement("tr");
  260.             var span = document.createElement("span");
  261.             var tdID = document.createElement("td");
  262.             tdID.innerHTML = userObj.ExternalID;
  263.             tdID.setAttribute("width", "25%");
  264.             var tdName = document.createElement("td");
  265.             tdName.innerHTML = userObj.Username;
  266.             tdName.setAttribute("width", "25%");
  267.             var tdChallenge = document.createElement("td");
  268.             tdChallenge.innerHTML = userObj.Challenge;
  269.             tdChallenge.setAttribute("width", "25%");
  270.             var tdChange = document.createElement("td");
  271.             tdChange.appendChild(span);
  272.             tdChange.setAttribute("width", "25%");
  273.  
  274.             tr.appendChild(tdID);
  275.             tr.appendChild(tdName);
  276.             tr.appendChild(tdChallenge);
  277.             tr.appendChild(tdChange);
  278.  
  279.             if (userObj.TypeID === 2) {
  280.                 span.innerHTML = "[&#10004; - Approve]";
  281.                 span.setAttribute("style", "color:#159615 ; font-weight : bold; font-size : 14px ; cursor : pointer;");
  282.                 span.addEventListener("click", function (id, newStatus) {
  283.                     changeStatus(this, id, newStatus)
  284.                 }
  285.                     .bind(span, userObj.ExternalID, 3));
  286.                 pendingTable.appendChild(tr);
  287.                 if (pendingCounter % 2 == 0) {
  288.                     tr.setAttribute("class", "even");
  289.                 } else {
  290.                     tr.setAttribute("class", "odd");
  291.                 }
  292.                 pendingCounter++;
  293.  
  294.             } else if (userObj.TypeID === 3) {
  295.                 span.innerHTML = "[&#10008; - Remove Access - Contact bold_ally to remove from DB]";
  296.                 span.setAttribute("style", "color:#b50101 ; font-weight : bold; font-size : 14px; cursor : pointer;");
  297.                 span.addEventListener("click", function (id, newStatus) {
  298.                     changeStatus(this, id, newStatus)
  299.                 }
  300.                     .bind(span, userObj.ExternalID, 2));
  301.                 approvedTable.appendChild(tr);
  302.                 if (approvedCounter % 2 == 0) {
  303.                     tr.setAttribute("class", "even");
  304.                 } else {
  305.                     tr.setAttribute("class", "odd");
  306.                 }
  307.                 approvedCounter++;
  308.             }
  309.         }
  310.         lolcontent.appendChild(approvedTable);
  311.         lolcontent.appendChild(pendingTable);
  312.     }
  313.  
  314.     //Sell off value part
  315.     function inteldetail() {
  316.         /*
  317.          * This grabs the first part of the page that inculdes information about the report
  318.          ********************************************************************************
  319.          * EX: Your spy sneaks into someone's base to attempt to gather intelligence.   *
  320.          * Your spy enters undetected. The spy gathers the following information:       *
  321.          ********************************************************************************
  322.          * Using this information we will determin if the intel report is a successful recon,
  323.          * or something else we don't care about
  324.          */
  325.         //Local variables
  326.         //This will store all the weapons for that user. Will also be used to store in a cookie for use outside of the intel page
  327.         var weaponsArray = [
  328.         ];
  329.         //The current estimated sell value without calculating the weapons that cannot be  indentified
  330.         var cesov = 0;
  331.         //This grabs the acutal object and not the text
  332.         //This might be bad cause it depends on the class. Will figure out a way the description using a different method
  333.         var descriptionObject = $('.td.c');
  334.         //Grab the text from the object
  335.         var descriptionText = descriptionObject.html();
  336.         /**
  337.          * Sample out put of the descriptionText
  338.          * console.log(descriptionText);
  339.  
  340.         Your 10 spies sneak into <a href="stats.php?id=28380">someone</a>'s base to attempt to gather intelligence.<br>
  341.         <br>
  342.         Your spies enter undetected.
  343.         <font size="3" color="RED">2</font> of your spies were a little slow and had to become an hero in order to evade capture and interrogation.
  344.         The spies gather the following information:
  345.  
  346.          * We will be using the use ID to reference the player
  347.          **/
  348.         //Search for "gathers the following information" to determin if this is the info we need
  349.         if (descriptionText.indexOf('gathers the following information') == 0) {
  350.             //This is not a valid log, and we need to stop the script,
  351.             //will comment out the log to keep things clean
  352.             //console.log("This is not a valid intel log");
  353.             //Return will cause the function to finish executing
  354.             return;
  355.         } //Use regular expression to match and extract the userID
  356.  
  357.         var userIDregex = /stats\.php\?id=([0-9]+)/;
  358.         var match = descriptionText.match(userIDregex);
  359.         if (match.length < 2) {
  360.             //Either code has changed, or I did something wrong, end the scrip for now
  361.             return;
  362.         } //We store the user id, it will be later used to store the remaining values
  363.         var userID = match[1];
  364.  
  365.         //Now we grab the weapons type
  366.         //We start by grabbig all the tables from the page
  367.         var tables = $('table.sep');
  368.         //Iterate over all the retrieved table to find the one with the header title "Weapons"
  369.         var weaponsObj = false;
  370.  
  371.         tables.each(function () {
  372.             //make a jQuery object
  373.             var tableObj = $(this);
  374.             var tbodyObj = tableObj.children();
  375.             //Check if the first element is a table body
  376.             //I do not think this is needed. And that check can be bypassed
  377.             if (tbodyObj[0].localName == 'tbody') {
  378.                 //We now fetch the first row from the table
  379.                 var trObj = tbodyObj.children().first();
  380.                 //Each row is formed of multiple cells, in the case of the header
  381.                 //It only has one cell, that contains the title
  382.                 //We fetch the first cell and check the title name
  383.                 var headerCell = trObj.children().first();
  384.                 //We check the value inside to figure out if this is the table we need
  385.                 //3 equals also matches the type
  386.                 if (headerCell.html() === 'Weapons') {
  387.                     //Once we found the Weapons table, we store the object and return the inner function
  388.                     //This return will not end the entire script, it will only end the function we are currently in
  389.                     weaponsObj = tableObj;
  390.                     return;
  391.                 }
  392.             }
  393.  
  394.         });
  395.  
  396.         //close the script if we did not find the Weapons table
  397.         if (weaponsObj === false) {
  398.             return;
  399.         } //Get rows from the table.
  400.         //The structure is as follow, Table > tablebody > rows > cells. Also skip the first 2 elemnts since they are headers.
  401.         //gt is zero based, gt(0) will skip the first, gt(1) will skip the first 2
  402.  
  403.         var weaponRows = weaponsObj.children().children(':gt(1)');
  404.         //Get the first row for the title, we will use that later to output our sell off value
  405.         var titleRow = weaponsObj.children().children(':nth-child(1)');
  406.         //Get the titleCell
  407.         titleCell = $(titleRow.children()[0]);
  408.         //Looping over all the rows, each row is a description of a weapon held by user
  409.         weaponRows.each(function () {
  410.             //Create object from row
  411.             var weaponRow = $(this);
  412.             //Get the cells from the row, there are 4 cells, name, type, quantity and strength
  413.             var weaponCells = weaponRow.children();
  414.             //Create an object of the cell, then extract the value from each cell.
  415.             var name = $(weaponCells[0]).html();
  416.             //We need to strip the link from the name that links to a sab. Regular expressions are your friend
  417.             //Sample name text without the link stripped
  418.             //<a href="attack.php?sab=WhoCares&amp;weapon=Maul">Maul</a>
  419.             //Keep in mind that the value can be "???" we need to account for that. I used ternary  operator here
  420.             name = (name === '???') ? '???' : name.match(/>(.*)<\/a>/)[1];
  421.             var type = $(weaponCells[1]).html();
  422.             //Remove the comma from the quantity and transform the string to an integer
  423.             var quantity = ($(weaponCells[2]).html() === '???') ? -1 : parseInt(replaceAll($(weaponCells[2]).html(), ',', ''));
  424.             var curStrength = $(weaponCells[3]).html();
  425.             //Splitting the curStrength to get the current damage and the max strength
  426.             //The max strength will help us determin the sell value of the item even if we are lacking information
  427.             //Check for ??? as well.
  428.             //We add place holder values
  429.             var damage = -1;
  430.             var strength = -1;
  431.             if (curStrength !== '???') {
  432.                 var strengthTok = curStrength.split('/');
  433.                 damage = strengthTok[0];
  434.                 strength = strengthTok[1];
  435.             } //Will now create an object for this weapon, this object will be used to store it in a cookie for later use.
  436.  
  437.             var wep = {
  438.                 'name': name,
  439.                 'type': type,
  440.                 'quantity': quantity,
  441.                 'damage': damage,
  442.                 'strength': strength
  443.             };
  444.             //Add that object to the weapon array
  445.             weaponsArray.push(wep);
  446.  
  447.         });
  448.         var webSTR = JSON.stringify(weaponsArray);
  449.         //Send the weapon array as an string to the server.
  450.         //We also do not care about the result. The server will decide what to do with the data.
  451.         //The results will show on the user page
  452.  
  453.         GM_xmlhttpRequest({
  454.             method: "POST",
  455.             headers: {
  456.                 'Content-type': 'application/x-www-form-urlencoded'
  457.             },
  458.             data: encodeURI("external_id=" + BB_statid + "&user_id=" + userID + "&weapons=" + webSTR),
  459.             url: bbScriptServer + "/roc/storesov",
  460.             onload: function (r) {
  461.                 //Do nothing
  462.             }
  463.         });
  464.  
  465.     }
  466.  
  467.     function stats() {
  468.         var pageURL = window.location.href;
  469.         var userIdTok = pageURL.match(/stats\.php\?id=([0-9]*)/);
  470.         if (userIdTok.length === 2) {
  471.             var userID = userIdTok[1];
  472.  
  473.             GM_xmlhttpRequest({
  474.                 method: "POST",
  475.                 headers: {
  476.                     'Content-type': 'application/x-www-form-urlencoded'
  477.                 },
  478.                 data: encodeURI("external_id=" + BB_statid + "&user_id=" + userID),
  479.                 url: bbScriptServer + "/roc/getplayerpage",
  480.                 onload: function (r) {
  481.                     var obj = JSON.parse(r.responseText);
  482.                     var tables = $(".sep.f");
  483.                     var goldTable = tables[1];
  484.                     var gtObj = $(goldTable);
  485.                     if (obj.Sov !== "") {
  486.                     var sov = obj.Sov;
  487.                         var sovRow = $('<tr><td class="lg" style="width: 25%"><b>SOV:</b></td><td class="lg" style="width: 75%">' + sov.toLocaleString() + '</td></tr>');
  488.                         gtObj.children().append(sovRow);
  489.                     }
  490.                     if (obj.Note !== "") {
  491.                     var note = obj.Note
  492.                         var noteRow = $('<tr><td class="lg" style="width: 25%"><b>Note:</b></td><td class="lg" style="width: 75%">' + note + '</td></tr>');
  493.                         gtObj.children().append(noteRow);                  
  494.                     }
  495.  
  496.                 }
  497.             });
  498.  
  499.         } else {
  500.             console.log("STOP TRYING TO BREAK MY SCRIPT");
  501.         }
  502.     }
  503.  
  504.     function replaceAll(target, search, replacement) {
  505.         return target.split(search).join(replacement);
  506.     };
  507.  
  508. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement