Advertisement
LennardTFD

DjPaTT MissionHeaderColorizer

May 27th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Mission Header Colorizer
  3. // @namespace    https://www.leitstellenspiel.de/
  4. // @version      1.3
  5. // @description  Changes Color of Header, depending on City, Mission ID or ZIP Code
  6. // @author       LennardTFD
  7. // @match        https://www.leitstellenspiel.de/
  8. // @match        https://www.leitstellenspiel.de/missions/*
  9. // @match        https://www.missionchief.com/
  10. // @match        https://www.missionchief.com/missions/*
  11. // @match        https://www.meldkamerspel.com/
  12. // @match        https://www.meldkamerspel.com/missions/*
  13. // @updateURL    https://github.com/LennardTFD/LeitstellenspielScripte/raw/master/LSS_MissionHeaderColorizer/missionHeaderColorizer.user.js
  14. // @downloadURL  https://github.com/LennardTFD/LeitstellenspielScripte/raw/master/LSS_MissionHeaderColorizer/missionHeaderColorizer.user.js
  15. // @grant        none
  16. // ==/UserScript==
  17.  
  18.  
  19. var settingsNamespace = "LSS_missionColorizer";
  20.  
  21. function resetSettings(reqConfirm)
  22. {
  23.  
  24.     if(!reqConfirm || confirm("Sicher? Alle deine Farbeinstellungen gehen verloren!")) {
  25.         localStorage.setItem(settingsNamespace, JSON.stringify({
  26.             "city":
  27.                 {"cities": [], "colors": []},
  28.             "mission":
  29.                 {"ids": [], "colors": []},
  30.             "zip":
  31.                 {"zips": [], "zipColors": []},
  32.             "tab":
  33.                 {"ids": [], "tabColors": []},
  34.             "defaultColor": ["#000000", false, 80],
  35.         }));
  36.     }
  37.  
  38.     loadSettings();
  39. }
  40.  
  41. function loadSettings()
  42. {
  43.     var colorData = JSON.parse(localStorage.getItem(settingsNamespace));
  44.  
  45.     settings = colorData;
  46.  
  47.     transparency = colorData["defaultColor"][2];
  48.     cities = colorData["city"]["cities"]; //Zu filternde Städte/Orte
  49.     citiesColors = colorData["city"]["colors"];//Farbe der zugehörigen Städte
  50.  
  51.     missionIDs = colorData["mission"]["ids"];
  52.     missionColors = colorData["mission"]["colors"]; //Farbe der Missionen
  53.  
  54.     tabs = colorData["tab"]["ids"]; //ID des Tabs
  55.     tabColors = colorData["tab"]["colors"]; //Des Tabs
  56.  
  57.     zips = colorData["zip"]["zips"]; //ID des Tabs
  58.     zipColors = colorData["zip"]["colors"]; //Des Tabs
  59.  
  60.     if(settings["defaultColor"][1])
  61.     {
  62.         defaultColor = settings["defaultColor"][0];
  63.     }
  64.     else
  65.     {
  66.         defaultColor = "";
  67.     }
  68.     return colorData;
  69. }
  70. ////////////////////FIST SETUP OR UPDATE//////////////////
  71. var firstLoad = JSON.parse(localStorage.getItem(settingsNamespace));
  72. if(localStorage.getItem(settingsNamespace) == null || firstLoad["defaultColor"] == null || firstLoad["tab"] == null || firstLoad["zip"] == null)
  73. {
  74.     if(firstLoad == null)
  75.     {
  76.         resetSettings(false);
  77.     }
  78.     else if(firstLoad["tab"] == null)
  79.     {
  80.         firstLoad["tab"] = {"ids": [], "tabColors": []};
  81.         localStorage.setItem(settingsNamespace, JSON.stringify(firstLoad));
  82.     }
  83.     else if(firstLoad["zip"] == null)
  84.     {
  85.         firstLoad["zip"] = {"zips": [], "zipColors": []};
  86.         localStorage.setItem(settingsNamespace, JSON.stringify(firstLoad));
  87.     }
  88. }
  89. ///////SETTINGS AREA START//////////////
  90. var settings = loadSettings();
  91. var transparency, cities, citiesColors, zips, zipColors, missionIDs, missionColors, tabs, tabColors, defaultColor;
  92. ///////SETTINGS AREA END//////////////
  93.  
  94.  
  95.  
  96. (function() {
  97.     'use strict';
  98.  
  99.     var missionID = -1;
  100.     var colorIndex = -1;
  101.     var colorSet = false;
  102.     var closed;
  103.     var currentGame = window.location.hostname;
  104.     var currentPage = window.location.pathname;
  105.  
  106.     //Create Button in Navbar
  107.     function createButton()
  108.     {
  109.         var btn = $(' <li class="dropdown" id="colorizerDropdown">' +
  110.             '<a href="#" class="dropdown-toggle" role="button" data-toggle="dropdown"><span style="color: white" class="glyphicon glyphicon-tint"><b class="caret"></b></a>' +
  111.             '<ul class="dropdown-menu">' +
  112.             '<li class="lightbox-open" id="colorizer"><a href="#" id="colorizerButton">Einstellungen</a></li>' +
  113.             '<li><a href="#" id="colorizerReload">Neu Laden</a></li>' +
  114.             '</ul></li>');
  115.         $('#navbar-main-collapse > ul').append(btn);
  116.  
  117.         $( "#colorizer" ).click(function() {
  118.             createForm();
  119.         });
  120.         $( "#colorizerReload" ).click(function() {
  121.             loadSettings();
  122.             colorizeMap();
  123.         });
  124.  
  125.     }
  126.  
  127.     ////////////////////Settings Form Stuff////////////////////
  128.  
  129.     //Create Color Menu when called via Bucked Icon
  130.     function createForm()
  131.     {
  132.         //Default Color
  133.         var defaultColor = "Standard Farbe: <input id='defaultColor' class='form-control' style='width: 80px;' type='color'> Nutzen: <input type='checkbox' id='enableDefaultColor'><br>" +
  134.             "Transparenz: <input type='range' value='100' max='100' min='0' step='10' id='colorTransparency' class='custom-range' style='width: 300px'><br><br>";
  135.  
  136.         //Colors for City Names
  137.         var divCity = "<div id='cityDiv'>" +
  138.             "<h3>Stadt Namen</h3>" +
  139.             //"<input id='addCity' class='btn btn-default' type='button' value='+ Stadt'>" +
  140.             "<button id='addCity' class='btn btn-default'><i class='glyphicon glyphicon-plus'></i> Stadt</button>" +
  141.             "<table id='cityTable'><tr><th width='150px'>Stadt</th><th>Farbe</th><th></th></tr></table>" +
  142.             "</div><br><br>";
  143.  
  144.         var divZip = "<div id='zipDiv'>" +
  145.             "<h3>Postleitzahlen</h3>" +
  146.             "<p>Beispiel:</p>" +
  147.             "<textarea disabled style='width: 150px; height: 30px;'>67892, 40892, 75241</textarea><br>" +
  148.             //"<input id='addZip' class='btn btn-default' type='button' value='+ ZIP'>" +
  149.             "<button id='addZip' class='btn btn-default'><i class='glyphicon glyphicon-plus'></i> Zip</button>" +
  150.             "<table id='zipTable'>" +
  151.             "<tr id='zipColorRow'></tr>" + //Color Row
  152.             "<tr id='zipRow'></tr>" + //Color Row
  153.             "</table>" +
  154.             "</div><br><br>";
  155.  
  156.  
  157.         //MISSION IDS///
  158.  
  159.         var divMission = "<div id='missionDiv'>" +
  160.             "<h3>Mission IDs</h3>" +
  161.             "<p>Beispiel:</p>" +
  162.             "<textarea disabled style='width: 150px; height: 30px;'>54, 32, 145, 290, 25</textarea><br>" +
  163.             //"<input id='addMissionColor' class='btn btn-default' type='button' value='+ Farbe'>" +
  164.             "<button id='addMissionColor' class='btn btn-default'><i class='glyphicon glyphicon-plus'></i> Farbe</button>" +
  165.             "<table id='missionTable'>" +
  166.             "<tr id='missionColorRow'></tr>" + //Color Row
  167.             "<tr id='missionIdRow'></tr>" + //Color Row
  168.             "</table>" +
  169.             "</div><br><br>";
  170.  
  171.         var divTab = "<div id='tabDiv'>" +
  172.             "<h3>Tab Färbung</h3>" +
  173.             //"<input id='addTab' class='btn btn-default' type='button' value='+ Tab'>" +
  174.             "<button id='addTab' class='btn btn-default'><i class='glyphicon glyphicon-plus'></i> Tab</button>" +
  175.             "<table id='tabTable'><tr><th width='200px'>Tab ID</th><th>Farbe</th><th></th></tr></table>" +
  176.             "</div>";
  177.  
  178.  
  179.  
  180.         var saver = "<br><input id='saveSettings' class='btn btn-success btn btn-lg' type='button' value='Speichern'>";
  181.  
  182.         //Reset Colors
  183.         var reseter = "<br>Sollten keine Einsätze eingefäbrt werden oder andere Fehler auftreten, kannst du hier deine Einstellungen zurücksetzen: <input style='color: white;' type='button' id='resetSettings' class='btn btn-danger' value='Zurücksetzen'>";
  184.  
  185.         //Set Content of Form
  186.         var newWindow = $("#lightbox_box")[0];
  187.         var content = "<div id='colorWrapper' class='panel-body' style='margin: 10px'>" + defaultColor + divCity + divZip + divMission + divTab + saver + reseter + "</div>";
  188.         $(newWindow).html(content);
  189.  
  190.  
  191.         for(var i = 0; i < cities.length; i++)
  192.         {
  193.             addCity(cities[i], citiesColors[i]);
  194.         }
  195.  
  196.         for(var i = 0; i < zips.length; i++)
  197.         {
  198.             addZip(zips[i], zipColors[i]);
  199.         }
  200.  
  201.         for(var i = 0; i < missionColors.length; i++)
  202.         {
  203.             addMissionColor(missionIDs[i], missionColors[i]);
  204.         }
  205.  
  206.         for(var i = 0; i < tabs.length; i++)
  207.         {
  208.             addTab(tabs[i], tabColors[i]);
  209.         }
  210.  
  211.         $("#defaultColor").val(settings["defaultColor"][0]);
  212.         $("#colorTransparency").val(settings["defaultColor"][2]);
  213.  
  214.         if(settings["defaultColor"][1])
  215.         {
  216.             $("#enableDefaultColor").prop("checked", true);
  217.         }
  218.  
  219.         $( "#addCity" ).click(function() {
  220.             addCity();
  221.         });
  222.         $( "#addZip" ).click(function() {
  223.             addZip();
  224.         });
  225.         $( "#addMissionColor" ).click(function() {
  226.             addMissionColor();
  227.         });
  228.         $( "#addTab" ).click(function() {
  229.             addTab();
  230.         });
  231.         $( "#saveSettings" ).click(function() {
  232.             saveSettings();
  233.         });
  234.         $( "#resetSettings" ).click(function() {
  235.             resetSettings(true);
  236.         });
  237.  
  238.         $("[id^='lightbox_iframe_']").remove();
  239.         //Make Form Scrollable
  240.         $("#lightbox_box").css("overflow-y", "auto");
  241.         closed = setInterval(removeForm, 100);
  242.     }
  243.  
  244.     //Delete Form Content after closed
  245.     function removeForm()
  246.     {
  247.         if($("#lightbox_background").css("display") == "none") {
  248.             $("#colorWrapper").remove();
  249.             $("#lightbox_box").css("overflow-y", "");
  250.             clearInterval(closed);
  251.         }
  252.         $("[id^='lightbox_iframe_']").remove();
  253.  
  254.     }
  255.  
  256.     //Add a new Input for Mission IDs
  257.     function addMissionColor(missionIDs, color)
  258.     {
  259.  
  260.         if(missionIDs != null && color != null)
  261.         {
  262.             var currID = missionIDs[0];
  263.             for(var i = 1; i < missionIDs.length; i++)
  264.             {
  265.                 currID = currID + ", " + missionIDs[i];
  266.             }
  267.  
  268.             //var newColorRow = "<th><input width='70px' type='color' class='missionColor' value='" + color + "'><input class='removeColor' type='button' value='- Farbe'></th>";
  269.             var newColorRow = "<th><input width='70px' type='color' class='missionColor form-control' style='width: 80px;' value='" + color + "'><button id='removeColor' class='removeColor btn btn-default'><i class='glyphicon glyphicon-minus'></i> Farbe</button></th>";
  270.             var newIdRow = "<td><textarea width='70px' class='missionId form-control'>" + currID + "</textarea></td>";
  271.  
  272.         }
  273.         else
  274.         {
  275.             //var newColorRow = "<th><input width='70px' type='color' class='missionColor'><input class='removeColor' type='button' value='- Farbe'></th>";
  276.             var newColorRow = "<th><input width='70px' type='color' class='missionColor form-control' style='width: 80px;'><button id='removeColor' class='removeColor btn btn-default'><i class='glyphicon glyphicon-minus'></i> Farbe</button></th>";
  277.             var newIdRow = "<td><textarea width='70px' class='missionId form-control'></textarea></td>";
  278.         }
  279.  
  280.         newColorRow = $.parseHTML(newColorRow);
  281.         newIdRow = $.parseHTML(newIdRow);
  282.  
  283.         $("#missionColorRow").append(newColorRow);
  284.         $("#missionIdRow").append(newIdRow);
  285.  
  286.         $( $(newColorRow).find(".removeColor") ).click(function() {
  287.             removeMissionColor($(newColorRow));
  288.             removeMissionColor($(newIdRow));
  289.         });
  290.     }
  291.  
  292.     //Remove City Input
  293.     function removeMissionColor(item)
  294.     {
  295.         item.remove(); //Remove Mission Color
  296.     }
  297.  
  298.     //Create new Input for City
  299.     function addCity(cityName, color)
  300.     {
  301.         if(cityName != null && color != null)
  302.         {
  303.             //var cityInput = "<tr class='city'><td><input type='text' name='city' value='" + cityName + "'></td><td><input type='color' name='color' value='" + color + "'></td><td><input class='removeCity' type='button' value='- Stadt'></td></tr>";
  304.             var cityInput = "<tr class='city'><td><input type='text' name='city' class='form-control' value='" + cityName + "'></td><td><input type='color' name='color' class='form-control' style='width: 80px;' value='" + color + "'></td><td><button class='removeCity btn btn-default'><i class='glyphicon glyphicon-minus'></i> Stadt</button></td></tr>";
  305.         }
  306.         else
  307.         {
  308.             var cityInput = "<tr class='city'><td><input type='text' name='city' class='form-control' ></td><td><input type='color' name='color' class='form-control' style='width: 80px;'></td><td><button class='removeCity btn btn-default'><i class='glyphicon glyphicon-minus'></i> Stadt</button></td></tr>";
  309.         }
  310.         cityInput = $.parseHTML(cityInput);
  311.         $("#cityTable").append(cityInput);
  312.  
  313.         $( $(cityInput).find(".removeCity") ).click(function() {
  314.             removeCity($(cityInput));
  315.         });
  316.  
  317.  
  318.     }
  319.  
  320.  
  321.  
  322.     //Remove City Input
  323.     function removeCity(item)
  324.     {
  325.         item.remove(); //Remove City
  326.     }
  327.     //Add ZIP input
  328.     function addZip(zips, color)
  329.     {
  330.  
  331.         if(zips != null && color != null)
  332.         {
  333.             var currZIP = zips[0];
  334.             for(var i = 1; i < zips.length; i++)
  335.             {
  336.                 currZIP = currZIP + ", " + zips[i];
  337.             }
  338.  
  339.             //var newColorRow = "<th><input width='70px' type='color' class='zipColor' value='" + color + "'><input class='removeZip' type='button' value='- ZIP'></th>";
  340.             var newColorRow = "<th><input width='70px' type='color' class='zipColor form-control' style='width: 80px;' value='" + color + "'><button class='removeZip btn btn-default'><i class='glyphicon glyphicon-minus'></i> Zip</button></th>";
  341.             var newZipRow = "<td><textarea width='70px' class='zip form-control'>" + currZIP + "</textarea></td>";
  342.  
  343.         }
  344.         else
  345.         {
  346.             var newColorRow = "<th><input width='70px' type='color' style='width: 80px;' class='zipColor form-control'><button class='removeZip btn btn-default'><i class='glyphicon glyphicon-minus'></i> Zip</button></th>";
  347.             var newZipRow = "<td><textarea width='70px' class='zip form-control'></textarea></td>";
  348.         }
  349.  
  350.         newColorRow = $.parseHTML(newColorRow);
  351.         newZipRow = $.parseHTML(newZipRow);
  352.  
  353.         $("#zipColorRow").append(newColorRow);
  354.         $("#zipRow").append(newZipRow);
  355.  
  356.         $( $(newColorRow).find(".removeZip") ).click(function() {
  357.             removeZip($(newColorRow));
  358.             removeZip($(newZipRow));
  359.         });
  360.     }
  361.  
  362.     //Remove City Input
  363.     function removeZip(item)
  364.     {
  365.         item.remove(); //Remove City
  366.     }
  367.  
  368.     //Add Tab Color
  369.     function addTab(tabID, color)
  370.     {
  371.         if(tabID != null && color != null)
  372.         {
  373.             //var tabInput = "<tr class='tab'><td><input type='text' name='tab' value='" + tabID + "'></td><td><input type='color' name='tabColor' value='" + color + "'></td><td><input class='removeTab' type='button' value='- Tab'></td></tr>";
  374.             var tabInput = "<tr class='tab'><td><input type='text' name='tab' class='form-control' value='" + tabID + "'></td><td><input type='color' name='tabColor' class='form-control' style='width: 80px;' value='" + color + "'></td><td><button class='removeTab btn btn-default'><i class='glyphicon glyphicon-minus'></i> Tab</button></td></tr>";
  375.         }
  376.         else
  377.         {
  378.             var tabInput = "<tr class='tab'><td><input type='text' name='tab' class='form-control'></td><td><input type='color' name='tabColor' class='form-control' style='width: 80px;'></td><td><button class='removeTab btn btn-default'><i class='glyphicon glyphicon-minus'></i> Tab</button></td></tr>";
  379.         }
  380.         tabInput = $.parseHTML(tabInput);
  381.         $("#tabTable").append(tabInput);
  382.  
  383.         $( $(tabInput).find(".removeTab") ).click(function() {
  384.             removeTab($(tabInput));
  385.         });
  386.     }
  387.  
  388.     //Remove Tab Input
  389.     function removeTab(item)
  390.     {
  391.         item.remove(); //Remove City
  392.     }
  393.  
  394.     //Save Settings from Input to Local Storage
  395.     function saveSettings()
  396.     {
  397.         var cityColorJSON = {};
  398.         var cities = [];
  399.         var colors = [];
  400.  
  401.         var missionColorJSON = {};
  402.         var missionColors = [];
  403.         var missionIDs = [];
  404.  
  405.         var tabColorJSON = {};
  406.         var tabColors = [];
  407.         var tabIDs = [];
  408.  
  409.         var zipColorJSON = {};
  410.         var zipColors = [];
  411.         var zipCodes = [];
  412.  
  413.         var combine = {};
  414.  
  415.         $(".city").each(function(index)
  416.         {
  417.             cities.push($("[name='city']:eq(" + index + ")").val());
  418.             colors.push($("[name='color']:eq(" + index + ")").val());
  419.         });
  420.  
  421.         $(".zip").each(function(index)
  422.         {
  423.             var zips = $.map($(".zip:eq(" + index + ")").val().split(','), function(value){
  424.  
  425.                 var out = parseInt(value, 10);
  426.                 if(isNaN(out))
  427.                 {
  428.                     out = [-1];
  429.                 }
  430.                 return out;
  431.             });
  432.  
  433.             zipColors.push($(".zipColor:eq(" + index + ")").val());
  434.             zipCodes.push(zips);
  435.         });
  436.  
  437.         $(".missionColor").each(function(index)
  438.         {
  439.             var ids = $.map($(".missionId:eq(" + index + ")").val().split(','), function(value){
  440.  
  441.                 var out = parseInt(value, 10);
  442.                 if(isNaN(out))
  443.                 {
  444.                     out = [-1];
  445.                 }
  446.                 return out;
  447.             });
  448.  
  449.             missionColors.push($(".missionColor:eq(" + index + ")").val());
  450.             missionIDs.push(ids);
  451.         });
  452.  
  453.         $(".tab").each(function(index)
  454.         {
  455.             tabIDs.push($("[name='tab']:eq(" + index + ")").val());
  456.             tabColors.push($("[name='tabColor']:eq(" + index + ")").val());
  457.         });
  458.  
  459.  
  460.         var defaultColor = [$("#defaultColor").val(), $("#enableDefaultColor").prop("checked"), $("#colorTransparency").val()];
  461.  
  462.         cityColorJSON["cities"] = cities;
  463.         cityColorJSON["colors"] = colors;
  464.  
  465.         missionColorJSON["ids"] = missionIDs;
  466.         missionColorJSON["colors"] = missionColors;
  467.  
  468.         tabColorJSON["ids"] = tabIDs;
  469.         tabColorJSON["colors"] = tabColors;
  470.  
  471.         zipColorJSON["zips"] = zipCodes;
  472.         zipColorJSON["colors"] = zipColors;
  473.  
  474.         combine["city"] = cityColorJSON;
  475.         combine["zip"] = zipColorJSON;
  476.         combine["mission"] = missionColorJSON;
  477.         combine["tab"] = tabColorJSON;
  478.  
  479.  
  480.         combine["defaultColor"] = defaultColor;
  481.  
  482.         localStorage.setItem(settingsNamespace, JSON.stringify(combine));
  483.  
  484.         loadSettings();
  485.     }
  486.  
  487.  
  488.     function luminanace(r, g, b) {
  489.         var a = [r, g, b].map(function (v) {
  490.             v /= 255;
  491.             return v <= 0.03928
  492.                 ? v / 12.92
  493.                 : Math.pow( (v + 0.055) / 1.055, 2.4 );
  494.         });
  495.         return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
  496.     }
  497.  
  498.     function contrast(rgb1, rgb2) {
  499.         return (luminanace(rgb1[0], rgb1[1], rgb1[2]) + 0.05)
  500.             / (luminanace(rgb2[0], rgb2[1], rgb2[2]) + 0.05);
  501.     }
  502.  
  503.     function hexToRgb(hex) {
  504.         var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  505.         return result ? [
  506.             parseInt(result[1], 16),
  507.             parseInt(result[2], 16),
  508.             parseInt(result[3], 16)
  509.         ] : null;
  510.     }
  511.  
  512.  
  513.     function regexLastIndexOf(string, regex, startpos) {
  514.         regex = (regex.global) ? regex : new RegExp(regex.source, "g" + (regex.ignoreCase ? "i" : "") + (regex.multiLine ? "m" : ""));
  515.         if(typeof (startpos) == "undefined") {
  516.             startpos = string.length;
  517.         } else if(startpos < 0) {
  518.             startpos = 0;
  519.         }
  520.         var stringToWorkWith = string.substring(0, startpos + 1);
  521.         var lastIndexOf = -1;
  522.         var nextStop = 0;
  523.         var result;
  524.         while((result = regex.exec(stringToWorkWith)) != null) {
  525.             lastIndexOf = result.index;
  526.             regex.lastIndex = ++nextStop;
  527.         }
  528.         return lastIndexOf;
  529.     }
  530.  
  531.  
  532.  
  533.     ////////////////////Colorize Stuff////////////////////
  534.  
  535.     //Get City of Mission
  536.     function getOrt(page, mission)
  537.     {
  538.         if(page == "map" && mission != null)
  539.         {
  540.             var ort = $(mission).find("[id^='mission_address_']").text();
  541.             //var ort = $("#missions_outer").find("[id^='mission_address_']:eq(" + index + ")").text();
  542.         }else if(page == "mission")
  543.         {
  544.             var ort = $(".col-md-6:eq(0)").find("small").text();
  545.         }
  546.  
  547.         if(ort.includes("Von: "))
  548.         {
  549.             ort = ort.slice(ort.indexOf("Von: ") + 5, ort.indexOf("Nach: "));
  550.         }
  551.         if(ort.includes("|"))
  552.         {
  553.             ort = ort.slice(0, ort.indexOf("|"));
  554.         }
  555.  
  556.         var cityBegin = regexLastIndexOf(ort, /\d/);
  557.         ort = ort.slice(cityBegin + 2);
  558.  
  559.         return ort;
  560.     }
  561.  
  562.     function getZip(page, mission)
  563.     {
  564.         if(page == "map" && mission != null)
  565.         {
  566.             var zip = $(mission).find("[id^='mission_address_']").text();
  567.         }else if(page == "mission")
  568.         {
  569.             var zip = $(".col-md-6:eq(0)").find("small").text();
  570.         }
  571.  
  572.         if(zip.includes("Von: "))
  573.         {
  574.             zip = zip.slice(zip.indexOf("Von: ") + 5, zip.indexOf("Nach: "));
  575.         }
  576.  
  577.         var zipStart = zip.lastIndexOf(",");
  578.         if(zipStart == -1)
  579.         {
  580.             zipStart = -2;
  581.         }
  582.         zip = zip.slice(zipStart + 2);
  583.         var zipEnd = zip.indexOf(" ");
  584.         if(zipEnd != -1)
  585.         {
  586.             zip = zip.slice(0, zipEnd);
  587.         }
  588.         return zip;
  589.     }
  590.  
  591.     //Colorize Mission List
  592.     function colorizeMap()
  593.     {
  594.         var subIndexKTW = 0;
  595.         var subIndexSW = 0;
  596.         var missionList = $("#missions_outer").find(".panel-heading");
  597.         //var missionList = $("#mission_list").find(".panel-heading");
  598.         if(transparency > 99)
  599.         {
  600.             transparency = "ff";
  601.         }
  602.  
  603.         $(missionList).each(function(index, mission){
  604.  
  605.             colorSet = false;
  606.  
  607.             var ort = getOrt("map", mission);
  608.             var zip = getZip("map", mission);
  609.             var cityIndex = findSimilarOrt(ort);
  610.             //var zipIndex = zips.indexOf(zip);
  611.  
  612.             var missionMultiplicator = 16;
  613.             if($(".MissionOut").length > 0)
  614.             {
  615.                 missionMultiplicator = 17;
  616.             }
  617.  
  618.             var missionListLength = $("[id='mission_list']").find(".missionSideBarEntrySearchable").length;
  619.             var ktwListLength = $("[id='mission_list_krankentransporte']").find(".missionSideBarEntrySearchable").length;
  620.             var ktwOrMission = "";
  621.             if(index < missionListLength)
  622.             {
  623.                 var missionID = $("[id='mission_list']").find("[id^='mission_']:eq(" + index * missionMultiplicator + ")").attr("mission_type_id");
  624.                 ktwOrMission = "mission";
  625.             }
  626.             else if(index < (missionListLength + ktwListLength))
  627.             {
  628.                 var missionID = $("[id='mission_list_krankentransporte']").find("[id^='mission_']:eq(" + subIndexKTW * missionMultiplicator + ")").attr("mission_type_id");
  629.                 ktwOrMission = "ktw";
  630.                 subIndexKTW++;
  631.             }
  632.             else
  633.             {
  634.                 var missionID = $("[id='mission_list_sicherheitswache']").find("[id^='mission_']:eq(" + subIndexSW * missionMultiplicator + ")").attr("mission_type_id");
  635.                 subIndexSW++;
  636.             }
  637.  
  638.  
  639.             missionList[index].style.backgroundImage = "none";
  640.             if(defaultColor == "")
  641.             {
  642.                 missionList[index].style["background-color"] = "";
  643.             }
  644.             else
  645.             {
  646.                 missionList[index].style["background-color"] = defaultColor + transparency;
  647.             }
  648.  
  649.             var cityNameColor = colorByOrt("map", cityIndex, index, missionList);
  650.             var zipCodeColor = colorByZip("map", zip, index, missionList);
  651.             var missionIdColor = colorByMission("map", missionID, index, missionList);
  652.  
  653.             var assignedBgColor = missionList[index].style["background-color"];
  654.             if(assignedBgColor.includes("rgba"))
  655.             {
  656.                 assignedBgColor = assignedBgColor.replace("rgba(", "");
  657.             }
  658.             else
  659.             {
  660.                 assignedBgColor = assignedBgColor.replace("rgb(", "");
  661.             }
  662.             assignedBgColor = assignedBgColor.replace(")", "");
  663.             assignedBgColor = assignedBgColor.split(",");
  664.             assignedBgColor[3] = "1";
  665.             //console.log(assignedBgColor);
  666.  
  667.             if(assignedBgColor.length > 1) {
  668.                 if (contrast([255, 255, 255], assignedBgColor) < 4.5) {
  669.                     $(missionList[index]).find("a:eq(1)").css("color", "black");
  670.                 } else {
  671.                     $(missionList[index]).find("a:eq(1)").css("color", "white");
  672.                 }
  673.             }
  674.             else
  675.             {
  676.                 $(missionList[index]).find("a:eq(1)").css("color", "#337ab7");
  677.             }
  678.  
  679.  
  680.         });
  681.     }
  682.  
  683.     //Colorize Mission Header
  684.     function colorizeMission()
  685.     {
  686.         colorizeTabs();
  687.         //Get full Address
  688.         var ort = getOrt("mission", -1);
  689.         var zip = getZip("mission");
  690.         var cityIndex = findSimilarOrt(ort);
  691.  
  692.         //Get Mission ID
  693.         missionID = $("#mission_help")[0].href;
  694.         missionID = parseInt(missionID.slice(missionID.search(/\d/), missionID.indexOf("?")));
  695.  
  696.         if(defaultColor == "")
  697.         {
  698.             $(".mission_header_info").css("background-color", "");
  699.         }
  700.         else
  701.         {
  702.             $(".mission_header_info").css("background-color", defaultColor);
  703.         }
  704.  
  705.  
  706.         var cityNameColor = colorByOrt("mission", cityIndex, -1, []);
  707.         var zipColor = colorByZip("mission", zip, -1, []);
  708.         var missionIdColor = colorByMission("mission", missionID, -1, []);
  709.  
  710.         var color = $(".mission_header_info").css("background-color");
  711.         color = color.replace("rgb(", "");
  712.         color = color.replace(")", "");
  713.         color = color.split(",");
  714.         //Calculate Contrast
  715.         if(contrast([255, 255, 255], color) < 4.5)
  716.         {
  717.             $(".mission_header_info:eq(0)").css("color", "black");
  718.             $(".patientPrisonerIcon:eq(0)").css("filter", "brightness(0)");
  719.         }
  720.         else
  721.         {
  722.             $(".mission_header_info:eq(0)").css("color", "white");
  723.         }
  724.     }
  725.  
  726.     function colorizeTabs()
  727.     {
  728.         for(var i = 0; i < tabs.length; i++)
  729.         {
  730.             $("a[href='" + tabs[i] +"']").css("background-color", tabColors[i]);
  731.  
  732.             var color = $("a[href='" + tabs[i] +"']").css("background-color");
  733.             color = color.replace("rgb(", "");
  734.             color = color.replace(")", "");
  735.             color = color.split(",");
  736.  
  737.             if(contrast([255, 255, 255], color) < 4.5)
  738.             {
  739.                 $("a[href='" + tabs[i] +"']").css("color", "black");
  740.             }
  741.             else
  742.             {
  743.                 $("a[href='" + tabs[i] +"']").css("color", "white");
  744.             }
  745.  
  746.         }
  747.     }
  748.  
  749.     //Colorize Mission by its ID
  750.     function colorByMission(page, missionID, index, missionList)
  751.     {
  752.         var found = false;
  753.         for(var i = 0; i < missionIDs.length; i++)
  754.         {
  755.             for(var j = 0; j < missionIDs[i].length; j++)
  756.             {
  757.                 if(missionIDs[i][j] == missionID)
  758.                 {
  759.                     colorIndex = i;
  760.                     found = true;
  761.                     break;
  762.                 }
  763.             }
  764.             if(found)
  765.             {
  766.                 break;
  767.             }
  768.         }
  769.         if(!found)
  770.         {
  771.             colorIndex = -1;
  772.         }
  773.  
  774.         if(page == "map" && colorIndex != -1)
  775.         {
  776.             missionList[index].style.backgroundImage = "none";
  777.             missionList[index].style["background-color"] = missionColors[colorIndex] + transparency;
  778.             colorSet = true;
  779.             return true;
  780.         }
  781.         else if (page == "mission" && colorIndex != -1)
  782.         {
  783.             $(".mission_header_info")[0].style["background-color"] = missionColors[colorIndex] + transparency;
  784.             //$(".mission_header_info").css("background-color", missionColors[colorIndex]);
  785.             colorSet = true;
  786.             return true;
  787.         }
  788.         else
  789.         {
  790.             return false;
  791.         }
  792.     }
  793.  
  794.     //Colorize Mission by its City
  795.     function colorByOrt(page, cityIndex, index, missionList)
  796.     {
  797.         if(page == "map")
  798.         {
  799.             missionList[index].style.backgroundImage = "none";
  800.             missionList[index].style["background-color"] = citiesColors[cityIndex] + transparency;
  801.             colorSet = true;
  802.             return true;
  803.         }
  804.         else if (page == "mission")
  805.         {
  806.             //$(".mission_header_info").css("background-color", citiesColors[cityIndex]);
  807.             $(".mission_header_info")[0].style["background-color"] = citiesColors[cityIndex] + transparency;
  808.             colorSet = true;
  809.             return true;
  810.         }
  811.         return false;
  812.     }
  813.  
  814.     function colorByZip(page, zip, index, missionList)
  815.     {
  816.         var found = false;
  817.         for(var i = 0; i < zips.length; i++)
  818.         {
  819.             for(var j = 0; j < zips[i].length; j++)
  820.             {
  821.                 if(zips[i][j] == zip)
  822.                 {
  823.                     colorIndex = i;
  824.                     found = true;
  825.                     break;
  826.                 }
  827.             }
  828.             if(found)
  829.             {
  830.                 break;
  831.             }
  832.         }
  833.         if(!found)
  834.         {
  835.             colorIndex = -1;
  836.         }
  837.  
  838.         if(page == "map" && colorIndex != -1)
  839.         {
  840.             missionList[index].style.backgroundImage = "none";
  841.             missionList[index].style["background-color"] = zipColors[colorIndex] + transparency;
  842.             colorSet = true;
  843.             return true;
  844.         }
  845.         else if (page == "mission" && colorIndex != -1)
  846.         {
  847.             $(".mission_header_info")[0].style["background-color"] = zipColors[colorIndex] + transparency;
  848.             //$(".mission_header_info").css("background-color", zipColors[colorIndex]);
  849.             colorSet = true;
  850.             return true;
  851.         }
  852.         else
  853.         {
  854.             return false;
  855.         }
  856.     }
  857.  
  858.     //Find Ort who's name is similar to in City List
  859.     function findSimilarOrt(ort)
  860.     {
  861.         for(var i = 0; i < cities.length; i++)
  862.         {
  863.             if(ort.includes(cities[i]))
  864.             {
  865.                 var cityIndex = i;
  866.                 return cityIndex;
  867.             }
  868.         }
  869.         return -1;
  870.     }
  871.  
  872.  
  873.     //Create Nav Bar Button
  874.     createButton();
  875.     //Check if Mission List needs to be colored
  876.     //or Mission Header
  877.     if(currentPage == "/")
  878.     {
  879.         colorizeMap();
  880.         var mutationObserver = new MutationObserver(function(mutations) {
  881.             mutations.forEach(function(mutation) {
  882.                 colorizeMap();
  883.             });
  884.         });
  885.         mutationObserver.observe($("#mission_list")[0], {
  886.             childList: true
  887.         });
  888.         mutationObserver.observe($("#mission_list_krankentransporte")[0], {
  889.             childList: true
  890.         });
  891.         mutationObserver.observe($("#mission_list_sicherheitswache")[0], {
  892.             childList: true
  893.         });
  894.     }
  895.     else
  896.     {
  897.         colorizeMission();
  898.     }
  899.  
  900. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement