简体   繁体   English

仅允许TinyMCE中的外部链接

[英]Allow only external links in TinyMCE

I'm using TinyMCE in my webapplication, and allowing people to use link in it. 我在我的webapplication中使用TinyMCE ,并允许人们在其中使用link This is my config: 这是我的配置:

        var editor = tinymce.init({
        plugins: "link",
        selector: this.$el.find("#shortdesc")["selector"],
        toolbar: "bold italic | undo redo | link unlink",
        link_list: [
        ],
        menubar: false,
        relative_urls: false,
        link_assume_external_targets: true,
        setup: function (editor) {
            editor.on("change", function (e) {})
        }
    });

The issue I'm trying to solve is that I want let people insert only external links . 我试图解决的问题是我想让人们只插入外部链接 In the current situation, when the user click on the link button and confirm, it shows this popup 在当前情况下,当用户单击link按钮并确认时,它会显示此弹出窗口

在此输入图像描述

My goal is to avoid showing this popup and use only http:// prefixed links. 我的目标是避免显示此弹出窗口并仅使用http://前缀链接。

I'm using the last version of tinyMCE . 我正在使用最新版本的tinyMCE

As far as I could understand relative_urls options does not fit my necessities. 据我所知, relative_urls选项不适合我的必需品。

Any ideas? 有任何想法吗?

Actually I solved overriding the tinymce.editor.convertURL function 实际上我解决了覆盖tinymce.editor.convertURL function

             setup: function (editor) {
                var fn = editor.convertURL;
                editor.convertURL = convertURL_;
                function convertURL_(url, name, elm){
                    fn.apply(this, arguments);
                    console.log(arguments);
                    var regex = new RegExp("(http:|https:)?\/\/");
                    if (!regex.test(url)) {
                        return url = "http://" + url
                    }
                    return url;
                }
             }

Based on an answer found here , I wrote the following compact solution for your problem: 基于此处的答案,我为您的问题编写了以下紧凑型解决方案:

editor.on('init',function(e) {

    // reference to original windowManager function
    var fn = editor.windowManager.open;

    // override with your own version of the function
    editor.windowManager.open = function(t,r){

        // make sure you only target the 'insert link' dialog
        if(t.title == 'Insert link'){

            // reference to the subumit function of the dialog
            var oldsubmit = t.onSubmit;

            // override the submit function
            t.onSubmit = function(e){

                // append the "http://" prefix here, note that the URL is contained within the property 'href' of data. 
                // also see link/plugin.js
                if(!e.data.href.match(/(ftp|https?):\/\//i)){
                    e.data.href = "http://" + e.data.href;
                }

                // submit original function
                return oldsubmit(e);
            }

            // after overwriting the submit function within the windowManager, make sure to call the original function
            fn.apply(this, [t,r]);
        }

        // use return instead of apply to prevent bugs in other dialogs
        else{
            return fn(t,r);
        }
    }
});

您应该复制链接插件,将其重命名为“mylink”,调整所有引用以链接到mylink并修改代码,以便弹出窗口不会显示,并且链接URL被检查为“https”/“http”,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM