Last active
July 27, 2017 19:11
-
-
Save fogrew/9dd44b07cd8f09067fc93608cf20287a to your computer and use it in GitHub Desktop.
Highlight each SVG on page without title
This file contains 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
function getSVGs() { | |
return $$('svg use').reduce((links, svg) => { | |
const image = svg.href.baseVal | |
const includes = links.some(obj => Object.values(obj).image) | |
if (!includes) { | |
links.push({ | |
element: svg, | |
image: image | |
}) | |
} | |
return links | |
}, []).sort() | |
} | |
function getFile(blob) { | |
return new Promise(resolve => { | |
let reader = new FileReader() | |
reader.addEventListener("loadend", () => resolve(reader.result)) | |
reader.readAsText(blob) | |
}); | |
} | |
function getSVGcontain(url) { | |
return fetch(url) | |
.then(res => res.blob()) | |
.then(res => getFile(res)) | |
} | |
function convertString2html(string) { | |
const parser = new DOMParser() | |
return parser.parseFromString(string, "text/xml") | |
} | |
function getHash(link) { | |
const url = new URL(link) | |
return url.hash | |
} | |
function getTitle(xml, hash) { | |
const title = xml.getElementById(hash.slice(1)).querySelector('title') | |
return title && title.textContent | |
} | |
function highlightElements(link, element, string) { | |
const hash = getHash(link) | |
const xml = convertString2html(string) | |
const title = getTitle(xml, hash) | |
if(title === null) { | |
element.style.border = '5px solid blue' | |
} | |
} | |
function checkTitles() { | |
getSVGs().map(url => { | |
getSVGcontain(url.image) | |
.then(res => { | |
highlightElements(url.image, url.element.parentNode, res) | |
}) | |
}) | |
} | |
checkTitles() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment