Skip to content

Instantly share code, notes, and snippets.

@nashingofteeth
Last active March 24, 2021 16:01
Show Gist options
  • Save nashingofteeth/9fa36784a7de8204d35a2a402f74d197 to your computer and use it in GitHub Desktop.
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)
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