Last active
December 16, 2020 20:34
-
-
Save teyfix/b72fcebd78a85b891a4041d52e6c302f to your computer and use it in GitHub Desktop.
a small script that adds label to tiermaker.com characters
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
(() => { | |
const debounce = (fn, ms = 50) => { | |
let timer; | |
const tick = () => { | |
fn(); | |
timer = null; | |
}; | |
return () => { | |
if (null != timer) { | |
clearTimeout(timer); | |
} | |
timer = setTimeout(tick, ms); | |
}; | |
}; | |
const common = '_' + Math.random().toString(36).substring(2); | |
const pattern = /(?<=\/flag-of-)[^\/]+(?=png\.png)/; | |
const selector = `[data-common=${common}]`; | |
document.body.addEventListener( | |
'DOMSubtreeModified', | |
debounce(() => { | |
document.querySelectorAll(`.character:not(${selector})`).forEach((e) => { | |
const style = getComputedStyle(e); | |
const match = style.backgroundImage.match(pattern); | |
if (null == match) { | |
return; | |
} | |
const title = match[0] | |
.replace(/-/g, ' ') | |
.replace(/(?<=^|\W)\w/g, (m) => m.toUpperCase()); | |
const span = document.createElement('span'); | |
span.innerText = title; | |
e.setAttribute('aria-label', title + ' Flag'); | |
e.setAttribute('data-common', common); | |
e.appendChild(span); | |
e.addEventListener('click', ($event) => { | |
if ($event.ctrlKey) { | |
$event.target.style.display = 'none'; | |
} | |
}); | |
}); | |
}) | |
); | |
const style = document.createElement('style'); | |
style.setAttribute('type', 'text/css'); | |
style.innerHTML = | |
`${selector} {` + | |
'position: relative' + | |
'}' + | |
`${selector} span {` + | |
'position: absolute;' + | |
'background-color: rgba(0,0,0,.68);' + | |
'padding: .2em .4em;' + | |
'max-width: 100%;' + | |
'border-radius: .3em;' + | |
'left: 0;' + | |
'color: #fff;' + | |
'margin: .4em;' + | |
'font-size: small;' + | |
'pointer-events: none' + | |
'}'; | |
document.body.appendChild(style); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment