Skip to content

Instantly share code, notes, and snippets.

@msalahat
Last active March 30, 2017 12:56
Show Gist options
  • Save msalahat/c684e118362081e8bb645046d2377a52 to your computer and use it in GitHub Desktop.
Save msalahat/c684e118362081e8bb645046d2377a52 to your computer and use it in GitHub Desktop.
Export data from countrycode.org
/**
* Copyrights for console.save http://bgrins.github.io/devtools-snippets/#console-save
**/
(function(console){
console.save = function(data, filename){
if(!data) {
console.error('Console.save: No data')
return;
}
if(!filename) filename = 'console.json'
if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4)
}
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
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)
}
})(console)
//***************************************//
let countries = [];
jQuery(".main-table").find("tr").each((index , item)=>{
let country = {"name":"" , "iso-code":"", "phone-keycode":"" };
let data = jQuery(item).find("td").each((i, d)=>{
console.log(i , jQuery(d).text());
switch(i){
case 0:
country.name = jQuery(d).text();
break;
case 1:
country['phone-keycode'] = jQuery(d).text();
break;
case 2:
country['iso-code'] = jQuery.trim(jQuery(d).text().split("/")[0]).toLowerCase();
break;
}
});
countries.push(country);
});
console.table(countries);
console.save(countries , "countries.json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment