Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Fargrond Health Info Script
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description A simple Farground script that displays the health of each gladiator within the battlefield lobby.
- // @author LyyK
- // @match https://fargrond.se/battlefield/*
- // @grant none
- // @require http://code.jquery.com/jquery-1.12.4.min.js
- // ==/UserScript==
- (function() {
- 'use strict';
- var nodeList = document.getElementsByClassName("health-progress");
- var hSteps = ['Kritiskt', 'Allvarligt Skadad', 'Halvskadad', 'Lindrigt Skadad', 'Frisk'];
- function listIterator(list) {
- [].forEach.call(list, function(item) {
- var hPercent = Math.ceil(parseFloat(item.style.width));
- var hElem;
- var hText;
- var child;
- var fooColor = window.getComputedStyle( item , null).getPropertyValue( "background" );
- var color = fooColor.split(")", 1)[0] + ")";
- if(! item.parentElement.querySelector(".health-addon")) {
- hElem = document.createElement("div");
- hElem.className = "health-addon";
- hElem.setAttribute("style", "font-weight: normal;font-size:12px; color: " + color + ";");
- } else {
- child = item.parentElement.querySelector(".health-addon");
- hElem = item.parentElement.querySelector(".health-addon");
- hElem.setAttribute("style", "font-weight: normal;font-size:12px; color: " + color + ";");
- }
- switch (true) {
- case (hPercent < 15):
- hText = document.createTextNode(hPercent + "% " + hSteps[0]);
- hElem.appendChild(hText);
- if(child)
- replaceChild(item.parentElement, child, hElem);
- else
- appendChild(item.parentElement, hElem);
- break;
- case (hPercent >= 15 && hPercent < 38):
- hText = document.createTextNode(hPercent + "% " + hSteps[1]);
- hElem.appendChild(hText);
- if(child)
- replaceChild(item.parentElement, child, hElem);
- else
- appendChild(item.parentElement, hElem);
- break;
- case (hPercent >= 38 && hPercent < 60):
- hText = document.createTextNode(hPercent + "% " + hSteps[2]);
- hElem.appendChild(hText);
- if(child)
- replaceChild(item.parentElement, child, hElem);
- else
- appendChild(item.parentElement, hElem);
- break;
- case (hPercent >= 60 && hPercent < 100):
- hText = document.createTextNode(hPercent + "% " + hSteps[3]);
- hElem.appendChild(hText);
- if(child)
- replaceChild(item.parentElement, child, hElem);
- else
- appendChild(item.parentElement, hElem);
- break;
- default:
- hText = document.createTextNode(hPercent + "% " + hSteps[4]);
- hElem.appendChild(hText);
- if(child)
- replaceChild(item.parentElement, child, hElem);
- else
- appendChild(item.parentElement, hElem);
- break;
- }
- });
- }
- function appendChild(parentElem, newElem) {
- parentElem.appendChild(newElem);
- }
- function replaceChild(parentElem, elem, newElem) {
- parentElem.replaceChild(newElem, elem);
- }
- waitForKeyElements (".health-bar .health-progress", listIterator);
- function waitForKeyElements (selectorTxt, actionFunction, bWaitOnce, iframeSelector) {
- var targetNodes, btargetsFound;
- if (typeof iframeSelector == "undefined")
- targetNodes = $(selectorTxt);
- else
- targetNodes = $(iframeSelector).contents ()
- .find (selectorTxt);
- if (targetNodes && targetNodes.length > 0) {
- btargetsFound = true;
- /*--- Found target node(s). Go through each and act if they
- are new.
- */
- targetNodes.each ( function () {
- var jThis = $(this);
- var alreadyFound = jThis.data ('alreadyFound') || false;
- if (!alreadyFound) {
- //--- Call the payload function.
- var cancelFound = actionFunction (jThis);
- if (cancelFound)
- btargetsFound = false;
- else
- jThis.data ('alreadyFound', true);
- }
- } );
- }
- else {
- btargetsFound = false;
- }
- //--- Get the timer-control variable for this selector.
- var controlObj = waitForKeyElements.controlObj || {};
- var controlKey = selectorTxt.replace (/[^\w]/g, "_");
- var timeControl = controlObj [controlKey];
- //--- Now set or clear the timer as appropriate.
- if (btargetsFound && bWaitOnce && timeControl) {
- //--- The only condition where we need to clear the timer.
- clearInterval (timeControl);
- delete controlObj [controlKey];
- }
- else {
- //--- Set a timer, if needed.
- if ( ! timeControl) {
- timeControl = setInterval ( function () {
- waitForKeyElements ( selectorTxt,
- actionFunction,
- bWaitOnce,
- iframeSelector
- );
- },
- 300
- );
- controlObj [controlKey] = timeControl;
- }
- }
- waitForKeyElements.controlObj = controlObj;
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement