Skip to content

Instantly share code, notes, and snippets.

@closetgeekshow
Last active October 26, 2024 00:56
Show Gist options
  • Select an option

  • Save closetgeekshow/2461ab2a8cd1b328633723f6dd8034f2 to your computer and use it in GitHub Desktop.

Select an option

Save closetgeekshow/2461ab2a8cd1b328633723f6dd8034f2 to your computer and use it in GitHub Desktop.
highlight all selects, onClick: copy options to clipboard, clear everything after
javascript:void%20function(){(()=%3E{function%20a(a){a.preventDefault();const%20e=Array.from(a.target.getElementsByTagName(%22option%22)).map(a=%3Ea.innerText),f=e.join(%22\n%22);navigator.clipboard.writeText(f),b.forEach((a,b)=%3E{a.style.backgroundColor=c[b]}),d.abort()}const%20b=Array.from(document.getElementsByTagName(%22select%22)),c=b.map(a=%3EgetComputedStyle(a).backgroundColor),d=new%20AbortController;b.forEach(a=%3Ea.style.backgroundColor=%22yellow%22),b.forEach((b,c)=%3E{b.addEventListener(%22mousedown%22,b=%3Ea(b,c),{capture:!0,signal:d.signal})})})()}();
const init = () => {
const highlightElements = Array.from(document.getElementsByTagName('select'));
const originalColors = highlightElements.map(el => getComputedStyle(el).backgroundColor);
const controller = new AbortController();
highlightElements.forEach(el => el.style.backgroundColor = 'yellow');
function handleMousedown(event, index) {
event.preventDefault();
const selectedElements = Array.from(event.target.getElementsByTagName('option'))
.map(opt => opt.innerText);
const outputString = selectedElements.join('\n');
navigator.clipboard.writeText(outputString);
highlightElements.forEach((el, i) => {
el.style.backgroundColor = originalColors[i];
});
controller.abort();
}
highlightElements.forEach((el, index) => {
el.addEventListener('mousedown', (e) => handleMousedown(e, index), {
capture: true,
signal: controller.signal
});
});
};
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment