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