losipiuk / Github hide green checks

// ==UserScript==
// @name         Github hide green checks
// @namespace    http://losipiuk.github.io/
// @version      0.5
// @description  Allow hiding boring checks on github PR
// @author       losipiuk
// @license      MIT
// @match        https://github.com/*/*/pull/*
// @match        https://github.com/*/*/actions/runs/*
// @match        https://github.com/*/*/runs/*
// @match        https://github.com/pulls
// @grant        none
// @require      http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==

(function () {
  'use strict';

  console.log('Installing "Hide Green Checks"');

  var greenChecksHidden = true;

  // crate CSS class
  $("<style type='text/css'> .check_hidden{ display: none !important;} </style>").appendTo("head");

  function determinePage() {
      if (window.location.href.indexOf("/runs/") != -1) {
          return "runs";
      }
      if (window.location.href.indexOf("/pull/") != -1) {
          return "pull";
      }
      return "other"
  }

  function addButtonIfNeeded() {
     switch(determinePage()) {
         case "pull":
             addButtonIfNeededPull();
             break;
         case "runs":
             addButtonIfNeededRuns();
             break;
         case "other":
             break;
     }

    jQuery('#_f_hide_green_checks').click(hideGreenChecks);
    jQuery('#_f_show_green_checks').click(showGreenChecks);

    ensureButtonVisibility();
  }

  function addButtonIfNeededPull() {
    // make status list higher
    jQuery('.branch-action-item.open > .merge-status-list-wrapper > .merge-status-list, .branch-action-item.open > .merge-status-list').css("max-height", "1600px");

    if (jQuery("#_f_hide_green_checks").length > 0) {
      return
    }

    var box = jQuery("span:contains('Hide all checks')").last().parent().parent();
    box.append('<a id="_f_hide_green_checks" class="btn-link float-right" style="display: none;">Hide green checks</a>');
    box.append('<a id="_f_show_green_checks" class="btn-link float-right" style="display: none;">Show green checks</a>');

    jQuery('#_f_hide_green_checks').click(hideGreenChecks);
    jQuery('#_f_show_green_checks').click(showGreenChecks);

    ensureButtonVisibility();
  }

  function addButtonIfNeededRuns() {
    if (jQuery("#_f_hide_green_checks").length > 0) {
      return
    }

    var box = jQuery("div.row:contains('Jobs')").last();

    box.append('&nbsp;&nbsp;<a id="_f_hide_green_checks" class="btn-link float-right" style="display: none;">Hide green checks</a>');
    box.append('&nbsp;&nbsp;<a id="_f_show_green_checks" class="btn-link float-right" style="display: none;">Show green checks</a>');

  }

  function ensureButtonVisibility() {
    if (!greenChecksHidden) {
      jQuery("#_f_hide_green_checks").show();
      jQuery("#_f_show_green_checks").hide();
    }
    else {
      jQuery("#_f_hide_green_checks").hide();
      jQuery("#_f_show_green_checks").show();
    }
  }

  function hideGreenChecks() {
    greenChecksHidden = true;
    ensureButtonVisibility();
  }

  function showGreenChecks() {
    greenChecksHidden = false;
    ensureButtonVisibility();
  }

  function enforceGreenCheckHidden() {
     switch(determinePage()) {
         case "pull":
             enforceGreenCheckHiddenPull();
             break;
         case "runs":
             enforceGreenCheckHiddenRuns();
             break;
         case "other":
             break;
     }
  }

  function enforceGreenCheckHiddenPull() {
    var actionRuns = jQuery(".merge-status-item > .merge-status-icon > .color-fg-success.octicon-check");
    console.log("Found " + actionRuns.length + " green checks");
    if (greenChecksHidden) {
      actionRuns.parent().parent().addClass("check_hidden")
    }
    else {
      actionRuns.parent().parent().removeClass("check_hidden")
    }
  }

  function enforceGreenCheckHiddenRuns() {
    var actionRuns = jQuery("li[data-test-selector=check_run_list_item] > div > a > span > div >svg.color-fg-success")
      .parent().parent().parent().parent();
    console.log("Found " + actionRuns.length + " green checks");
    if (greenChecksHidden) {
      actionRuns.addClass("check_hidden")
    }
    else {
      actionRuns.removeClass("check_hidden")
    }
  }

  determinePage();
  addButtonIfNeeded()
  window.setInterval(addButtonIfNeeded, 200);
  window.setInterval(enforceGreenCheckHidden, 200);

})();