Raw Source
Jekyll / pm_ColorHashMenu

// ==UserScript==
// @id              465249
// @name            pm_ColorHashMenu
// @version         0.6
// @namespace       samenhaus.de
// @author          Daniel Jackel <dev@0x4a.net> http://0x4a.net/
// @description     Color the PlentyMarkets menu at the top according to hostname. Hide purchasing prices of items.
// @include         */plenty/ui/*
// @require         https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// @require         https://gist.github.com/raw/2625891/waitForKeyElements.js
// @icon            https://camo.githubusercontent.com/5509efbef113affa6671254a6a4c65c14052be10/687474703a2f2f692e696d6775722e636f6d2f4f746c74585a312e706e67
// @downloadURL     https://openuserjs.org/install/Jekyll/PlentyMarkets_ColorHashMenu.user.js

// @run-at          document-end
// @grant           GM_getValue
// @grant           GM_setValue
// @grant           GM_notification
// @grant           GM_addStyle
// ==/UserScript==
const NAME = "pm_ColorHashMenu";
const VERSION = "0.5b";

this.$ = this.jQuery = jQuery.noConflict(true);
(function(){
    var firstrun = 1,
        toggle = false,
        origTitle = document.title,
        color, color_menu, color_border, color_font, interval;
    waitLogin();

    function waitLogin(){
      console.log(NAME + " waiting for login..." + (firstrun ? "" : " again!"));  
      firstrun = 0;
      waitForKeyElements (".LogoutLabel", colorMenuBar);
    }

    function hashCode(str) {                                                    // https://stackoverflow.com/a/3426956
        var hash = 0;
        for (var i = 0; i < str.length; i++) {
           hash = str.charCodeAt(i) + ((hash << 5) - hash);
        }
        return hash;
    }

    function intToRGB(i) {
        return ((i>>16)&0xFF).toString(16) + 
               ((i>>8)&0xFF).toString(16) + 
               (i&0xFF).toString(16);
    }

    function shadeColor(color, percent) {                                       // https://stackoverflow.com/a/13542669
        var num = parseInt(color,16),
        amt = Math.round(2.55 * percent),
        R = (num >> 16) + amt,
        G = (num >> 8 & 0x00FF) + amt,
        B = (num & 0x0000FF) + amt;
        return (0x1000000 + (R<255?R<1?0:R:255)*0x10000 + (G<255?G<1?0:G:255)*0x100 + (B<255?B<1?0:B:255)).toString(16).slice(1);
    }

    function contrastingColor(color)                                            // https://stackoverflow.com/a/6511606
    {
        return (luma(color) >= 165) ? '000' : 'fff';
    }
    function luma(color) // color can be a hx string or an array of RGB values 0-255
    {
        var rgb = (typeof color === 'string') ? hexToRGBArray(color) : color;
        return (0.2126 * rgb[0]) + (0.7152 * rgb[1]) + (0.0722 * rgb[2]); // SMPTE C, Rec. 709 weightings
    }
    function hexToRGBArray(color)
    {
        if (color.length === 3)
            color = color.charAt(0) + color.charAt(0) + color.charAt(1) + color.charAt(1) + color.charAt(2) + color.charAt(2);
        else if (color.length !== 6)
            throw('Invalid hex color: ' + color);
        var rgb = [];
        for (var i = 0; i <= 2; i++)
            rgb[i] = parseInt(color.substr(i * 2, 2), 16);
        return rgb;
    }
    
    function watchTitle (active = true) {
      if (active) {
        var newTitle;
        interval = setInterval( function() {
          hideItems();
          hideOrders();
          
        /*
          newTitle = document.title;
          if(origTitle != newTitle) {
            origTitle = document.title;
            console.log("titlechange: " + origTitle + " --> " + newTitle);
            if (newTitle.includes("Artikel bearbeiten:") {
              alert("artikel");
            }
            else if (newTitle.includes("Aufträge") {
              alert("aufträge");
            }
          

          } */
        }, 500);
      }
      else 
        clearInterval(interval);
    }
    function hideItems (active = true) {
      console.log("hide items");
      if (active)
        $(".ItemDetailItemView").find("div.Content").find("div.PlentyConfigTable").find(".PlentyConfigTableRow:contains('EK (netto)')").eq(2).fadeOut();
      else
        $(".ItemDetailItemView").find("div.Content").find("div.PlentyConfigTable").find(".PlentyConfigTableRow:contains('EK (netto)')").eq(2).fadeIn();
    }
    function hideOrders (active = true) {
      console.log("hide orders");
      // orders: get orderID
      var orderid = $(".gwt-TabLayoutPanelTab-selected").eq("2").text();
      // select article table
      var $items = $("iframe.PlentyTabContentInner").contents().find("#itemPositionContainerPaneId" + orderid + " > table");
      // get column to hide
      var marginindex = $items.find("thead").find("th:contains('Gewinnspanne')").index() + 1;
      // select overview table
      var $overview = $("iframe.PlentyTabContentInner").contents().find("#overviewPaneTable" + orderid + " > tbody").children().eq(1);
      
      if (active) {
        // hide column
        $items.find("tr > td:nth-child("+marginindex+")").fadeOut();
        $items.find("tr > th:nth-child("+marginindex+")").fadeOut();
        // blank boxes
        $overview.find("td:nth-child(3)").children().css("color", "transparent");
        $overview.find("th:nth-child(3)").children().css("color", "transparent");
      }
      else {
        // show column
        $items.find("tr > td:nth-child("+marginindex+")").fadeIn();
        $items.find("tr > th:nth-child("+marginindex+")").fadeIn();
        // show boxes
        $overview.find("td:nth-child(3)").children().css("color", "#000");
        $overview.find("th:nth-child(3)").children().css("color", "#222");
      }
    }
    function bossMode (active) {
      if (active) {
        $("li#pwnbutton").css("background", "#" + color_menu);
        watchTitle();
        hideItems();
        hideOrders();
      }
      else {
        $("li#pwnbutton").css("background", "none");
        watchTitle(0);
        hideItems(0);
        hideOrders(0);
      }
    }
    
    function toggleMode () {
      if (!toggle)
        bossMode(1);
      else
        bossMode(0);
      toggle = !toggle;
    }
    
    function colorMenuBar (jNode) {
        console.log(NAME + " " + VERSION + " started! (jQuery " + $().jquery + ")");  

        color = intToRGB(hashCode(window.location.hostname));
        color_menu = shadeColor(color, -20);
        color_border = shadeColor(color_menu, -20);
        color_font = contrastingColor(color_menu);

        console.log("main color for " + window.location.hostname + " is #" + color_menu);

        // menu background
        GM_addStyle('.horizontalNavigationWrapper, .PlentyMainMenuBar.gwt-MenuBar-vertical .gwt-MenuItem-selected, .PlentyMainMenuBar.gwt-MenuBar-vertical tr:hover > td, .PlentyMainMenuBar.gwt-MenuBar-vertical .gwt-MenuItem-selected + .subMenuIcon-selected {background-color: #' + color_menu + ' !important}');

        // menu border
        GM_addStyle('.horizontalNavigationWrapper, .PlentyVerticalNavigationWrapper > .adminMainIconNavi {border-color: #' + color_border + ' !important}');
        
        // vertical menu border
        GM_addStyle('.gwt-MenuBar-vertical.PlentyMainMenuBar {border: 1px solid #' + color_menu + ' !important}');
        
        // menu font
        GM_addStyle('.PlentyMainMenuBar.gwt-MenuBar-horizontal .gwt-MenuItem {color: #' + color_font + ' !important}');

        // add hostname
        $('.horizontalNavigationWrapper').append('<div class="HostnamePanel" style="position: relative; overflow: hidden;"><a href="' + location.protocol + "//" + location.hostname + '" target="_blank">' + window.location.hostname.replace('www.','') + '</a></div>');

        // hostname link
        GM_addStyle('.HostnamePanel {cursor: default; color: #' + color_font + '; float: right; margin-right: 16px; margin-top: 7px; font-size: 13px; letter-spacing: 0.2em; }');
        GM_addStyle('.HostnamePanel a {text-decoration: none; text-shadow: 0 0 .4px #' + color_font + '; color: transparent; }');
        GM_addStyle('.HostnamePanel a:hover {color: #' + color_font + '; -webkit-transition: 0.5s; -moz-transition: 0.5s; -o-transition: 0.5s; -ms-transition: 0.5s; transition: 0.5s; }');

        
        
        drawButton(color_menu, color_border);
        console.log("added button!");

        waitForKeyElements (".adminLoginBox", waitLogin);
    }
    

    
    function drawButton() {
      var contentWinWrapper = new XPCNativeWrapper(content);
      $("ul#PlentyAdminIcons").append(XPCNativeWrapper.unwrap($("<li id='pwnbutton'> <div class='PlentyAdminIcon' tabindex='0' style='background: url(/layout/cyt/img/favicon.ico) no-repeat scroll 7px 2px transparent; width: 52px;'><input style='opacity: 0; height: 1px; width: 1px; z-index: -1; overflow: hidden; position: absolute;' role='presentation' tabindex='-1' type='text'></div></li>"))).delay(50);
      $("li#pwnbutton").css({
        "background": "none",
        "border": "none"
      });
      $("li#pwnbutton").hover( function(){
        $(this).css({
          "border-left": "1px solid #" + color_border,
          "border-right": "1px solid #" + color_border
        });
      },
      function(){
        $(this).css({
          "border-left": "1px solid transparent",
          "border-right": "1px solid transparent"
        });
      });
      
      $("li#pwnbutton").click(function(){ toggleMode(); }); 
      
      $("li#pwnbutton").css({
          "border-left": "1px solid transparent",
          "border-right": "1px solid transparent"
      });
      return;
    }
})();