Created
April 11, 2021 16:52
-
-
Save Qix-/8741eb5eab7d35aa9a362095f01d92b0 to your computer and use it in GitHub Desktop.
Ctrl+Click on tracks from EveryNoise.com to copy the artist/title from the "title" attribute
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
// ==UserScript== | |
// @match https://everynoise.com/* | |
// ==/UserScript== | |
function attachCSS(css) { | |
const element = document.createElement('style'); | |
element.setAttribute('type', 'text/css'); | |
if ('textContent' in element) { | |
element.textContent = css; | |
} else { | |
element.styleSheet.cssText = css; | |
} | |
document.getElementsByTagName('head')[0].appendChild(element); | |
} | |
function cleanTitle(title) { | |
return title.replace(/^\s*e\.\s*g\.\s*/i, ''); | |
} | |
function onMaybeClick(ev) { | |
if (!ev.ctrlKey) return; | |
const target = ev.target; | |
if (!target.classList.contains('scanme')) return; | |
ev.preventDefault(); | |
event.stopPropagation(); | |
console.debug('copying', target); | |
target.classList.add('copying'); | |
setTimeout(() => target.classList.remove('copying'), 0); | |
navigator.clipboard.writeText( | |
cleanTitle(target.getAttribute('title')) | |
).then( | |
() => {}, | |
() => alert('failed to write to clipboard!') | |
); | |
return false; | |
} | |
attachCSS(` | |
.scanme.copying { | |
transition: background-color 0s linear; | |
background-color: #84f542; | |
} | |
.scanme { | |
transition: background-color 2s linear; | |
} | |
`); | |
document.addEventListener( | |
'click', | |
onMaybeClick, | |
{capture: true} | |
); | |
navigator.permissions.query({name: "clipboard-write"}).then(result => { | |
if (!(result.state === "granted" || result.state === "prompt")) { | |
alert('Clipboard denied; I won\'t be able to copy for you.'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment