Skip to content

Instantly share code, notes, and snippets.

@jeffbcross
Last active July 18, 2016 22:36
Show Gist options
  • Save jeffbcross/aef6562cfb27be5ccdc57c943499025a to your computer and use it in GitHub Desktop.
Save jeffbcross/aef6562cfb27be5ccdc57c943499025a to your computer and use it in GitHub Desktop.
Script to add a label to all cards in a trello board
/**
* Step 1: Create a named label
* Step 2: Open board
* Step 3: Open devtools console
* Step 4: Copy and paste this whole gist into console
* Step 5: Create an array of all cards to be renamed. For all cards: var allCards = Array.prototype.slice.call(document.querySelectorAll('.list-card'))
* Step 6: call addLabelToCards with the list of nodes as first argument, label name as 2nd argument: addLabelToCards(allCards, 'work')
**/
function addLabelToCards(cards, labelName) {
'use strict';
// Should be a real array of nodes.
let next = cards.shift();
if (!next) return;
clickAndWait(next, '.js-edit-labels')
.then(_ => clickAndWait('.js-edit-labels', '.edit-labels-pop-over'))
.then(_ => {
// Get just the label that matches passed in name
let label = Array.prototype.filter.call(getAvailableLabels(), (el) => {
return el.textContent === labelName;
})[0];
if(!label) {
// This card probably already has the label applied
return;
}
return clickAndWait(label, `[data-idlabel="${label.attributes['data-idlabel'].nodeValue}"].active`);
})
.then(_ => clickAndWait('.dialog-close-button', '!.card-detail-window'))
.then(_ => addLabelToCards(cards, labelName));
}
function clickAndWait (clickerSelectorOrEl, elSelector) {
let clicker = typeof clickerSelectorOrEl == 'string' ? document.querySelector(clickerSelectorOrEl) : clickerSelectorOrEl;
let shouldGoAway = elSelector.indexOf('!') === 0;
elSelector = elSelector.replace(/^!/, '');
return new Promise((res) => {
clicker.click();
let interval = setInterval(() => {
if (!!shouldGoAway !== !!document.querySelector(elSelector)) {
clearInterval(interval);
res();
}
}, 10);
});
}
function getAvailableLabels() {
return document.querySelectorAll('.edit-labels-pop-over .card-label:not(.active)');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment