Skip to content

Instantly share code, notes, and snippets.

@alexkli
Last active March 24, 2023 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexkli/d84482b582bcc4eb859ba8c521779382 to your computer and use it in GitHub Desktop.
Save alexkli/d84482b582bcc4eb859ba8c521779382 to your computer and use it in GitHub Desktop.
Trello collapsible list userscript (tested with Safari)

Trello does not support collapsing lists out of the box. There are different solutions such as an extension for Chrome. But nothing that works in Safari any more, as of March 2023, so I wrote my own user script.

Use this with the Userscripts extension for Safari.

It looks like this:

trello-collapsible-list-screenshot

Click on the ▼ icon to toggle the list.

Collapsed state is persisted across page loads.

// ==UserScript==
// @name Trello Collapsible Lists
// @description written by @alexkli
// @match https://trello.com/b/*
// ==/UserScript==
const boardId = window.location.pathname.replace(/^\/b\/([^\/]+).*$/, '$1');
function getListKey(list) {
const listName = list.querySelector('.list-header-name-assist').textContent;
return `list-collapsed-${boardId}-${listName}`;
}
function toggleList(collapseButton) {
const list = collapseButton.parentNode.parentNode;
const listContent = list.querySelector('.list-cards');
const listKey = getListKey(list);
if (listContent.toggleAttribute('hidden')) {
// hidden
collapseButton.innerText = '▶︎';
list.querySelector('.card-composer-container').style.display = 'none';
localStorage.setItem(listKey, 'true');
} else {
// visible
collapseButton.innerText = '▼';
list.querySelector('.card-composer-container').style.display = 'flex';
localStorage.removeItem(listKey);
}
}
function init() {
// list: .list
// header: .list-header
// list content: .list-cards
document.querySelectorAll('.list').forEach((list) => {
const listKey = getListKey(list);
const listHeader = list.querySelector('.list-header');
const collapseButton = document.createElement('div');
collapseButton.className = 'list-collapse-button';
collapseButton.innerText = '▼';
collapseButton.style.position = 'absolute';
collapseButton.style.width = '8px';
collapseButton.style.cursor = 'pointer';
collapseButton.addEventListener("click", (event) => {
toggleList(event.target);
});
listHeader.insertBefore(collapseButton, listHeader.firstChild);
const listHeaderTarget = list.querySelector('.list-header-target');
listHeaderTarget.style.left = '25px';
const listTitle = list.querySelector('.list-header-name');
listTitle.style['margin-left'] = '10px';
if (localStorage.getItem(listKey)) {
toggleList(collapseButton);
}
});
}
function onDocumentReady() {
// wait until we have .list elements present in the DOM
const observer = new MutationObserver((mutationList) => {
if (document.querySelector('.list')) {
observer.disconnect();
init();
}
});
observer.observe(document, {
childList: true,
subtree: true
});
}
if (document.readyState !== "loading") {
onDocumentReady();
} else {
window.addEventListener("DOMContentLoaded", onDocumentReady);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment