Advertisement
Guest User

Fargrond Experience Bar - Dynamic Color

a guest
Jul 2nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    //==UserScript==+------------------------------------------------------------------------+
  2.    //           _  |          _  __                                                         |
  3.    //          | | |_  _ _  _| |/ /                                                         |
  4.    //  @author:| |_| || | || | ' <                                                          |
  5.    //          |___|\_, |\_, |_|\_\                                                         |
  6.    //              ||_ / |__/       <LyyK@protonmail.ch>                                    |
  7.    //--------------+------------------------------------------------------------------------+
  8.    //  @name       |    Fargrond Experience Bar - Dynamic Color                             |
  9.    //  @version    |    0.1                                                                 |
  10.    //  @namespace  |    http://tampermonkey.net/                                            |
  11.    //  @description|    A simple script that changes the blue color of the experience       |
  12.    //              |    bar to a red-to-green color dynamically based on your current       |
  13.    //              |    experience percentage and initialized parameters.                   |
  14.    //--------------+------------------------------------------------------------------------+
  15.    //  @match           https://fargrond.se/*
  16.    //  @grant           GM_xmlhttpRequest
  17.    //  @require         http://code.jquery.com/jquery-1.12.4.min.js
  18.    //  @require         https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js
  19.    //==/UserScript==
  20.  
  21. (function() {
  22.     'use strict';
  23.    
  24.     var rgb_container = {
  25.         "start": {
  26.             "r": 250,
  27.             "g": 0,
  28.             "b": 0
  29.         },"mult": {
  30.             "r": -15.625,
  31.             "g": 31.25,
  32.             "b": 0
  33.         },"limit": {
  34.             "r": 16,
  35.             "g": 8,
  36.             "b": 0
  37.         }
  38.     };
  39.    
  40.     waitForKeyElements("div.experience-progress", rainbowify);
  41.    
  42.     function rainbowify(XPBarNL) {
  43.     /*=========================================================================
  44.     | Function rainbowify
  45.     |
  46.     |   Purpose: Callback for the waitForKeyElements() utility function that
  47.     |       detects and handles AJAXed content. Called when an element
  48.     |       matching div.experience-progress loads content by GET or POST.
  49.     |       Uses RGB CSS values, pre-determined multipliers, and the
  50.     |       Object.style.width to generate the Object.style.backgroundColor
  51.     |       for the given Object.
  52.     |
  53.     |   Pre-condition: Object.style.backgroundColor of div.experience-progress
  54.     |       is #6c9ad2.
  55.     |
  56.     |   Post-condition: Object.style.backgroundColor of div.experience-progress
  57.     |       is a rgb in the range [rgb(0,250,0), rgb(250,0,0)].
  58.     |
  59.     |   Parameters:
  60.     |       XPBarNL         -- NodeList of all div.experience-progress
  61.     |
  62.     |   Returns:
  63.     |       void
  64.     \*========================================================================*/
  65.         $.each(XPBarNL, function(index, XPBarN) {
  66.             var XPPercentage = parseFloat(XPBarN.style.width);
  67.             var RGB_int = [
  68.                 Math.round( rgb_container.start.r + (rgb_container.mult.r * XPPercentage) ),
  69.                 Math.round( rgb_container.start.g + (rgb_container.mult.g * XPPercentage) ),
  70.                 Math.round( rgb_container.start.b )
  71.             ];
  72.             var CSSRGB_int = "rgb(" + [RGB_int].join(',') + ')';
  73.             document.querySelectorAll("div.experience-progress")[0].style.backgroundColor = CSSRGB_int;
  74.         });
  75.     }
  76.  
  77.     function waitForKeyElements( selectorTxt, actionFunction, bWaitOnce, iframeSelector ) {
  78.     /*=========================================================================
  79.     | Function waitForKeyElements
  80.     |
  81.     |   Purpose: Utility function that detects and handles AJAXed content.
  82.     |
  83.     |   Parameters:
  84.     |       selectorTxt     -- Required: The jQuery selector string that specifies the desired element(s).
  85.     |       actionFunction  -- Required: The code to run when elements are found. It is passed a jNode to the matched element.
  86.     |       bWaitOnce       -- Optional: If false, will continue to scan for new elements even after the first match is found.
  87.     |       iframeSelector  -- Optional: If set, identifies the iframe to search.
  88.     |
  89.     |   Returns:
  90.     |       void
  91.     \*========================================================================*/
  92.         var targetNodes, btargetsFound;
  93.  
  94.         if(typeof iframeSelector == "undefined")
  95.             targetNodes = $(selectorTxt);
  96.         else
  97.             targetNodes = $(iframeSelector).contents().find (selectorTxt);
  98.  
  99.         if (targetNodes && targetNodes.length > 0) {
  100.             //Found target node(s). Go through each and act if they are new.
  101.             btargetsFound = true;
  102.            
  103.             targetNodes.each(function() {
  104.                 var jThis        = $(this);
  105.                 var alreadyFound = jThis.data('alreadyFound') || false;
  106.  
  107.                 if(! alreadyFound ) {
  108.                     //--- Call the payload function.
  109.                     var cancelFound = actionFunction(jThis);
  110.                     if(cancelFound)
  111.                         btargetsFound = false;
  112.                     else
  113.                         jThis.data('alreadyFound', true);
  114.                 }
  115.             });
  116.         } else {
  117.             btargetsFound = false;
  118.         }
  119.  
  120.         //--- Get the timer-control variable for this selector.
  121.         var controlObj  = waitForKeyElements.controlObj || {};
  122.         var controlKey  = selectorTxt.replace(/[^\w]/g, "_");
  123.         var timeControl = controlObj[controlKey];
  124.        
  125.         //--- Now set or clear the timer as appropriate.
  126.         if(btargetsFound && bWaitOnce && timeControl) {
  127.             //--- The only condition where we need to clear the timer.
  128.             clearInterval(timeControl);
  129.             delete controlObj[controlKey];
  130.         } else {
  131.             //--- Set a timer, if needed.
  132.             if(! timeControl ) {
  133.                 timeControl = setInterval(function() {
  134.                     waitForKeyElements(
  135.                         selectorTxt,
  136.                         actionFunction,
  137.                         bWaitOnce,
  138.                         iframeSelector
  139.                     );
  140.                 },300);
  141.                 controlObj[controlKey] = timeControl;
  142.             }
  143.         }
  144.         waitForKeyElements.controlObj = controlObj;
  145.     }
  146. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement