Skip to content

Instantly share code, notes, and snippets.

@milosb793
Last active February 11, 2021 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milosb793/2a740f91c3aea3fad4c19b64db763627 to your computer and use it in GitHub Desktop.
Save milosb793/2a740f91c3aea3fad4c19b64db763627 to your computer and use it in GitHub Desktop.
A JS class that downloads all icons and pack it in proper format for further usage
/**
* 1. Load a page: https://fontawesome.com/icons?d=gallery
* 2. Press F12 and paste the code in the console
* 3. Wait until all data is fetched and packed
* 4. Use downloader.dump() to get JSON
**/
class FontAwesomeDownloader {
static proIconsClass = 'gray4'; /* parent */
static visibleIconsSelector = '#results-icons i';
static loadMoreButtonSelector = '#results-icons > div > button';
totalIcons = 0;
allIcons = [];
interval = 0;
construct(limit = -1) {
this.limit = limit;
}
isProIcon(icon) {
return icon.parentElement.classList.contains(FontAwesomeDownloader.proIconsClass);
}
shouldStop() {
return this.totalIcons >= this.limit;
}
stop() {
clearInterval(this.interval);
}
processPage() {
const icons = document.querySelectorAll(FontAwesomeDownloader.visibleIconsSelector);
console.log(`Processing page: ${icons.length} icons`);
console.log(this.isProIcon);
icons.forEach(icon => {
if (icon) {
let [type, iconClass] = icon.classList,
isPro = this.isProIcon(icon);
this.allIcons.push({
type,
iconClass,
isPro
});
this.totalIcons++;
}
});
}
removeOld() {
const icons = document.querySelectorAll(FontAwesomeDownloader.visibleIconsSelector);
console.log(`Removing old: ${icons.length} icons`);
Array.prototype.forEach.call(icons, (icon) => icon.parentNode.removeChild(icon));
}
loadMore() {
console.log("Loading more");
document.querySelector(FontAwesomeDownloader.loadMoreButtonSelector).click();
}
downloadPage() {
this.loadMore();
setTimeout(() => {
this.processPage();
this.removeOld();
}, 1500);
}
downloadAll() {
this.interval = setInterval(() => {
try {
if (this.shouldStop()) {
console.log("Limit reached, stopping");
this.stop();
return false;
}
this.downloadPage();
console.log(`Total icons: ${this.totalIcons}`);
} catch (e) {
console.log("Stopping");
this.stop();
}
}, 1500);
}
dump() {
return JSON.stringify(this.allIcons);
}
}
let downloader = new FontAwesomeDownloader(); downloader.downloadAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment