Domanda

I'm working on a chat app with Meteor and I don't want users to be able to copy/paste things into the form for obvious spam reasons. Is this possible? Here is the code I use to run the chat app:

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;
        }
    }
}

I have no idea where to even start with this. How do you prevent copy/pasting?

È stato utile?

Soluzione

It's not a good idea. Internet Explorer has a onpaste event, and there's been a convoluted implementation on Stack Overflow, but in general it's messy, crosses lines that generally shouldn't be crossed in web design, impossible to pull off completely across all browsers, and can be circumvented without too much trouble.

It's a far better idea to put character-rate limits and detect red flags for spam, like high link density and repetition.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top