如何防止/禁用Tinymce中的复制和粘贴

时间:2019-05-07 00:29:03

标签: javascript html tinymce

我正在系统上设置tinymce,并希望在tinymce编辑器中为用户禁用复制和粘贴,但是找不到解决方案。如何禁用tinymce中的复制粘贴

我已经实施了 Disable pasting text into HTML form 但是它仅在简单的文本区域有效,而在tinymce文本区域无效

<script>
// Register onpaste on inputs and textareas in browsers that don't
// natively support it.
(function () {
    var onload = window.onload;

    window.onload = function () {
        if (typeof onload == "function") {
            onload.apply(this, arguments);
        }

        var fields = [];
        var inputs = document.getElementsByTagName("input");
        var textareas = document.getElementsByTagName("textarea");

        for (var i = 0; i < inputs.length; i++) {
            fields.push(inputs[i]);
        }

        for (var i = 0; i < textareas.length; i++) {
            fields.push(textareas[i]);
        }

        for (var i = 0; i < fields.length; i++) {
            var field = fields[i];

            if (typeof field.onpaste != "function" && !!field.getAttribute("onpaste")) {
                field.onpaste = eval("(function () { " + field.getAttribute("onpaste") + " })");
            }

            if (typeof field.onpaste == "function") {
                var oninput = field.oninput;

                field.oninput = function () {
                    if (typeof oninput == "function") {
                        oninput.apply(this, arguments);
                    }

                    if (typeof this.previousValue == "undefined") {
                        this.previousValue = this.value;
                    }

                    var pasted = (Math.abs(this.previousValue.length - this.value.length) > 1 && this.value != "");

                    if (pasted && !this.onpaste.apply(this, arguments)) {
                        this.value = this.previousValue;
                    }

                    this.previousValue = this.value;
                };

                if (field.addEventListener) {
                    field.addEventListener("input", field.oninput, false);
                } else if (field.attachEvent) {
                    field.attachEvent("oninput", field.oninput);
                }
            }
        }
    }
})();
</script>       

    </head>
    <body>
<!-- Not Working here-->
        <textarea class="tinymce" onpaste="return false;"></textarea>

        <!-- javascript -->
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="plugin/tinymce/tinymce.min.js"></script>
        <script type="text/javascript" src="plugin/tinymce/init-tinymce.js"></script>

<!-- Working here-->        
<textarea onpaste="return false;"></textarea>



    </body>

我希望它应该在tinymce的文本区域中起作用,

提前谢谢您,我将非常感激

1 个答案:

答案 0 :(得分:0)

您是否尝试阻止默认设置

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

我还读到您可以拦截tinymce.init

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

希望其中一种方法对您有效