Ansolley

Plugin - Favorites Sorter (Broken on HTTPS)

May 10th, 2019
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 17.80 KB | None | 0 0
  1. /**
  2. * Categorized Favorites for BF4 and BFH - Categorize your favorites
  3. *
  4. * @author I-MrFixIt-I
  5. * @version 1.0
  6. * @url https://getbblog.com/
  7. */
  8.  
  9. // initialize your plugin
  10. BBLog.handle("add.plugin", {
  11.  
  12.     /**
  13.     * The unique, lowercase id of my plugin
  14.     * Allowed chars: 0-9, a-z, -
  15.     */
  16.     id : "mrfixit-categorized-favorites",
  17.  
  18.     /**
  19.     * The name of my plugin, used to show config values in bblog options
  20.     * Could also be translated with the translation key "plugin.name" (optional)
  21.     *
  22.     * @type String
  23.     */
  24.     name : "Categorized Favorites",
  25.  
  26.     /**
  27.     * Some translations for this plugins
  28.     * For every config flag must exist a corresponding EN translation
  29.     *   otherwise the plugin will no be loaded
  30.     *
  31.     * @type Object
  32.     */
  33.     translations : {
  34.         "en" : {
  35.             "use.categorized-favorites" : "Use Categorized Favorites",
  36.            
  37.             "create-folder-message" : "Which category you want to add?",
  38.             "remove-folder-message" : "Which category you want to remove?",
  39.            
  40.             "create-folder" : "Add category",
  41.             "delete-folder" : "Remove category",
  42.            
  43.             "add-to-folder" : "Add to %s",
  44.             "remove-from-folder" : "Remove from %s"
  45.         },
  46.         "de" : {
  47.             "use.categorized-favorites" : "Categorized Favorites verwenden",
  48.            
  49.             "create-folder-message" : "Welche Kategorie möchtest du hinzufügen?",
  50.             "remove-folder-message" : "Welche Kategorie möchtest du entfernen?",
  51.            
  52.             "create-folder" : "Kategorie hinzufügen",
  53.             "delete-folder" : "Kategorie entfernen",
  54.            
  55.             "add-to-folder" : "Zu %s hinzufügen",
  56.             "remove-from-folder" : "Von %s entfernen"
  57.         }
  58.     },
  59.  
  60.     /**
  61.     * Configuration Options that appears in the BBLog Menu
  62.     * Every option must be an object with properties as shown bellow
  63.     * Properties available:
  64.     *   key : The name for your config flag - The user can toggle this option
  65.     *         and you can retreive the users choice with instance instance.storage(YOUR_KEY_NAME) (0 or 1 will be returned)
  66.     *   init : Can be 0 or 1 - Represent the initial status of the option when the user load the plugin for the first time
  67.     *          If you want that this option is enabled on first load (opt-out) than set it to 1, otherwise to 0 (opt-in)
  68.     *   handler(optional): When set as a function this config entry turns into a button (like the plugins button you see in the bblog menu)
  69.     *                       The function well be executed when the user clicks the button
  70.     */
  71.     configFlags : [
  72.         {"key" : "use.categorized-favorites", "init" : 1}
  73.     ],
  74.  
  75.     // get the game from the url => bf3, bf4, bfh, or mohw
  76.     getGameFromURL : function(){
  77.         var prefix = "http://battlelog.battlefield.com/";
  78.         var substring = window.location.href.substring(prefix.length);
  79.         var index = substring.indexOf("/");
  80.         return substring.substring(0, index);
  81.     },
  82.  
  83.     // get all saved folders from storage
  84.     getFolderList : function(instance){
  85.         var game = instance.getGameFromURL() + ".";
  86.         return JSON.parse(instance.storage(game + "folders"));
  87.     },
  88.    
  89.     // save folderList in storage
  90.     setFolderList : function(instance, folderList){
  91.         var game = instance.getGameFromURL() + ".";
  92.         instance.storage(game + "folders", JSON.stringify(folderList));
  93.     },
  94.    
  95.     // add folder to folderList
  96.     addFolder : function(instance, folder){
  97.         var folderList = instance.getFolderList(instance);
  98.         if (folderList === null)
  99.         {
  100.             var tmp = [];
  101.             tmp[0] = folder;
  102.             instance.setFolderList(instance, tmp);
  103.         }
  104.         else if (folderList.indexOf(folder) == -1)
  105.         {
  106.             folderList.push(folder);
  107.             instance.setFolderList(instance, folderList);
  108.         }
  109.     },
  110.    
  111.     // remove folder from folderList
  112.     removeFolder : function(instance, folder){
  113.         var game = instance.getGameFromURL() + ".";
  114.         instance.storage(game + folder, null); // delete folder and all containing server from storage
  115.        
  116.         var folderList = instance.getFolderList(instance);
  117.         if (folderList !== null)
  118.         {
  119.             var folderIndex = folderList.indexOf(folder);
  120.             if (folderIndex > -1)
  121.             {
  122.                 folderList.splice(folderIndex, 1); // delete folder from array
  123.                 instance.setFolderList(instance, folderList);
  124.             }
  125.         }
  126.     },
  127.  
  128.     // get all saved server from folder from storage
  129.     getServerList : function(instance, folderName){
  130.         var game = instance.getGameFromURL() + ".";
  131.         return JSON.parse(instance.storage(game + folderName));
  132.     },
  133.    
  134.     // save serverList in folder in storage
  135.     setServerList : function(instance, folderName, serverList){
  136.         var game = instance.getGameFromURL() + ".";
  137.         instance.storage(game + folderName, JSON.stringify(serverList));
  138.     },
  139.    
  140.     // add server to serverList in folder
  141.     addServer : function(instance, folder, server){
  142.         var serverList = instance.getServerList(instance, folder);
  143.         if (serverList === null)
  144.         {
  145.             var tmp = [];
  146.             tmp[0] = server;
  147.             instance.setServerList(instance, folder, tmp);
  148.         }
  149.         else if (serverList.indexOf(server) == -1)
  150.         {
  151.             serverList.push(server);
  152.             instance.setServerList(instance, folder, serverList);
  153.         }
  154.     },
  155.    
  156.     // remove server from serverList in folder
  157.     removeServer : function(instance, folder, server){
  158.         var serverList = instance.getServerList(instance, folder);
  159.         if (serverList !== null)
  160.         {
  161.             var serverIndex = serverList.indexOf(server);
  162.             if (serverIndex > -1)
  163.             {
  164.                 serverList.splice(serverIndex, 1); // delete server from array
  165.                 instance.setServerList(instance, folder, serverList);
  166.             }
  167.         }
  168.     },
  169.    
  170.     // get the currently active data-guid from the "join server" button
  171.     getActiveServerGUID : function(){
  172.         return $("#favorites .server-info .action-buttons-container button").attr("data-guid");
  173.     },
  174.    
  175.     // create folder in storage and add to page
  176.     // this function gets called by clicking the "Create folder" button
  177.     createFolder_onClick : function(instance){
  178.         var folderName = prompt(instance.t("create-folder-message")); // open prompt and let use choose a folder name
  179.         if (folderName !== null) { // if the prompt was successfully
  180.             instance.addFolder(instance, folderName); // add the foldername to the folderlist in storage
  181.            
  182.             // if the "add-to-folder" div element isn't created until now, create it now
  183.            if (!$("#add-to-folder").length)
  184.            {
  185.                var folderHTML = "<div id='add-to-folder' style='margin-top: 9px;'></div>";
  186.                $(folderHTML).insertAfter($("#favorites .server-info .action-buttons-container"));
  187.            }
  188.            
  189.            // append the new created folder to the page and add the onClick function
  190.            $("#add-to-folder").append("<button data-folder-name=" + folderName + " class='btn btn-secondary btn-tiny btn-block arrow' style='margin-top: -1px;'>" + instance.t("add-to-folder").replace("%s", folderName) + "</button>").click(function() {
  191.                instance.addServerToFolder_onClick(instance, this);
  192.            });
  193.            
  194.            // if the "favorites-folder" div element isn't created until now, create it now
  195.             if (!$("#favorites-folder").length)
  196.             {
  197.                 var folderMenuHTML = "<nav id='favorites-folder' class='submenu spacing-bottom-small'><ul>";
  198.                 $(folderMenuHTML).insertAfter($("#serverbrowser > nav[class*='submenu']"));
  199.             }
  200.             // append the new created menu item to the page and add the onClick function
  201.             $("#favorites-folder > ul").append( "<li data-folder-name='" + folderName + "'><a>" + folderName + "</a></li>").click(function() {
  202.                 instance.openFolder_onClick(instance, this);
  203.             });
  204.         }
  205.     },
  206.    
  207.     // remove folder and all it's containing server from storage and remove it from page
  208.    // this function gets called by clicking the "Delete folder" button
  209.    deleteFolder_onClick : function(instance){
  210.        var folderName = prompt(instance.t("create-folder-message")); // open prompt and let use choose a folder name
  211.        if (folderName !== null) { // if the prompt was successfully
  212.            instance.removeFolder(instance, folderName);
  213.            
  214.            $("#add-to-folder button[data-folder-name='" + folderName + "']").remove();
  215.            $("#favorites-folder ul li[data-folder-name='" + folderName + "']").remove();
  216.        }
  217.    },
  218.    
  219.    openFolder_onClick : function(instance, element){
  220.        $("#favorites-folder ul li[class='active']").removeClass("active"); // remove from all menu items the active class...
  221.        $(element).addClass("active"); // ... and add it to the clicked element
  222.        
  223.        var folderName = $(element).attr("data-folder-name");
  224.        var serverList = instance.getServerList(instance, folderName);
  225.        if (serverList !== null && serverList.length > 0)
  226.        {
  227.            $("#serverbrowser-results tbody tr").css("display", ""); // make all server visible
  228.            $.each($("#serverbrowser-results tbody tr"), function() { // iterate through each server...
  229.                if (serverList.indexOf($(this).attr("data-guid")) == -1) // and if the server isn't in the folder...
  230.                 {
  231.                     $(this).css("display", "none"); // make it invisible again
  232.                 }
  233.             });
  234.         }
  235.         else
  236.         {
  237.             $("#serverbrowser-results tbody tr").css("display", "none"); // make all server visible
  238.         }
  239.        
  240.         $("#serverbrowser-results tbody tr:visible:first").click(); // click the first visible server, battlelog will now refresh the server info on the right side
  241.     },
  242.    
  243.     closeFolder_onClick : function(){
  244.         $("#favorites-folder ul li[class='active']").removeClass("active"); // remove from all menu items the active class...
  245.         $("#serverbrowser-results tbody tr").css("display", ""); // make all server visible
  246.         $("#serverbrowser-results tbody tr:visible:first").click(); // click the first visible server, battlelog will now refresh the server info on the right side
  247.     },
  248.    
  249.     // add server to folder in storage
  250.     // this function gets called by clicking the "Add to ..." button
  251.     addServerToFolder_onClick : function(instance, element){
  252.         var folderName = $(element).attr("data-folder-name");
  253.         var guid = instance.getActiveServerGUID();
  254.         instance.addServer(instance, folderName, guid);
  255.        
  256.         $(element).attr("data-action", "remove-from-folder");
  257.        
  258.         $(element).text(instance.t("remove-from-folder").replace("%s", folderName));
  259.         if ($("#favorites-folder ul li[class='active'] a").text() == folderName)
  260.         {
  261.             $("#serverbrowser-results tbody tr[data-guid='" + guid + "']").css("display", ""); // make it invisible again
  262.         }
  263.     },
  264.    
  265.     // remove server from folder in storage
  266.     // this function gets called by clicking the "Remove from ..." button
  267.     removeServerFromFolder_onClick : function(instance, element){
  268.         var folderName = $(element).attr("data-folder-name");
  269.         var guid = instance.getActiveServerGUID();
  270.         instance.removeServer(instance, folderName, guid);
  271.        
  272.         $(element).attr("data-action", "add-to-folder");
  273.        
  274.         $(element).text(instance.t("add-to-folder").replace("%s", folderName));
  275.         if ($("#favorites-folder ul li[class='active'] a").text() == folderName)
  276.         {
  277.             $("#serverbrowser-results tbody tr[data-guid='" + guid + "']").css("display", "none"); // make it invisible again
  278.         }
  279.     },
  280.  
  281.     /**
  282.     * A trigger that fires everytime when the dom is changing but at max only once each 200ms (5x per second) to prevent too much calls in a short time
  283.     * Example Case: If 10 DOM changes happen in a period of 100ms than this function will only been called 200ms after the last of this 10 DOM changes
  284.     * This make sure that all actions in battlelog been finished before this function been called
  285.     * This is how BBLog track Battlelog for any change, like url, content or anything
  286.     *
  287.     * @param object instance The instance of your plugin which is the whole plugin object
  288.     *    Always use "instance" to access any plugin related function, not use "this" because it's not working properly
  289.     *    For example: If you add a new function to your addon, always pass the "instance" object
  290.     */
  291.     domchange : function(instance){
  292.         if(instance.storage("use.categorized-favorites") && window.location.href.match("/servers/favourites/pc/")){
  293.             var game = instance.getGameFromURL();
  294.             if (game == "bf4" || game == "bfh")
  295.             {
  296.                 if (!$("#folder-management").length)
  297.                 {
  298.                     $("#serverbrowser > header[class='clearfix'").append("<div id='folder-management' class='pull-right'></div>");
  299.                    
  300.                     $("#serverbrowser > header[class='clearfix'] #folder-management").append("<button id='create-folder-button' class='btn btn-secondary btn-medium arrow'>" + instance.t("create-folder") + "</button>");
  301.                     $("#serverbrowser > header[class='clearfix'] #folder-management #create-folder-button").click(function() {
  302.                         instance.createFolder_onClick(instance);
  303.                     });
  304.                    
  305.                     $("#serverbrowser > header[class='clearfix'] #folder-management").append("<button id='delete-folder-button' class='btn btn-secondary btn-medium arrow'>" + instance.t("delete-folder") + "</button>");
  306.                     $("#serverbrowser > header[class='clearfix'] #folder-management #delete-folder-button").click(function() {
  307.                         instance.deleteFolder_onClick(instance);
  308.                     });
  309.                 }
  310.        
  311.                 var folderList = instance.getFolderList(instance);
  312.                 if (folderList !== null)
  313.                 {
  314.                     if (!$("#add-to-folder").length)
  315.                     {
  316.                         var folderHTML = "<div id='add-to-folder' style='margin-top: 9px;'>";
  317.                         var guid = instance.getActiveServerGUID();
  318.                        
  319.                         folderList.forEach(function(folder) {
  320.                             var serverList = instance.getServerList(instance, folder);
  321.                             if (serverList === null || serverList.indexOf(guid) == -1)
  322.                             {
  323.                                 folderHTML += "<button data-folder-name='" + folder + "' data-action='add-to-folder' class='btn btn-secondary btn-tiny btn-block arrow' style='margin-top: -1px;'>" + instance.t("add-to-folder").replace("%s", folder) + "</button>";
  324.                             }
  325.                             else
  326.                             {
  327.                                 folderHTML += "<button data-folder-name='" + folder + "' data-action='remove-from-folder' class='btn btn-secondary btn-tiny btn-block arrow' style='margin-top: -1px;'>" + instance.t("remove-from-folder").replace("%s", folder) + "</button>";
  328.                             }
  329.                         });
  330.            
  331.                         folderHTML += "</div>";
  332.        
  333.                         $(folderHTML).insertAfter($("#favorites .server-info .action-buttons-container"));
  334.        
  335.                         $.each($("#favorites .server-info #add-to-folder button"), function() {
  336.                             $(this).click(function() {
  337.                                 if ($(this).attr("data-action") == "add-to-folder")
  338.                                 {
  339.                                     instance.addServerToFolder_onClick(instance, this);
  340.                                 }
  341.                                 else if ($(this).attr("data-action") == "remove-from-folder")
  342.                                 {
  343.                                     instance.removeServerFromFolder_onClick(instance, this);
  344.                                 }
  345.                             });
  346.                         });
  347.                     }
  348.                    
  349.                     if (!$("#favorites-folder").length)
  350.                     {
  351.                         var folderMenuHTML = "<nav id='favorites-folder' class='submenu spacing-bottom-small'><ul>";
  352.                         folderList.forEach(function(folderName) {
  353.                             folderMenuHTML += "<li data-folder-name='" + folderName + "'><a>" + folderName + "</a></li>";
  354.                         });
  355.            
  356.                         folderMenuHTML += "</ul></nav>";
  357.            
  358.                         $(folderMenuHTML).insertAfter($("#serverbrowser > nav[class*='submenu']"));
  359.                        
  360.                         $.each($("#favorites-folder ul li"), function() {
  361.                             $(this).click(function() {
  362.                                 if (!$(this).hasClass("active"))
  363.                                 {
  364.                                     instance.openFolder_onClick(instance, this);
  365.                                 }
  366.                                 else
  367.                                 {
  368.                                     instance.closeFolder_onClick();
  369.                                 }
  370.                             });
  371.                         });
  372.                     }
  373.                 }
  374.             }
  375.         }
  376.     }
  377. });
Add Comment
Please, Sign In to add comment