kaldata video embed

Try to auto embed video files.

Mint 2019.02.08.. Lásd a legutóbbi verzió

// ==UserScript==
// @name kaldata video embed
// @description Try to auto embed video files.
// @author bornofash
// @match https://www.kaldata.com/forums/*
// @require	https://code.jquery.com/jquery-3.3.1.min.js
// @require https://greasyfork.org/scripts/2199-waitforkeyelements/code/waitForKeyElements.js?version=6349
// @version 0.0.9
// @namespace https://greasyfork.org/users/196421
// ==/UserScript==

(function() {
    'use strict';
    waitForKeyElements('div.cke_editable a[ipsnoembed]', embed);

    function embed(link) {
        if (link.closest('video').length !== 0) return 0;
        var video = put_in_video(link);
        if (video === 'stop') return 0;
        show_menu_for(video);
    }

    function put_in_video(source) {
        var src = source.attr('href');
        /* Embed only links containing file extention */
        if (src.indexOf('.mp4') !== -1) {
            var type = 'type="video/mp4"';
        } else if (src.indexOf('.webm') !== -1) {
            type = 'type="video/webm"';
        } else if (src.indexOf('v.redd.it') !== -1) {
            type = ''
        } else {
            return 'stop';
        }

        /* Create necessary tags */
        var div_tag = $('<div class="ipsEmbeddedVideo"></div>');
        var video_tag = $('<video controls contenteditable="false"></video>');
        var source_tag = $('<source src=' + src + '#t=0.1 ' + type + '>');
        /* Connect created tags */
        div_tag.append(video_tag);
        video_tag.append(source_tag);
        video_tag.append(src); // show link if video is not supported
        /* Style video */
       	video_tag.css({
            'max-width': '100%',
            'width':'auto',
        });

        source.parent().replaceWith(div_tag);
        div_tag.after($('<br>'));
        //source.remove();

        return $(video_tag);
    }

    function show_menu_for(video) {
        /* Create necessary tags */

        var embedMessage = $('<div class="ipsHide ipsComposeArea_editorPaste" data-role="embedMessage"></div>');
        var ipsPad_half = $('<p class="ipsType_reset ipsPad_half"></p>');
        var close = $('<a class="ipsPos_right ipsType_normal ipsCursor_pointer ipsComposeArea_editorPasteSwitch">×</a>');
        var info = $('<i class="fa fa-cog" aria-hidden="true"></i>');
        /* Size */
        var small = $('<a class="ipsCursor_pointer ipsMarked_size">Малък</a>');
        var big = $('<a class="ipsCursor_pointer ipsMarked_size">Голям</a>');

        /* Add functions to the links */
        function resize(size_tag, px) {
            size_tag.click(function() {
                $('.ipsMarked_size').css('filter', 'opacity(100%)');
                video.animate({
                    width: px
                }, 500);
                $(this).css('filter', 'opacity(30%)');
            });
        }
        resize(small, '300px');
        resize(big, '600px');

        close.click(function() {
            $('[data-role="embedMessage"]').slideUp();
        });

        video.bind("DOMNodeRemoved", function () {
            embedMessage.slideUp();
        });

        /* Connect created tags */
        embedMessage.append(ipsPad_half);
        ipsPad_half.append(close);
        ipsPad_half.append(info);

        ipsPad_half.append(' Размер: ');
        ipsPad_half.append(small); small.after(' ');
        ipsPad_half.append(big); big.after(' ');

        video.closest('[data-role="mainEditorArea"]').after(embedMessage);
        embedMessage.slideDown();
    }
})();