Instantly share code, notes, and snippets.

@MoOx /index.js
Last active Sep 12, 2018

Embed
What would you like to do?
Export/import github labels
// go on you labels pages
// eg https://github.com/cssnext/cssnext/labels
// paste this script in your console
// copy the output and now you can import it using https://github.com/popomore/github-labels !
var labels = [];
[].slice.call(document.querySelectorAll(".label-link"))
.forEach(function(element) {
labels.push({
name: element.textContent.trim(),
// using style.backgroundColor might returns "rgb(...)"
color: element.getAttribute("style")
.replace("background-color:", "")
.replace(/color:.*/,"")
.trim()
// github wants hex code only without # or ;
.replace(/^#/, "")
.replace(/;$/, "")
.trim(),
})
})
console.log(JSON.stringify(labels, null, 2))
@apotek

This comment has been minimized.

Show comment
Hide comment
@apotek

apotek Feb 5, 2016

Useful! Thank you.

apotek commented Feb 5, 2016

Useful! Thank you.

@vincentaudebert

This comment has been minimized.

Show comment
Hide comment
@vincentaudebert

vincentaudebert Apr 15, 2016

Awesome! Saved me a lot of time :)

vincentaudebert commented Apr 15, 2016

Awesome! Saved me a lot of time :)

@Isaddo

This comment has been minimized.

Show comment
Hide comment
@Isaddo

Isaddo May 6, 2016

Another approach to import github labels via console command

https://gist.github.com/Isaddo/7efebcb673a0957b9c6f07cd14826ea4

Isaddo commented May 6, 2016

Another approach to import github labels via console command

https://gist.github.com/Isaddo/7efebcb673a0957b9c6f07cd14826ea4

@wo0dyn

This comment has been minimized.

Show comment
Hide comment
@wo0dyn

wo0dyn Oct 13, 2016

@MoOx: Thank you so much mate! ❤️

wo0dyn commented Oct 13, 2016

@MoOx: Thank you so much mate! ❤️

@jpike88

This comment has been minimized.

Show comment
Hide comment
@jpike88

jpike88 Jun 21, 2017

This script doesn't work on Safari

jpike88 commented Jun 21, 2017

This script doesn't work on Safari

@oguennec

This comment has been minimized.

Show comment
Hide comment
@oguennec

oguennec Jul 4, 2017

Great, thank you !

oguennec commented Jul 4, 2017

Great, thank you !

@ntwb

This comment has been minimized.

Show comment
Hide comment
@ntwb

ntwb Jul 12, 2017

I just used the following which was quick and easy https://github.com/jvandemo/copy-github-labels-cli

(I was linked to this after the fact)

ntwb commented Jul 12, 2017

I just used the following which was quick and easy https://github.com/jvandemo/copy-github-labels-cli

(I was linked to this after the fact)

@martindafonte

This comment has been minimized.

Show comment
Hide comment
@martindafonte

martindafonte Jun 22, 2018

Hi, you can add one line and also get the description:

var labels = [];
[].slice.call(document.querySelectorAll(".label-link"))
.forEach(function(element) {
  labels.push({
    name: element.textContent.trim(),
    description: element.getAttribute("aria-label"),
    // using style.backgroundColor might returns "rgb(...)"
    color: element.getAttribute("style")
      .replace("background-color:", "")
      .replace(/color:.*/,"")
      .trim()
      // github wants hex code only without # or ;
      .replace(/^#/, "")
      .replace(/;$/, "")
      .trim(),
  })
})
console.log(JSON.stringify(labels, null, 2))

martindafonte commented Jun 22, 2018

Hi, you can add one line and also get the description:

var labels = [];
[].slice.call(document.querySelectorAll(".label-link"))
.forEach(function(element) {
  labels.push({
    name: element.textContent.trim(),
    description: element.getAttribute("aria-label"),
    // using style.backgroundColor might returns "rgb(...)"
    color: element.getAttribute("style")
      .replace("background-color:", "")
      .replace(/color:.*/,"")
      .trim()
      // github wants hex code only without # or ;
      .replace(/^#/, "")
      .replace(/;$/, "")
      .trim(),
  })
})
console.log(JSON.stringify(labels, null, 2))
@micalevisk

This comment has been minimized.

Show comment
Hide comment
@micalevisk

micalevisk Jul 27, 2018

and this function to download labels as file directly

function saveJSON(data, filename) {
  const blob = new Blob([JSON.stringify(data, undefined, 4)], { type: 'text/json' });
  const e = document.createEvent('MouseEvents');
  const a = document.createElement('a');

  a.download = filename;
  a.href = window.URL.createObjectURL(blob);
  a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
  e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  a.dispatchEvent(e);
}

example

saveJSON(labels, 'labels.json')

micalevisk commented Jul 27, 2018

and this function to download labels as file directly

function saveJSON(data, filename) {
  const blob = new Blob([JSON.stringify(data, undefined, 4)], { type: 'text/json' });
  const e = document.createEvent('MouseEvents');
  const a = document.createElement('a');

  a.download = filename;
  a.href = window.URL.createObjectURL(blob);
  a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
  e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  a.dispatchEvent(e);
}

example

saveJSON(labels, 'labels.json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment