Created
November 22, 2018 14:28
-
-
Save tancredi/47e9712120ed6e857b188cf1b3edb6e7 to your computer and use it in GitHub Desktop.
Export / Import GitHub tags (browser script)
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
function rgbToHex(color) { | |
const [ r, g, b ] = color.substring(4, color.length - 1).split(', '); | |
return '#' + [ r, g, b ].map(x => { | |
const hex = parseInt(x).toString(16); | |
return hex.length === 1 ? '0' + hex : hex | |
}).join(''); | |
} | |
function exportLabels() { | |
const items = [].slice.call(document.querySelectorAll('.js-label-list .js-labels-list-item')); | |
const labels = items.map(element => { | |
const tag = element.querySelector('.js-label-link'); | |
const description = element.querySelector('.label-description'); | |
return { | |
color: rgbToHex(tag.style.backgroundColor), | |
text: tag.innerText, | |
description: description.innerText | |
} | |
}); | |
copy(JSON.stringify(labels)); | |
console.log('✅ Copied to clipboard as JSON') | |
} | |
function importLabels(json) { | |
const newLabelButton = document.querySelector('.js-details-target-new-label') | |
const tasks = JSON.parse(json).map(({ color, text, description }) => () => { | |
!document.querySelector('.labels-list.js-details-container.open') && newLabelButton.click(); | |
const textInput = document.querySelector('.js-new-label-name-input'); | |
const descriptionInput = document.querySelector('.js-new-label-description-input'); | |
const colorInput = document.querySelector('.js-new-label-color-input'); | |
const submitButton = document.querySelector('.js-label-form button[type="submit"]'); | |
textInput.value = text; | |
descriptionInput.value = description; | |
colorInput.value = color; | |
submitButton.removeAttribute('disabled'); | |
submitButton.click(); | |
}); | |
tasks.forEach((task, i) => setTimeout(task, i * 150)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment