autoembed.js
3.53 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* This extension auto embeds urls.
*/
(function () {
angular.module('footyroom')
.directive('autoEmbed', ['urlEmbedder', function(urlEmbedder) {
return {
link: function(scope, element, attrs) {
if (urlEmbedder.get(attrs.href) === undefined) {
var spinner = angular.element('<i class="spinner"></spinner>').appendTo(element);
}
urlEmbedder.fetch(attrs.href)
.then(function(embed) {
if (embed) {
element.replaceWith(iframeEmbed(embed, attrs.href));
}
})
.finally(function() {
spinner.remove();
})
}
};
}])
.service('urlEmbedder', ['$http', '$q', function($http, $q) {
var service = this;
service.cache = {};
service.get = get;
service.fetch = fetch;
function fetch(url) {
if (service.cache[url] !== undefined) {
return $q.when(this.cache[url]);
}
return $http.get('/embed?url=' + url)
.then(function(response) {
return service.cache[url] = response.data.html;
})
.catch(function() {
return service.cache[url] = null;
});
}
function get(url) {
return service.cache[url];
}
}]);
function init() {
var height;
var src = window.frameElement.getAttribute('data-src');
setInterval(function () {
resize();
reload();
}, 1000);
function resize() {
var current = window.document.documentElement.offsetHeight;
if (height !== current) {
window.frameElement.style.height = current + "px";
}
height = current;
}
function reload() {
var current = window.frameElement.getAttribute('data-src');
if (src !== current) {
window.frameElement.src = "javascript: unescape(window.frameElement.getAttribute('src-doc'));";
}
src = current;
}
}
function iframeEmbed(embed, url) {
var i = '(' + init.toString() + ')();';
return '<div class="embed-wrap"><iframe data-src="'+ url +'"' +
'src-doc="<!DOCTYPE html><html><head></head><body><script>'+ escape(i) + '<\/script>' + escape(embed) +'</body></html>"' +
'src="javascript: unescape(window.frameElement.getAttribute(\'src-doc\'));"' +
'frameborder="0" width="100%" allowfullscreen></iframe></div>';
}
function autoEmbed() {
const urlEmbedder = angular.injector(['ng', 'footyroom']).get('urlEmbedder');
return [{
type: 'lang',
filter: function(text) {
return text.replace(/^((?:[-+*] |\d+\. |>)? *)((?:https:\/\/|www\.)[^'">\s]+\.[^'">\s]+)( *$)/gm, function(match, p1, p2, p3) {
var embed = urlEmbedder.get(p2);
if (!embed) {
return p1 + '<div class="p" href="'+ p2 +'" auto-embed><a href="'+ p2 +'">'+ p2 + '</a></div>' + p3;
}
return iframeEmbed(embed, p2);
});
}
}];
}
if (window && window.showdown) {
window.showdown.extensions.autoembed = autoEmbed;
}
if (typeof module !== 'undefined') {
module.exports = autoEmbed;
}
})();