xref: /plugin/vshare/script.js (revision 1dcea37aa2d4036959e9f3477889a33862b919be)
1
2/**
3 * Append a toolbar button
4 */
5if(window.toolbar != undefined){
6    toolbar[toolbar.length] = {"type":  "pluginvshare",
7                               "title": LANG['plugins']['vshare']['button'],
8                               "icon":  "../../plugins/vshare/button.png",
9                               "key":   ""};
10}
11
12/**
13 * Try to determine the video service, extract the ID and insert
14 * the correct syntax
15 */
16function tb_pluginvshare(btn, props, edid) {
17    PluginVShare.edid = edid;
18
19    PluginVShare.buildSyntax();
20}
21
22var PluginVShare = {
23    edid: null,
24
25    buildSyntax: function () {
26
27        var text = prompt(LANG['plugins']['vshare']['prompt']);
28        if (!text) return;
29
30        // This includes the site patterns:
31        /* DOKUWIKI:include sites.js */
32
33        for (var key in sites) {
34
35            if(sites.hasOwnProperty(key)) {
36                var RE = new RegExp(sites[key], 'i');
37                var match = text.match(RE);
38                if (match) {
39                    var urlparam = '';
40                    var videoid = match[1];
41
42                    switch (key) {
43                        case 'slideshare':
44                            //provided video url?
45                            if(match[2]) {
46
47                                jQuery.ajax({
48                                    url: '//www.slideshare.net/api/oembed/2',
49                                    dataType: 'jsonp',
50                                    data: {
51                                        url: match[2],
52                                        format: 'jsonp'
53                                    }
54                                }).done(function (response, status, error) {
55                                    var videoid = response.slideshow_id;
56                                    PluginVShare.insert(key, videoid, urlparam);
57                                }).fail(function (data, status, error) {
58                                    /* http://www.slideshare.net/developers/oembed
59                                     * If not found, an status 200 with response {error:true} is returned,
60                                     * but "Content-Type:application/javascript; charset=utf-8" is then
61                                     * wrongly changed to "Content-Type:application/json; charset=utf-8"
62                                     * so it throws a parseerror
63                                     */
64                                    alert(LANG['plugins']['vshare']['notfound']);
65                                });
66                                return;
67                            }
68                            break;
69                        case 'bliptv':
70                            //provided video url?
71                            if(match[2]) {
72
73                                jQuery.ajax({
74                                    url: '//blip.tv/oembed/',
75                                    dataType: 'jsonp',
76                                    data: {
77                                        url: match[2],
78                                        format: 'json'
79                                    },
80                                    timeout: 2000
81                                }).done(function (response, status, error) {
82                                    var videoidmatch = response.html.match(RE);
83                                    PluginVShare.insert(key, videoidmatch[1], urlparam);
84                                }).fail(function (data, status, error) {
85                                    /*
86                                     * If url is not found(=wrong numerical number on end), blip.tv returns a 404
87                                     * because jsonp is not a xmlhttprequest, there is no 404 catched
88                                     * errors are detected by waiting at the timeout
89                                     */
90                                    alert(LANG['plugins']['vshare']['notfound']);
91                                });
92                                return;
93                            }
94                            break;
95                        case 'twitchtv':
96                            if (match[2]) {
97                                urlparam = '&chapter_id=' + match[2];
98                            }
99                            break;
100                    }
101
102                    PluginVShare.insert(key, videoid, urlparam);
103                    return;
104                }
105            }
106        }
107
108        alert(LANG['plugins']['vshare']['notfound']);
109    },
110
111    insert: function(key, videoid, urlparam, edid) {
112        var code = '{{' + key + '>' + videoid + '?medium' + urlparam + '}}';
113        insertAtCarret(PluginVShare.edid, code);
114    }
115};
116
117