Created
November 13, 2019 18:41
-
-
Save phillipadsmith/ecd74fe69db5455f174780fc9acaa8ea to your computer and use it in GitHub Desktop.
Quick and dirty client-side script to download names and titles from a Slack channel
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
// In the Web UI for Slack, navigate to a channel, click on "See All Members" | |
// and then scroll until you've hit the end of the list | |
// Open your browser's console and run this | |
var NameAndTitles = document.getElementsByClassName('c-base_entity__text-contents') | |
var NamesData = []; | |
for (var i=0; i < NameAndTitles.length; i++) { | |
var title = NameAndTitles[i].getElementsByClassName('c-member__title')[0]; | |
var person = NameAndTitles[i].getElementsByClassName('c-member_name')[0]; | |
var title_str = ''; | |
var person_str = ''; | |
if (title) { title_str = title.textContent }; | |
if (name) { person_str = person.textContent }; | |
NamesData[i] = [ person_str, title_str ]; | |
console.log(NamesData[i]); | |
} | |
var data = NamesData; | |
// https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side#24922761 | |
// Building the CSV from the Data two-dimensional array | |
// Each column is separated by ";" and new line "\n" for next row | |
var csvContent = ''; | |
data.forEach(function(infoArray, index) { | |
dataString = infoArray.join(';'); | |
csvContent += index < data.length ? dataString + '\n' : dataString; | |
}); | |
// The download function takes a CSV string, the filename and mimeType as parameters | |
// Scroll/look down at the bottom of this snippet to see how download is called | |
var download = function(content, fileName, mimeType) { | |
var a = document.createElement('a'); | |
mimeType = mimeType || 'application/octet-stream'; | |
if (navigator.msSaveBlob) { // IE10 | |
navigator.msSaveBlob(new Blob([content], { | |
type: mimeType | |
}), fileName); | |
} else if (URL && 'download' in a) { //html5 A[download] | |
a.href = URL.createObjectURL(new Blob([content], { | |
type: mimeType | |
})); | |
a.setAttribute('download', fileName); | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
} else { | |
location.href = 'data:application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported | |
} | |
} | |
download(csvContent, 'dowload.csv', 'text/csv;encoding:utf-8'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment