Advertisement
eter

Discord Toggle Channels Bar

Nov 15th, 2020
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Discord Toggle Channels Bar
  3. // @namespace    https://discordapp.com
  4. // @version      2.0
  5. // @description  Adds show/hide channels sidebar button to Discord Web App
  6. //               forked from https://github.com/mindovermiles262/discord-toggle-channels
  7. // @author       v2.0 modified by Eter#3834
  8. // @match        https://discord.com/*
  9. // @grant        none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13.     'use strict';
  14.  
  15.     // Classes of DIVs you want to be able to toggle sidebar
  16.     const toggleButtons = [
  17.         "children-19S4PO",
  18.         "toggleChannelsBtn"
  19.     ];
  20.     const columnToHide = "sidebar-2K8pFh"
  21.     const showHideSidebarButtonParentClass = "scroller-2TZvBN";
  22.     //const showHideSidebarButtonParentClass = "wrapper-1Rf91z"; //top bar instead
  23.     const roomDivClass = "containerDefault-1ZnADq";
  24.     const unreadClass = "unread-3zKkbm";
  25.     const channelsWidth = "240px";
  26.  
  27.     function sleep(ms) {
  28.         return new Promise(resolve => setTimeout(resolve, ms));
  29.     }
  30.  
  31.     async function pageload() {
  32.         // Wait 5 seconds for page to load
  33.         await sleep(5000);
  34.         main()
  35.     }
  36.  
  37.     const toggleVisibility = function() {
  38.         let sidebar = document.getElementsByClassName(columnToHide)[0];
  39.         sidebar.style.transition = "width 150ms ease-out";
  40.         if (sidebar.style.width == "1px") {
  41.             sidebar.style.width = channelsWidth;
  42.         } else {
  43.             sidebar.style.width = "1px";
  44.         }
  45.     }
  46.  
  47.     let cheveronDirection = function() {
  48.         const sidebar = document.getElementsByClassName(columnToHide)[0]
  49.         const btn = document.getElementById("dtcb-cheverons")
  50.         if (sidebar.style.width == "1px") {
  51.             btn.innerText = "β€Ί"; // symbol for >
  52.         } else {
  53.             btn.innerText = "β€Ή"; // symbol for <
  54.         }
  55.     }
  56.  
  57.     const createSidebarButton = function() {
  58.         let btnDiv = document.createElement("div");
  59.         btnDiv.setAttribute("class", "toggleChannelsBtn");
  60.         btnDiv.setAttribute("id", "toggleChannelsBtn");
  61.         btnDiv.style.width = "70px";
  62.         btnDiv.style.color = "#FFFFFF";
  63.         btnDiv.style.position = "absolute";
  64.         btnDiv.style.top = "0";
  65.         let btn = document.createElement("p");
  66.         btn.setAttribute("id", "dtcb-cheverons");
  67.         btn.innerText = "β€Ή"; // symbol for <
  68.         btn.style.margin = "0";
  69.         btn.style.cursor = "pointer";
  70.         //btn.style.backgroundColor = "#36393f";
  71.         btn.style.color = "#8e9297";
  72.         btn.style.display = "inline-flex";
  73.         btn.style.padding = "0px 6px 4px";
  74.         btn.style.justifyContent = "center";
  75.         btn.style.borderBottomRightRadius = "10px";
  76.         btn.style.fontSize = "15px";
  77.         btnDiv.appendChild(btn);
  78.         return btnDiv;
  79.     }
  80.  
  81.     const addListenersToToggleButtons = function() {
  82.         toggleButtons.forEach(function(elem) {
  83.             document.getElementsByClassName(elem)[0].addEventListener('click', function() {
  84.                 toggleVisibility();
  85.                 cheveronDirection();
  86.             })
  87.         });
  88.     }
  89.  
  90.     const autohideSidebar = function() {
  91.         const roomDivs = document.getElementsByClassName(roomDivClass)
  92.         Array.from(roomDivs).forEach(function(room) {
  93.             room.addEventListener('click', function() {
  94.                 toggleVisibility();
  95.                 cheveronDirection();
  96.             })
  97.         })
  98.     }
  99.  
  100.  
  101.  
  102.     const main = function() {
  103.         console.log("[*] Loading Discord Toggle Channenels Bar Userscript");
  104.         const newBtn = createSidebarButton()
  105.         document.getElementsByClassName(showHideSidebarButtonParentClass)[0].appendChild(newBtn)
  106.         addListenersToToggleButtons();
  107.         autohideSidebar();
  108.     }
  109.  
  110.     pageload();
  111. })();
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement