How does facebook detect when a link as been PASTED

前端 未结 8 1545
轮回少年
轮回少年 2020-12-16 04:45

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.

相关标签:
8条回答
  • 2020-12-16 05:16

    Listen to the keyup event in the field. If the field's content has changed by more than 1 character after one keyup, something has been pasted.

    As @epascarello points out, check right click events, too, as the user could be using the context menu.

    0 讨论(0)
  • 2020-12-16 05:17

    Compare successive onChange events. If the difference between them is more than one character, it's a paste. Else it's typed in.

    0 讨论(0)
  • 2020-12-16 05:17

    keypress does not work in IE8. Use keydown instead.

    0 讨论(0)
  • 2020-12-16 05:20

    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>
    
    0 讨论(0)
  • 2020-12-16 05:22

    I think the most reliable way to do this is to implement it on your own, for example like this:

    $(document).ready(function(){
        var ctrl = false;
      var pasted = 0;
      $("#paste").keydown(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
            $("#console").val($("#console").val()+String(code)+",");
        if (!ctrl && code == 17){
            ctrl = true;
        }else{
            if(ctrl && code == 86){
            $(".paste>div")[0].innerHTML = "Pasted "+(++pasted)+"x";
          }
        }
      });
      $("#paste").keyup(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 17){
            ctrl = false;
        }
      });
    });
    

    $(document).ready(function(){
    	var ctrl = false;
      var pasted = 0;
      $("#paste").keydown(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
    		$("#console").val($("#console").val()+String(code)+",");
        if (!ctrl && code == 17){
        	ctrl = true;
        }else{
        	if(ctrl && code == 86){
          	$(".paste>div")[0].innerHTML = "Pasted "+(++pasted)+"x";
          }
        }
      });
      $("#paste").keyup(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 17){
        	ctrl = false;
        }
      });
    });
    div.top {
      width: 100%;
      display: inline-block;
    }
    
    div.top>div {
      width: 50%;
      display: inline-block;
      float: left;
      padding: 0px 10px 0px 0px;
      box-sizing: border-box;
    }
    
    textarea {
      width: 100%;
      height: 100px;
      resize: none;
      box-sizing: border-box;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="top">
      <div>
        <div>
          Try paste here:
        </div>
        <div class="test">
          <textarea value="" id="paste"></textarea>
        </div>
      </div>
      <div>
        <div>
          Console:
        </div>
        <div class="console">
          <textarea value="" id="console"></textarea>
        </div>
      </div>
      <div class="paste">
        <div>
          Pasted:
        </div>
      </div>
    </div>

    0 讨论(0)
  • 2020-12-16 05:25
     <script type="text/javascript">
      $(document).ready(function(){
       $("#text").keypress (function(e){
        var code = (e.keyCode ? e.keyCode : e.which);
        if ((code == 86 || code == 118) && e.ctrlKey) //86 = v 118 = V
        {
         alert("Pasted!");
        }   
       });
      });
     </script>
    
    </head>
    <body>
     <textarea id="text">
     </textarea>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题