1/**
2 * Append a toolbar button
3 */
4if (window.toolbar !== undefined) {
5    toolbar[toolbar.length] = {
6        "type": "pluginvshare",
7        "title": LANG['plugins']['vshare']['button'],
8        "icon": "../../plugins/vshare/button.png",
9        "key": ""
10    };
11}
12
13/**
14 * Try to determine the video service, extract the ID and insert
15 * the correct syntax
16 */
17function tb_pluginvshare(btn, props, edid) {
18    PluginVShare.edid = edid;
19    PluginVShare.buildSyntax();
20}
21
22const PluginVShare = {
23    edid: null,
24
25    /**
26     * Ask for URL, extract data and create syntax
27     */
28    buildSyntax: function () {
29
30        const text = prompt(LANG['plugins']['vshare']['prompt']);
31        if (!text) return;
32
33        for (const [site, rex] of Object.entries(JSINFO.plugins.vshare)) {
34                const RE = new RegExp(rex, 'i');
35                const match = text.match(RE);
36                if (match) {
37                    const urlparam = '';
38                    const videoid = match[1];
39                    PluginVShare.insert(site, videoid, urlparam);
40                    return;
41                }
42        }
43
44        alert(LANG['plugins']['vshare']['notfound']);
45    },
46
47    /**
48     * Insert the syntax in the editor
49     *
50     * @param {string} key
51     * @param {string} videoid
52     * @param {string} urlparam
53     */
54    insert: function (key, videoid, urlparam) {
55        const code = '{{' + key + '>' + videoid + '?' + urlparam + '}}';
56        insertAtCarret(PluginVShare.edid, code);
57    },
58
59    /**
60     * Allow loading videos on click
61     */
62    attachGDPRHandler: function () {
63        const $videos = jQuery('div.vshare');
64
65        // add click handler
66        $videos.on('click', function () {
67            // create an iframe and copy over the attributes
68            const iframe = document.createElement('iframe');
69            let attr;
70            let attributes = Array.prototype.slice.call(this.attributes);
71            while(attr = attributes.pop()) {
72                iframe.setAttribute(attr.nodeName, attr.nodeValue);
73            }
74            // replace the div with the iframe
75            this.replaceWith(iframe);
76        });
77
78        // add info text
79        $videos.each(function (){
80            const $self = jQuery(this);
81            const info = document.createElement('p');
82            info.innerText = LANG.plugins.vshare.click.replace('%s', $self.data('domain'));
83            $self.append(info);
84        });
85    }
86};
87
88jQuery(function () {
89    PluginVShare.attachGDPRHandler();
90});
91