Skip to content

Instantly share code, notes, and snippets.

@MrHus
Created July 21, 2017 09:53
Show Gist options
  • Save MrHus/e1ecce16e14d8a440bc3e06121b7419a to your computer and use it in GitHub Desktop.
Save MrHus/e1ecce16e14d8a440bc3e06121b7419a to your computer and use it in GitHub Desktop.
Find all material-icons on https://material.io/icons/
/*
Wanted a list to get all the names for each material design
icon. Could not find a list online, other than the page at:
https://material.io/icons/
So I wrote a script to get all the icons from that page. Unfortunately
it is made in polymer which uses the shadowdom extensively. So
getting all the icons means digging through the entire dom and shadow
dom.
To make thinks worse the icons are only inserted into the dom
which they are visible in the viewport. This means that in order
to get all the icons you must scroll down through the page.
So this script scrolls down by X pixels at a time, inspects the
body for <i class="md-icon"> and repeats until the bottom of the
page is reached.
Tested on Chrome.
*/
// Will store the names of the icons.
var names = [];
var scrollPos = 0; // The initial scroll pos.
const scrollMoveBy = 100; // Move by 100 pixels each time
// Start at the top
window.scrollTo(0, 0);
// Moves the scrollbar down to the bottom of the page.
function moveScrollbar() {
if (scrollPos < document.body.scrollHeight) {
window.scrollBy(0, scrollMoveBy);
scrollPos += scrollMoveBy;
// Give the browser some breathing room, prevents freezing of browser.
setTimeout(() => {
getAllIconsForGreatJustice(document.body);
moveScrollbar();
}, 100);
} else {
// Finished the process now log the names that where found.
console.log('Found: ' + names.length + ' icons');
console.log(names.join('" | "'));
}
}
setTimeout(() => {
moveScrollbar();
}, 1000);
// Recursively get all icons, and find them in shadowRoots as well.
function getAllIconsForGreatJustice(element) {
element.children.array().forEach(huntIcons);
if (element.shadowRoot) {
element.shadowRoot.children.array().forEach(huntIcons);
}
}
function huntIcons(element) {
// Add if it is an icon.
if (isMaterialIcon(element)) {
var name = element.textContent;
// Add it only once, should have used a set but fuck it it works
if (names.includes(name) === false) {
names.push(name);
}
}
// Call again to start the recursion
getAllIconsForGreatJustice(element);
}
function isMaterialIcon(element) {
return element.tagName === 'I' && element.classList.contains('md-icon');
}
@MrHus
Copy link
Author

MrHus commented Jul 21, 2017

To run the script simply go to https://material.io/icons/ and copy and past the script into the console in the developer tools.

@jvhoven
Copy link

jvhoven commented Jul 19, 2019

const icons = document.getElementsByTagName("i");

const iconSet = [];

for (let icon of icons) {
  // Remove the .icon-image-preview class
  const [className] = icon.className.split(" ");
  const [_, iconName] = className.split("-");

  iconSet.push(iconName);
}

console.log(`Found ${iconSet.length} icons`);
console.log(iconSet.join('" | "'));

@gidomanders
Copy link

De link verwijst nu naar Google Fonts, daar wordt de volgende json ingeladen: https://fonts.google.com/metadata/icons. Misschien kun je daar gebruik van maken in een verbeterde versie van dit script ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment