【问题标题】:How to Disable Copy/Paste in tinymce如何在tinymce中禁用复制/粘贴
【发布时间】:2019-05-09 09:58:37
【问题描述】:

我在我的网站上使用 tinymce RTF 编辑器。我想禁用 tinymce textarea 中的复制/粘贴选项。我在 stackoverflow 上找到了这个方法,但它对我不起作用。

How to Prevent/disable copy and paste in Tinymce

document.addEventListener('paste', function(e){
   e.preventDefault(); 
});

【问题讨论】:

  • 查看答案下的评论:“...您的第二个代码答案:'paste_preprocess' 运行良好。我帮了我很多忙”。使用第二种解决方案。
  • @TimVN 我也试过了。我可能没有以正确的方式使用它

标签: javascript jquery angularjs codeigniter


【解决方案1】:

如果您包含paste 插件,您应该可以使用paste_preprocess。如果您使用paste_preprocess,请确保将其作为选项传递给tinymce.init(),并且还包括插件。例如:

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"
});

有关示例,请参阅 this updated fiddle

【讨论】:

  • 这些答案都不会阻止粘贴。它只是粘贴在空白文本中。例如,如果用户选择了一些文本,然后尝试粘贴新的内容,它只是将选定的文本替换为空字符串,从而有效地删除了选择。
  • @user323774 我更新了指向小提琴的链接,因为原始链接似乎不再指向正确的小提琴。但是,我仍然无法重现您所描述的内容。如果我转到链接的小提琴,在文本编辑器中键入文本,突出显示它,CTRL+C,将光标移动到文本末尾,然后 CTRL+V,文本区域不会添加任何内容。您也可以通过查看源代码来确认这一点。
【解决方案2】:

建议替换 args.contents = '' 的早期答案实际上并不能阻止粘贴操作,而是将粘贴的内容更改为仍被粘贴的空字符串。

TinyMCE Event Doc

这实际上完全阻止了粘贴。

paste_preprocess: (plugin, args) => {
    args.stopImmediatePropagation();
    args.stopPropagation();
    args.preventDefault();
});

【讨论】:

    【解决方案3】:

    如前所述,您可以使用paste_preprocess。但是,您需要将paste 添加到plugins

    例子:

    tinymce.init({
      ...,
      plugins: [
        "paste"
      ],
      paste_preprocess: function (plugin, args) {
        console.log(args.content);
        args.content = '';
      }
    });
    

    【讨论】:

    • 您可以通过点击 CTRL + SHIFT + J 打开 JavaScript 控制台。查看错误。如果你直接复制这段代码,它是行不通的。我用... 代替了你的其他配置。
    【解决方案4】:

    你可以拦截tinymce.init中的粘贴

    paste_preprocess: function(plugin, args) {
        console.log(args.content);
        args.content = '';
      }
    

    【讨论】:

    • 我试过这个方法,但它对我不起作用。我像这样使用 tinymce.init({ paste_preprocess: function(plugin, args) { console.log(args.content); args.content = ''; } });
    猜你喜欢
    • 2019-09-24
    • 2012-12-26
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    相关资源
    最近更新 更多