如何禁止复制/粘贴表单?

时间:2014-03-26 04:05:36

标签: javascript forms meteor chat copy-paste

我正在使用Meteor的聊天应用程序,我不希望用户能够出于明显的垃圾邮件原因将内容复制/粘贴到表单中。这可能吗?以下是我用来运行聊天应用的代码:

使用Javascript:

// render all of our messages in the ui
Template.chatBox.helpers({
  "messages": function() {
    return chatCollection.find();
  }
});

// get the value for handlerbar helper user
Template.chatMessage.helpers({
  "user": function() {
    if(this.userId == 'me') {
      return this.userId;
    } else if(this.userId) {
      getUsername(this.userId);
      return Session.get('user-' + this.userId);
    } else {
      return 'anonymous-' + this.subscriptionId;
    }
  }
});

// when Send Chat clicked at the message to the collection
Template.chatBox.events({
    "click #send": function() {
            if (Meteor.user() == null) {
              alert("You must login to post");
            return;
          }
            $('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
            var message = $('#chat-message').val();

            // check to see if the message has any characters in it
            if (message.length < 1) {
              alert("You must enter a message to post.");
            return;
          }

            chatCollection.insert({
                userId: 'me',
                message: message
            });
            $('#chat-message').val('');

            //Validation
            var bot =Check_bots();

            if(bot==false)
            {    
            //add the message to the stream
            chatStream.emit('chat', message);
       }
        else
        {
            alert("Slow down! No need to post that fast.");
            return false;
        }
    },

    "keypress #chat-message": function(e) {
        if (Meteor.user() == null) {
            alert("You must login to post");
            return;
        }
        if (e.which == 13) {

          //Validation
       var bot =Check_bots();

        if(bot==false)
        {
            $('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
            console.log("you pressed enter");
            e.preventDefault();
            //repeat function from #send click event here
            var message = $('#chat-message').val();

            // check to see if the message has any characters in it
            if (message.length < 1) {
              alert("You must enter a message to post.");
            return;
          }

            chatCollection.insert({
                userId: 'me',
                message: message
            });
            $('#chat-message').val('');

            //add the message to the stream
            chatStream.emit('chat', message);
        }
        else
        {
            alert("Slow down! No need to post that fast.");
            return false;
        }
    }
  }
});

chatStream.on('chat', function(message) {
  chatCollection.insert({
    userId: this.userId,
    subscriptionId: this.subscriptionId,
    message: message
  });
});

var lastintime=0;
var defference=0;
var msg_count=0;

function Check_bots()
{
    var seconds = new Date().getTime() / 1000;
    seconds=parseInt(seconds);

    if(lastintime < seconds)
    {
        defference = seconds -lastintime;
        lastintime=seconds;

        if(defference<=5 && msg_count>=3)
        {
            return true;
        }
        else
        {
             return false;
        }
    }
}

我不知道从哪里开始。你如何防止复制/粘贴?

1 个答案:

答案 0 :(得分:3)

这不是一个好主意。 Internet Explorer有一个onpaste活动,并且有一个convoluted implementation on Stack Overflow,但一般来说它很混乱,通常不应该在网页设计中穿过,不可能拉开完全适用于所有浏览器,并且可以毫不费力地规避。

设置字符速率限制并检测垃圾邮件的红旗,比如高链接密度和重复,这是一个好主意。