Advertisement
Guest User

Fargrond Health Info Script

a guest
Jun 27th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Fargrond Health Info Script
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  A simple Farground script that displays the health of each gladiator within the battlefield lobby.
  6. // @author       LyyK
  7. // @match        https://fargrond.se/battlefield/*
  8. // @grant        none
  9. // @require      http://code.jquery.com/jquery-1.12.4.min.js
  10. // ==/UserScript==
  11.  
  12. (function() {
  13.     'use strict';
  14.     var nodeList = document.getElementsByClassName("health-progress");
  15.     var hSteps = ['Kritiskt', 'Allvarligt Skadad', 'Halvskadad', 'Lindrigt Skadad', 'Frisk'];
  16.  
  17.     function listIterator(list) {
  18.         [].forEach.call(list, function(item) {
  19.             var hPercent = Math.ceil(parseFloat(item.style.width));
  20.             var hElem;
  21.             var hText;
  22.             var child;
  23.             var fooColor = window.getComputedStyle( item , null).getPropertyValue( "background" );
  24.             var color = fooColor.split(")", 1)[0] + ")";
  25.             if(! item.parentElement.querySelector(".health-addon")) {
  26.                 hElem = document.createElement("div");
  27.                 hElem.className = "health-addon";
  28.                 hElem.setAttribute("style", "font-weight: normal;font-size:12px; color: " + color + ";");
  29.             } else {
  30.                 child = item.parentElement.querySelector(".health-addon");
  31.                 hElem = item.parentElement.querySelector(".health-addon");
  32.                 hElem.setAttribute("style", "font-weight: normal;font-size:12px; color: " + color + ";");
  33.             }
  34.  
  35.             switch (true) {
  36.                 case (hPercent < 15):
  37.                     hText = document.createTextNode(hPercent + "% " + hSteps[0]);
  38.                     hElem.appendChild(hText);
  39.                     if(child)
  40.                         replaceChild(item.parentElement, child, hElem);
  41.                     else
  42.                         appendChild(item.parentElement, hElem);
  43.                     break;
  44.                 case (hPercent >= 15 && hPercent < 38):
  45.                     hText = document.createTextNode(hPercent + "% " + hSteps[1]);
  46.                     hElem.appendChild(hText);
  47.                     if(child)
  48.                         replaceChild(item.parentElement, child, hElem);
  49.                     else
  50.                         appendChild(item.parentElement, hElem);
  51.                     break;
  52.                 case (hPercent >= 38 && hPercent < 60):
  53.                     hText = document.createTextNode(hPercent + "% " + hSteps[2]);
  54.                     hElem.appendChild(hText);
  55.                     if(child)
  56.                         replaceChild(item.parentElement, child, hElem);
  57.                     else
  58.                         appendChild(item.parentElement, hElem);
  59.                     break;
  60.                 case (hPercent >= 60 && hPercent < 100):
  61.                     hText = document.createTextNode(hPercent + "% " + hSteps[3]);
  62.                     hElem.appendChild(hText);
  63.                     if(child)
  64.                         replaceChild(item.parentElement, child, hElem);
  65.                     else
  66.                         appendChild(item.parentElement, hElem);
  67.                     break;
  68.                 default:
  69.                     hText = document.createTextNode(hPercent + "% " + hSteps[4]);
  70.                     hElem.appendChild(hText);
  71.                     if(child)
  72.                         replaceChild(item.parentElement, child, hElem);
  73.                     else
  74.                         appendChild(item.parentElement, hElem);
  75.                     break;
  76.             }
  77.         });
  78.     }
  79.  
  80.     function appendChild(parentElem, newElem) {
  81.         parentElem.appendChild(newElem);
  82.     }
  83.  
  84.     function replaceChild(parentElem, elem, newElem) {
  85.         parentElem.replaceChild(newElem, elem);
  86.     }
  87.  
  88.     waitForKeyElements (".health-bar .health-progress", listIterator);
  89.  
  90.     function waitForKeyElements (selectorTxt, actionFunction, bWaitOnce, iframeSelector) {
  91.         var targetNodes, btargetsFound;
  92.  
  93.         if (typeof iframeSelector == "undefined")
  94.             targetNodes     = $(selectorTxt);
  95.         else
  96.             targetNodes     = $(iframeSelector).contents ()
  97.                 .find (selectorTxt);
  98.  
  99.         if (targetNodes  &&  targetNodes.length > 0) {
  100.             btargetsFound   = true;
  101.             /*--- Found target node(s).  Go through each and act if they
  102.             are new.
  103.         */
  104.             targetNodes.each ( function () {
  105.                 var jThis        = $(this);
  106.                 var alreadyFound = jThis.data ('alreadyFound')  ||  false;
  107.  
  108.                 if (!alreadyFound) {
  109.                     //--- Call the payload function.
  110.                     var cancelFound     = actionFunction (jThis);
  111.                     if (cancelFound)
  112.                         btargetsFound   = false;
  113.                     else
  114.                         jThis.data ('alreadyFound', true);
  115.                 }
  116.             } );
  117.         }
  118.         else {
  119.             btargetsFound   = false;
  120.         }
  121.  
  122.         //--- Get the timer-control variable for this selector.
  123.         var controlObj      = waitForKeyElements.controlObj  ||  {};
  124.         var controlKey      = selectorTxt.replace (/[^\w]/g, "_");
  125.         var timeControl     = controlObj [controlKey];
  126.  
  127.         //--- Now set or clear the timer as appropriate.
  128.         if (btargetsFound  &&  bWaitOnce  &&  timeControl) {
  129.             //--- The only condition where we need to clear the timer.
  130.             clearInterval (timeControl);
  131.             delete controlObj [controlKey];
  132.         }
  133.         else {
  134.             //--- Set a timer, if needed.
  135.             if ( ! timeControl) {
  136.                 timeControl = setInterval ( function () {
  137.                     waitForKeyElements (    selectorTxt,
  138.                                         actionFunction,
  139.                                         bWaitOnce,
  140.                                         iframeSelector
  141.                                        );
  142.                 },
  143.                                            300
  144.                                           );
  145.                 controlObj [controlKey] = timeControl;
  146.             }
  147.         }
  148.         waitForKeyElements.controlObj   = controlObj;
  149.     }
  150.  
  151. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement