Last active
February 11, 2021 11:31
-
-
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
This file contains hidden or 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
/** | |
* 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