aboutsummaryrefslogtreecommitdiffstats
path: root/addon/oembed/oembed.js
blob: 26541490b18fc01b7cb570f1996175bb17336062 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function oembed(){
  $("#oembed").toggleClass('hide');
}

function oembed_do(){
  embedurl = $('#oembed_url').attr('value');
  var url = 'http://oohembed.com/oohembed/?url='+escape( embedurl )+"&callback=?";
  
  $.getJSON(url, function(data) {
    var ret="";
    switch(data.type){
      case "video": {
        if (data.thumbnail_url){
          tw = 200; if (data.thumbnail_width) tw=data.thumbnail_width;
          th = 180; if (data.thumbnail_height) tw=data.thumbnail_height;
          ret = "<a href='"+embedurl+"'>";
          // tiny mce bbcode plugin not support image size......
          ret += "<img width='"+tw+"' height='"+th+"' src='"+data.thumbnail_url+"'></a>";
        } else {
          ret = data.html;  
        }
      }; break;
      case "photo": {
          // tiny mce bbcode plugin not support image size......        
          ret = "<img width='"+data.width+"' height='"+data.height+"' src='"+data.url+"'>";
      }; break;
      case "link": {
          ret = "<a href='"+embedurl+"'>"+data.title+"</a>";
      }; break;
      case "rich": {
          ret = data.html; // not so safe... http://www.oembed.com/ : "Consumers may wish to load the HTML in an off-domain iframe to avoid XSS" 
      }; break;
      default: {
        alert("Error retriving data!");
        return;
      }
    }
    var embedlink = embedurl;
    if (data.title) embedlink = data.title
    ret+="<br><a href='"+embedurl+"'>"+embedlink+"</a>";
    if (data.author_name) {
      ret+=" by "+data.author_name;
    }
    if (data.provider_name) {
      ret+=" on "+data.provider_name;
    }
    tinyMCE.execCommand('mceInsertRawHTML',false,ret);
    oembed();
  });
  
}