views:

60

answers:

4

Hello,

I would like to use JavaScript to clean up text that’s being copied from my site.

I use snippets like this:

body {
    vertical-align: middle; ➊
}

Where ➊ indicates comment later on. I want readers to copy this snippet and use it – so I need to delete that Unicode marker. How can I access text that’s being copied and make changes to it?

I considered deleting marker(s) from snippet when user clicks (mousedown) on it, so she could select the text, copy it and then I would restore markers but it seems a really long way to do it.

A: 

just set the markers in comment? so it doesn't do any harm when being used after copying

Tim Mahy
Thumbs up for the idea, but that’s not what I’m looking for.
riddle
+1  A: 

You could turn the unicode marker into an image, as images are ignored when copying plain text.

SLC
That’s actually something I might try. Thanks!
riddle
+2  A: 

Just put the unicode markers in span tags, and put display none on them when the user clicks

body {
    vertical-align: middle; <span class="marker">➊</span>
}

And then do this in jQuery

$('.code')
    .mousedown(function() {
        $(this).find('.marker').css('display','none');
    })
    .mouseleave(function() {
        $(this).find('.marker').css('display','inline');
    });

As a bonus, you could then apply the following style to the .marker elements:

​.marker
{
    position:absolute;   
    right:0;
}​
Eric
IMHO this seams to be the most resonable way! +1
aSeptik
Have you tested it successfully? I can’t make it work (I’m proficient with JavaScript, so code executes but I still copy markers.)
riddle
http://www.jsfiddle.net/92En5/ Definitely works in chrome. Maybe not in others
Eric
riddle
Ok, fixed. Used mouse leave to prevent premature re-showing.
Eric
A: 

There is an oncopy handler, but I doubt it is widely supported. There are also selection event handlers like onselectstart (again, different for different browsers) and various attributes to make a part of the text unselectable, like -moz-user-select: none (yet again, not cross-browser). You are probably better of using absolutely positioned markers or making the marker unaccessible through z-index.

Tgr
User-select: none works in Mozilla and WebKit and yes, it doesn’t select markers. But they’re copied anyway. :/
riddle