-
-
Save maboloshi/dce7ec107dff766d3e43824942f48d1a to your computer and use it in GitHub Desktop.
TamperMonkey Script: Gist expand / collapse files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 https://gist.github.com/Jaace/7b70d2bb19af63e10b144ed7d867eae0 | |
// @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', () => { | |
if (document.body.classList.contains('page-gist-edit')) { | |
return; | |
} | |
initializeButtons(); | |
initializeExpandCollapseAll(); | |
}); | |
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 = '折叠全部'; | |
expandCollapseAllBtn.onclick = () => { | |
if ('折叠全部' === expandCollapseAllBtn.innerHTML) { | |
expandCollapseAllBtn.innerHTML = '展开全部'; | |
for (let btn of buttons) { | |
btn.innerHTML = '展开'; | |
} | |
for (let file of files) { | |
const fileContainer = file.querySelector('.file-header').nextSibling.nextSibling; | |
fileContainer.classList.add('collapsed'); | |
} | |
} else { | |
expandCollapseAllBtn.innerHTML = '折叠全部'; | |
for (let btn of buttons) { | |
btn.innerHTML = '折叠'; | |
} | |
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 = '折叠'; | |
actions.appendChild(expandCollapseBtn); | |
expandCollapseBtn.onclick = function() { | |
if ('折叠' === expandCollapseBtn.innerHTML) { | |
expandCollapseBtn.innerHTML = '展开'; | |
} else { | |
expandCollapseBtn.innerHTML = '折叠'; | |
} | |
fileContainer.classList.toggle('collapsed'); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment