sammcj / GitLab Toggle Activity Information

// ==UserScript==
// @name         GitLab Toggle Activity Information
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Allow to toggle the issue and merge request informations to show only the discussion and comments. Add a sumarry on Wiki pages
// @author       Sam McLeod
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js
// @icon         https://gitlab.com/assets/gitlab_logo-7ae504fe4f68fdebb3c2034e36621930cd36ea87924c11ff65dbcb8ed50dca58.png
// @match        https://gitlab.com/*
// @match        https://gitlab.*/*
// @copyright    2018, sammcj (https://openuserjs.org//users/sammcj)
// @license      GPL-2.0-or-later
// This is a fork of https://greasyfork.org/en/scripts/36251-gitlab-essential
// ==/UserScript==

/* jshint ignore:start */
var inline_src = (<><![CDATA[
/* jshint ignore:end */
    /* jshint esnext: false */
    /* jshint esversion: 6 */

    function addGlobalStyle(css) {
      var head, style;
      head = document.getElementsByTagName('head')[0];
      if (!head) { return; }
      style = document.createElement('style');
      style.type = 'text/css';
      style.innerHTML = css;
      head.appendChild(style);
    }

    function createToggleButton () {
      const button = document.createElement("button");
      button.id = "toggle-system-notes";
      button.title = "Toggle activity information";
      button.classList = "award-control btn has-tooltip";
      button.type = "button";
      const glEmoji = document.createElement("gl-emoji");
      glEmoji.style.margin = 0;
      const emoji = document.createTextNode("ℹ️");
      glEmoji.appendChild(emoji);
      button.appendChild(glEmoji);
      return button;
    }

    function initButton () {
      document.querySelector("#toggle-system-notes").addEventListener("click", function() {
        if (this.getAttribute("system-notes-hided") === "true") {
          this.setAttribute("system-notes-hided", false);
          document.querySelectorAll(".system-note").forEach(function(systemNote) {
            systemNote.style.display = "block";
          });
        } else {
          this.setAttribute("system-notes-hided", true);
          document.querySelectorAll(".system-note").forEach(function(systemNote) {
            systemNote.style.display = "none";
          });
        }
      });
    }

    let isWikiPage = document.querySelectorAll('.wiki-sidebar').length;
    let isIssuePage = document.querySelectorAll('.new-branch-col').length;
    let isMRPage = document.querySelectorAll('.merge-request-tabs-container').length;

    if (isWikiPage) {
      const sidebar = document.querySelector('.sidebar-container');
      const firstChild = document.querySelector('.sidebar-container').childNodes[0];

      const summaryWrapper = document.createElement('div');
      summaryWrapper.classList = 'summary-wrapper';
      sidebar.insertBefore(summaryWrapper, firstChild);

      const summaryTitle = document.createElement('h1');
      summaryTitle.innerHTML = 'Sommaire';
      summaryWrapper.appendChild(summaryTitle);

      const titles = document.querySelectorAll('.wiki h1, .wiki h2, .wiki h3, .wiki h4, .wiki h5, .wiki h6');
      titles.forEach(function(title) {
        var summaryLink = title.firstElementChild.cloneNode(true);
        summaryLink.innerHTML = title.textContent;
        summaryLink.classList = title.tagName.toLowerCase();
        summaryWrapper.appendChild(summaryLink);
      });

      addGlobalStyle('.summary-wrapper { margin-bottom: 10px; padding: 0 16px 1vh; border-bottom: 1px solid #e8e8e8; }');
      addGlobalStyle('.summary-wrapper h1 { margin-top: 1vh; }');
      addGlobalStyle('.summary-wrapper a { display: block;font-size: 1em; margin: 10px; }');
      addGlobalStyle('.summary-wrapper a.h2 { padding-left: 10px; }');
      addGlobalStyle('.summary-wrapper a.h3 { padding-left: 20px; }');
      addGlobalStyle('.summary-wrapper a.h4 { padding-left: 30px; }');
      addGlobalStyle('.summary-wrapper a.h5 { padding-left: 40px; }');
      addGlobalStyle('.summary-wrapper a.h6 { padding-left: 50px; }');
    }

    if (isIssuePage) {
      const container = document.createElement("button");
      container.classList = "col-sm-1";
      const button = createToggleButton();
      container.appendChild(button);
      const newBranchDiv = document.querySelector(".new-branch-col");
      newBranchDiv.classList.remove("col-sm-4");
      newBranchDiv.classList.add("col-sm-3");
      newBranchDiv.after(button);
      initButton();
    }

    if (isMRPage) {
      const button = createToggleButton();
      const mrDiv = document.querySelector(".merge-request-tabs-container");
      mrDiv.appendChild(button);
      initButton();
    }

/* jshint ignore:start */
]]></>).toString();
var c = Babel.transform(inline_src, { presets: [ "es2015", "es2016" ] });
eval(c.code);
/* jshint ignore:end */