Просмотр исходного кода

use oembed for vimeo and soundcloud.

youtube doesn't allow cross-origin requests, so we have to use the
old method for now.
Lucas Stadler лет назад: 11
Родитель
Сommit
bd190e0d4b
1 измененных файлов с 22 добавлено и 30 удалено
  1. 22 30
      js/babl/public/index.html

+ 22 - 30
js/babl/public/index.html

@ -37,6 +37,26 @@
37 37
				}
38 38
			}
39 39
40
			function oembedElement(url, oembedEndpoint) {
41
				var el = document.createElement('a');
42
				el.href = url;
43
				el.textContent = url;
44
45
				var xhr = new XMLHttpRequest();
46
				xhr.open('GET', oembedEndpoint + '?format=json&maxwidth=450&url=' + url);
47
				xhr.onreadystatechange = function() {
48
					if (xhr.readyState == XMLHttpRequest.DONE) {
49
						console.log(url, xhr.status);
50
						var tmpEl = document.createElement('div');
51
						tmpEl.innerHTML = JSON.parse(xhr.responseText).html;
52
						el.parentElement.replaceChild(tmpEl.firstChild, el);
53
					}
54
				};
55
				xhr.send();
56
57
				return el;
58
			}
59
40 60
			var word_matchers = [{
41 61
				re: /https?:\/\/.*youtube.com\/watch\?v=([a-zA-Z0-9-]+).*/,
42 62
				fn: function(match) {
@ -49,38 +69,10 @@
49 69
				}
50 70
			}, {
51 71
				re: /https?:\/\/.*soundcloud.com\/.*/,
52
				fn: function(match, msgEl) {
53
					var el = document.createElement('a');
54
					el.href = match[0];
55
					el.textContent = match[0];
56
57
					var xhr = new XMLHttpRequest();
58
					xhr.open('GET', 'https://soundcloud.com/oembed?format=json&maxwidth=450&maxheight=315&url=' + match[0]);
59
					xhr.onreadystatechange = function() {
60
						if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
61
							var data = JSON.parse(xhr.responseText);
62
63
							var tmpEl = document.createElement("div");
64
							tmpEl.innerHTML = data.html;
65
66
							msgEl.replaceChild(tmpEl.firstChild, el);
67
						}
68
					}
69
					xhr.send();
70
71
					return el;
72
				}
72
				fn: function(match) { return oembedElement(match[0], "https://soundcloud.com/oembed"); }
73 73
			}, {
74 74
				re: /https?:\/\/vimeo.com\/([0-9]+).*/,
75
				fn: function(match) {
76
					var el = document.createElement("iframe");
77
					el.src = "//player.vimeo.com/video/" + match[1];
78
					el.sandbox = "allow-same-origin allow-scripts allow-popups";
79
					el.width = 420;
80
					el.height = 315;
81
					el.setAttribute("frameborder", 0);
82
					return el;
83
				}
75
				fn: function(match) { return oembedElement(match[0], "http://vimeo.com/api/oembed.json"); }
84 76
			}, {
85 77
				re: /https?:\/\/.*(png|gif|jpg|jpeg)$/i,
86 78
				fn: function(match) {