Skip to content

Instantly share code, notes, and snippets.

@hacker0limbo
Forked from Jaace/expand-collapse.user.js
Created September 27, 2020 10:53
Show Gist options
  • Select an option

  • Save hacker0limbo/5f57b9ab184eaccdd7cf7dff985a12c8 to your computer and use it in GitHub Desktop.

Select an option

Save hacker0limbo/5f57b9ab184eaccdd7cf7dff985a12c8 to your computer and use it in GitHub Desktop.

Revisions

  1. @Jaace Jaace renamed this gist Feb 13, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Jason Boyle revised this gist Mar 12, 2017. 1 changed file with 43 additions and 3 deletions.
    46 changes: 43 additions & 3 deletions expand-collapse.js
    Original file line number Diff line number Diff line change
    @@ -22,18 +22,58 @@ window.addEventListener('load', () => {
    return;
    }

    initializeBtn();
    initializeButtons();
    initializeExpandCollapseAll();
    });

    function initializeBtn() {
    function initializeExpandCollapseAll() {
    const pageHeadActions = document.querySelector('.pagehead-actions');
    const listItem = document.createElement('li');
    const expandCollapseAllBtn = document.createElement('a');
    const files = document.querySelectorAll('.file');
    const buttons = document.querySelectorAll('.gist-expand-collapse-btn');

    listItem.appendChild(expandCollapseAllBtn);
    pageHeadActions.appendChild(listItem);

    expandCollapseAllBtn.classList.add('gist-expand-collapse-all-btn', 'btn', 'btn-sm');
    expandCollapseAllBtn.innerHTML = 'Collapse All';

    expandCollapseAllBtn.onclick = () => {
    if ('Collapse All' === expandCollapseAllBtn.innerHTML) {
    expandCollapseAllBtn.innerHTML = 'Expand All';

    for (let btn of buttons) {
    btn.innerHTML = 'Expand';
    }

    for (let file of files) {
    const fileContainer = file.querySelector('.file-header').nextSibling.nextSibling;
    fileContainer.classList.add('collapsed');
    }
    } else {
    expandCollapseAllBtn.innerHTML = 'Collapse All';

    for (let btn of buttons) {
    btn.innerHTML = 'Collapse';
    }

    for (let file of files) {
    const fileContainer = file.querySelector('.file-header').nextSibling.nextSibling;
    fileContainer.classList.remove('collapsed');
    }
    }
    };
    }

    function initializeButtons() {
    const files = document.querySelectorAll('.file');

    for (let file of files) {
    const actions = file.querySelector('.file-actions');
    const fileContainer = file.querySelector('.file-header').nextSibling.nextSibling;
    const expandCollapseBtn = document.createElement('a');


    expandCollapseBtn.classList.add('gist-expand-collapse-btn', 'btn', 'btn-sm');
    expandCollapseBtn.innerHTML = 'Collapse';
    actions.appendChild(expandCollapseBtn);
  3. Jason Boyle revised this gist Mar 12, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions expand-collapse.js
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,10 @@ GM_addStyle(
    );

    window.addEventListener('load', () => {
    if (document.body.classList.contains('page-gist-edit')) {
    return;
    }

    initializeBtn();
    });

  4. Jason Boyle revised this gist Mar 12, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion expand-collapse.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    // ==UserScript==
    // @name Github Gists: Expand / Collapse Files
    // @namespace @TODO
    // @namespace https://gist.github.com/Jaace/7b70d2bb19af63e10b144ed7d867eae0
    // @version 0.1
    // @description Add a button to expand or collapse files in github gists
    // @author Jason Boyle
  5. Jason Boyle renamed this gist Mar 12, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. Jason Boyle created this gist Mar 12, 2017.
    47 changes: 47 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    // Add a button to Collapse or Expand files in a Github Gist
    //
    // Install Tampermonkey and add this as a script

    // ==UserScript==
    // @name Github Gists: Expand / Collapse Files
    // @namespace @TODO
    // @version 0.1
    // @description Add a button to expand or collapse files in github gists
    // @author Jason Boyle
    // @match https://gist.github.com/*
    // @grant GM_addStyle
    // ==/UserScript==

    GM_addStyle(
    '.gist-expand-collapse-btn { margin: 0 0 0 6px; } ' +
    '.collapsed { display: none; }'
    );

    window.addEventListener('load', () => {
    initializeBtn();
    });

    function initializeBtn() {
    const files = document.querySelectorAll('.file');

    for (let file of files) {
    const actions = file.querySelector('.file-actions');
    const fileContainer = file.querySelector('.file-header').nextSibling.nextSibling;
    const expandCollapseBtn = document.createElement('a');


    expandCollapseBtn.classList.add('gist-expand-collapse-btn', 'btn', 'btn-sm');
    expandCollapseBtn.innerHTML = 'Collapse';
    actions.appendChild(expandCollapseBtn);

    expandCollapseBtn.onclick = function() {
    if ('Collapse' === expandCollapseBtn.innerHTML) {
    expandCollapseBtn.innerHTML = 'Expand';
    } else {
    expandCollapseBtn.innerHTML = 'Collapse';
    }

    fileContainer.classList.toggle('collapsed');
    };
    }
    }