1

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.

riddle
  • 655
  • 1
  • 6
  • 9

4 Answers4

2

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
  • 95,302
  • 53
  • 242
  • 374
1

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

NibblyPig
  • 51,118
  • 72
  • 200
  • 356
0

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

Tim Mahy
  • 1,319
  • 12
  • 28
0

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
  • 27,442
  • 12
  • 81
  • 118
  • User-select: none works in Mozilla and WebKit and yes, it doesn’t select markers. But they’re copied anyway. :/ – riddle May 08 '10 at 12:22