Skip to content

Instantly share code, notes, and snippets.

@fogrew
Last active July 27, 2017 19:11
Show Gist options
  • Save fogrew/9dd44b07cd8f09067fc93608cf20287a to your computer and use it in GitHub Desktop.
Save fogrew/9dd44b07cd8f09067fc93608cf20287a to your computer and use it in GitHub Desktop.
Highlight each SVG on page without title
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