Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does facebook detect when a link as been PASTED

Facebook's status update input (well, contenteditable div) detects links.

When typing a link it waits until the spacebar is pressed before fetching the URL.

When pasting a link it fetches the URL instantly.

I can already parse the url after the spacebar is pressed...but I'm not sure about detecting when content is pasted.

Any solution would be awesome; a jQuery formatted solution will be BEST!

like image 235
David Murdoch Avatar asked Dec 11 '09 22:12

David Murdoch


1 Answers

Modern day browsers support onpaste:

<!DOCTYPE html>
<html>
<head>
<title>onpaste event example</title>
</head>

<body>
<h1>Play with this editor!</h1>
<textarea id="editor" rows="3" cols="80">
Try pasting text into this area!
</textarea>

<script>
function log(txt) {
  document.getElementById("log").appendChild(document.createTextNode(txt + "\n"));
}

function pasteIntercept(evt) {
  log("Pasting!");
}

document.getElementById("editor").addEventListener("paste", pasteIntercept, false);
</script>

<h2>Log</h2>
<textarea rows="15" cols="80" id="log" readonly="true"></textarea>
</body>
</html>
like image 163
epascarello Avatar answered Nov 03 '22 00:11

epascarello