4

Im trying to get the users copied text and rewrite what they paste.

example....

<p>This is some awesome text that i wrote</p>

say the user copies the words "awesome text"

I want to take the words "awesome text" and add on " - from my mywebsite.com"

So now when users paste this text copied from my website it will say, "awesome text - from my mywebsite.com"

I've googled around this is what I got so far http://jsfiddle.net/YD88T/

bghouse
  • 558
  • 2
  • 8
  • 26
  • User privacy violation.. Isn't it? I don't think this is possible with just JS (a flash/applet addon can maybe help). – techfoobar Apr 26 '13 at 13:50

2 Answers2

2

Perhaps this code could do the trick. It appends your text to the user selection before the user actually copies it.

jQuery.fn.addtocopy = function(usercopytxt) {
    var options = {htmlcopytxt: '<br>More: <a href="'+window.location.href+'">'+window.location.href+'</a><br>', minlen: 25, addcopyfirst: false}
    $.extend(options, usercopytxt);
    var copy_sp = document.createElement('span');
    copy_sp.id = 'ctrlcopy';
    copy_sp.innerHTML = options.htmlcopytxt;
    return this.each(function(){
        $(this).mousedown(function(){$('#ctrlcopy').remove();});
        $(this).mouseup(function(){
            if(window.getSelection){    //good times
                var slcted=window.getSelection();
                var seltxt=slcted.toString();
                if(!seltxt||seltxt.length<options.minlen) return;
                var nslct = slcted.getRangeAt(0);
                seltxt = nslct.cloneRange();
                seltxt.collapse(options.addcopyfirst);
                seltxt.insertNode(copy_sp);
                if (!options.addcopyfirst) nslct.setEndAfter(copy_sp);
                slcted.removeAllRanges();
                slcted.addRange(nslct);
            } else if(document.selection){  //bad times
                var slcted = document.selection;
                var nslct=slcted.createRange();
                var seltxt=nslct.text;
                if (!seltxt||seltxt.length<options.minlen) return;
                seltxt=nslct.duplicate();
                seltxt.collapse(options.addcopyfirst);
                seltxt.pasteHTML(copy_sp.outerHTML);
                if (!options.addcopyfirst) {nslct.setEndPoint("EndToEnd",seltxt); nslct.select();}
            }
        });
  });
}

I've taken it from here: http://naviny.by/js/main.min.js

A demo (select at least 25 characters)

Raman Chodźka
  • 558
  • 1
  • 7
  • 16
  • 1
    Once you have added this custom function to jQuery, you can simply use it with : $(document).ready(function() { $('#areaCopyCan').addtocopy({ htmlcopytxt: '

    more: ' + window.location.href + '', minlen: 35, addcopyfirst: false }); });
    – Cedric Jun 06 '13 at 08:43
  • 1
    And the css : #ctrlcopy{position: absolute; left: -100px; top: -100px; /**I have used the position absolute with negative positionning, instead of a 0 size and opacity 0 that were used on naviny.by . That's because in some text editors (word,...), the size and opacity may be kept, while not the negative positionning*/ – Cedric Jun 06 '13 at 08:45
-1

If you are the developer of the site that had the

  <p>This is some awesome text that i wrote</p> 

you can make a script that catches the right click event, replace the normal menu with one made by you, and also catch the ctrl+c combination and do whatever you want from there.

If you are not... you can't.

zozo
  • 8,230
  • 19
  • 79
  • 134
  • Replacing the context menu is a really bad idea. I don't know what ctrl-p (print) has to do with the question; if you meant ctrl-c, you can't intercept keypresses at that level on most browsers. – JJJ Apr 27 '13 at 13:40
  • I meant ctrl+c. Also doesn't really matter the combination. Is the event you want to intercept. Also you CAN intercept keypresses on most browsers (personally did it at least 2 times). About the context menu... yes is a bad idea. But also is the only available one. Bad >>>>> None. So I really don't get the downvote here (not that I care but I really don't get downvoting a perfectly valid solution). I'm beginning to think that this community is going down. – zozo Apr 27 '13 at 21:30
  • Also, this doesn't answer the question: the "do whatever you want from there" part is the problem, because you can't modify the user's clipboard directly. In other words, once you've intercepted the copy event, then what? – JJJ Apr 28 '13 at 06:20