Disable selection, allow copy / paste

I am creating an application that overrides the standard selection behavior and allows you to copy and paste elements. The problem is that if I turn off the selection, copies of events will also disappear.

I tried to use

onselectstart="return false;"

and

.no-select {     
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;  
}

and it works, but also disables the copy event.

I also tried to add an attribute .no-selectonly for these parts containing text, but it is difficult to maintain and does not work well - sometimes copy events are blocked, and I can not control it.

How to disable selection, but allow copy / paste the correct path?


Edit:

  • I do not want to copy the text, but my own json structures. Copying is done in the handler onCopy.
  • -, - .
+6
1

/, ?

( ), json

2 :

  • ( )

    if (document.addEventListener) {
            document.addEventListener('contextmenu', function(e) {
                alert("Write own menu with copy");
                e.preventDefault();
            }, false);
        } else {
            document.attachEvent('oncontextmenu', function() {
                alert("Write own menu with copy");
                window.event.returnValue = false;
            });
        }
    body {     
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;  
    }
    <body>
    Some text
    </body>
  • "" ( )

  • ctrl + c ( command + c) ( )
  • Flash ( )
+1

Source: https://habr.com/ru/post/1015247/


All Articles