Last active
March 24, 2021 16:01
-
-
Save nashingofteeth/9fa36784a7de8204d35a2a402f74d197 to your computer and use it in GitHub Desktop.
intercept paste event and convert urls in clipboard into links (for contenteditable elements)
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
document.addEventListener('paste', function (event) { | |
var clip = event.clipboardData.getData('text/plain'); | |
var urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; | |
var urlq = clip.match(urlRegex); | |
if (urlq != null) { | |
event.preventDefault(); | |
var anchored = clip.replace(urlRegex, function(url) { | |
return '<a href="' + url + | |
'" style="cursor:pointer" onclick="window.open(this.href);return false">' + | |
url + '</a>'; | |
}); | |
document.execCommand('insertHTML', false, anchored); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment