PR Files Collapser

It collapses files on Github PR view

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         PR Files Collapser
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  It collapses files on Github PR view
// @author       Alex. U
// @match        https://github.com/*
// @require      https://code.jquery.com/jquery-2.1.4.min.js
// @grant        none
// @noframes
// ==/UserScript==

var filesLandingPage = () => {
    if (window.location.href.includes('files')) {
        renderCollapseAll()
    }
}

var setListener = () => {
  $(document).on('pjax:success', function(event, data, status, xhr, options) {
    if (window.location.href.match(/https:\/\/github.com\/[\D]+\/[\d]+\/files/g)) {
      renderCollapseAll()
    }
  });
}

var renderCollapseAll = () => {
    var zNode = document.createElement ('details')
    zNode.innerHTML = '<summary id="collapseAll" class="btn btn-sm" style="margin-left: 20px;"> Collapse all</summary>'

    $('.pr-review-tools')[0].append(zNode)
    $('.pr-review-tools').css('display', 'flex')

    document.getElementById('collapseAll').addEventListener('click', (event) => {
        document.querySelectorAll('button.js-details-target').forEach(button => button.click())
    })
}

(() => {
    console.info("Github Files Collapser Injected")
    filesLandingPage()
    setListener()
})()