Created
June 11, 2021 23:10
-
-
Save ryanpcmcquen/ca0a6a4af224777fe050f31ae19f645c to your computer and use it in GitHub Desktop.
Get all CSS colors as a HashMap directly from W3.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>repl.it</title> | |
<link href="style.css" rel="stylesheet" type="text/css" /> | |
</head> | |
<body> | |
<script src="script.js"></script> | |
<pre class="css_color_map"></pre> | |
</body> | |
</html> |
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
document.addEventListener('DOMContentLoaded', async () => { | |
const w3_color_page = await fetch('https://www.w3.org/TR/css-color-4/'); | |
const parser = new DOMParser(); | |
const new_doc = parser.parseFromString( | |
await w3_color_page.text(), | |
'text/html' | |
); | |
const color_table_rows = Array.from( | |
new_doc.querySelectorAll('.named-color-table tr') | |
); | |
const color_names = color_table_rows.slice(1).reduce((acc, row) => { | |
acc[row.querySelector('th').textContent.trim()] = row | |
.querySelector('td:nth-child(4)') | |
.textContent.trim(); | |
return acc; | |
}, {}); | |
console.log(color_names); | |
document.querySelector('.css_color_map').textContent = | |
JSON.stringify(color_names, null, '\t'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment