4

i'm using tinymce RTF editor on my website. i want to disable copy/paste option in tinymce textarea. i found this method on stackoverflow but it didn't work for me.

How to Prevent/disable copy and paste in Tinymce

document.addEventListener('paste', function(e){
   e.preventDefault(); 
});
madara uchiha
  • 202
  • 2
  • 9
  • 1
    See the comment under the answer: "... your second code answer: 'paste_preprocess' is working perfectly. I help me a lot". Use the second solution. – Tim VN May 09 '19 at 10:02
  • @TimVN i tried that one too. may I'm not using it in correct way – madara uchiha May 09 '19 at 10:09

4 Answers4

5

You should be able to use paste_preprocess if you include the paste plugin. If you're using paste_preprocess, make sure you're passing it as an option to tinymce.init(), and also including the plugin. For example:

tinymce.init({
    selector: "textarea",
    plugins: [
        "advlist autolink lists link image charmap print preview anchor",
        "searchreplace visualblocks code fullscreen",
        "insertdatetime media table contextmenu paste"
    ],
    paste_preprocess: function (plugin, args) {
        console.log("Attempted to paste: ", args.content);
        // replace copied text with empty string
        args.content = '';
    },
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});

See this updated fiddle for an example.

JoshG
  • 6,472
  • 2
  • 38
  • 61
  • none of these answers block paste. it just pastes in blank text. For example if the user selects some text, and then attempts to paste in something new, it just replaces the selected text with an empty string, effectively deleting the selection. – user323774 Jun 30 '21 at 22:05
  • @user323774 I updated the link to the fiddle, as the original link seemed to no longer go to the correct fiddle. However, I still cannot reproduce what you described. If I go to the linked fiddle, type text in the text editor, highlight it, CTRL+C, move cursor to end of the text, and CTRL+V, nothing is added to the text area. You can confirm this by viewing the source as well. – JoshG Jul 01 '21 at 02:53
3

Earlier answers suggesting to replace args.contents = '' do not actually prevent a paste operation, they rather alter the contents of the paste to an empty string which still gets pasted.

TinyMCE Event Doc

This actually prevents the paste completely.

paste_preprocess: (plugin, args) => {
    args.stopImmediatePropagation();
    args.stopPropagation();
    args.preventDefault();
});
user323774
  • 400
  • 3
  • 9
2

As previously answered, you can use paste_preprocess. However, you'll need to add paste to plugins.

Example:

tinymce.init({
  ...,
  plugins: [
    "paste"
  ],
  paste_preprocess: function (plugin, args) {
    console.log(args.content);
    args.content = '';
  }
});
Tim VN
  • 1,183
  • 1
  • 7
  • 19
  • You can open the javascript console by hitting ```CTRL``` + ```SHIFT``` + ```J```. See the error. If you directly copied this piece of code, it won't work. I put ```...``` in place of your other configuration. – Tim VN May 09 '19 at 10:59
0

You can intercept paste in the tinymce.init

paste_preprocess: function(plugin, args) {
    console.log(args.content);
    args.content = '';
  }
Daniel Smith
  • 1,626
  • 3
  • 29
  • 59
  • i tried this method but it doesn't work for me. im using this like this tinymce.init({ paste_preprocess: function(plugin, args) { console.log(args.content); args.content = ''; } }); – madara uchiha May 09 '19 at 10:06