@@ -4,7 +4,6 @@ customElements.define(
4
4
connectedCallback ( ) {
5
5
let shadow = this . attachShadow ( { mode : "closed" } ) ;
6
6
const urlAttr = this . getAttribute ( "url" ) ;
7
- let urlToEmbed ;
8
7
if ( urlAttr ) {
9
8
renderOembed ( shadow , urlAttr , {
10
9
maxwidth : this . getAttribute ( "maxwidth" ) ,
@@ -13,30 +12,36 @@ customElements.define(
13
12
} else {
14
13
const discoverUrl = this . getAttribute ( "discover-url" ) ;
15
14
if ( discoverUrl ) {
16
- const discoveredUrl = getDiscoverUrl ( discoverUrl , function (
17
- discoveredUrl
18
- ) {
19
- renderOembed ( shadow , discoveredUrl ) ;
15
+ getDiscoverUrl ( discoverUrl , function ( discoveredUrl ) {
16
+ renderOembed ( shadow , discoveredUrl , null ) ;
20
17
} ) ;
21
18
}
22
19
}
23
20
}
24
21
}
25
22
) ;
26
23
24
+ /**
25
+ *
26
+ * @param {ShadowRoot } shadow
27
+ * @param {string } urlToEmbed
28
+ * @param {{maxwidth: string?; maxheight: string?}? } options
29
+ */
27
30
function renderOembed ( shadow , urlToEmbed , options ) {
28
- let apiUrl = new URL ( `https://cors-anywhere.herokuapp.com/${ urlToEmbed } ` ) ;
31
+ let apiUrlBuilder = new URL (
32
+ `https://cors-anywhere.herokuapp.com/${ urlToEmbed } `
33
+ ) ;
29
34
if ( options && options . maxwidth ) {
30
- apiUrl . searchParams . set ( "maxwidth" , options . maxwidth ) ;
35
+ apiUrlBuilder . searchParams . set ( "maxwidth" , options . maxwidth ) ;
31
36
}
32
37
if ( options && options . maxheight ) {
33
- apiUrl . searchParams . set ( "maxheight" , options . maxheight ) ;
38
+ apiUrlBuilder . searchParams . set ( "maxheight" , options . maxheight ) ;
34
39
}
35
- apiUrl = apiUrl . toString ( ) ;
36
- httpGetAsync ( apiUrl , rawResponse => {
37
- response = JSON . parse ( rawResponse ) ;
40
+ const apiUrl = apiUrlBuilder . toString ( ) ;
41
+ httpGetAsync ( apiUrl , ( /** @type { Object } */ rawResponse ) => {
42
+ const response = JSON . parse ( rawResponse ) ;
38
43
39
- let iframe ;
44
+ /** @type { HTMLIFrameElement } */ let iframe ;
40
45
switch ( response . type ) {
41
46
case "rich" :
42
47
iframe = createIframe ( response ) ;
@@ -101,19 +106,25 @@ function renderOembed(shadow, urlToEmbed, options) {
101
106
} ) ;
102
107
}
103
108
109
+ /**
110
+ * @param {{ height: number?; width: number?; html: string; } } response
111
+ */
104
112
function createIframe ( response ) {
105
113
let iframe = document . createElement ( "iframe" ) ;
106
114
iframe . setAttribute ( "border" , "0" ) ;
107
115
iframe . setAttribute ( "frameborder" , "0" ) ;
108
- iframe . setAttribute ( "height" , ( response . height || 500 ) + 20 ) ;
109
- iframe . setAttribute ( "width" , ( response . width || 500 ) + 20 ) ;
116
+ iframe . setAttribute ( "height" , ( ( response . height || 500 ) + 20 ) . toString ( ) ) ;
117
+ iframe . setAttribute ( "width" , ( ( response . width || 500 ) + 20 ) . toString ( ) ) ;
110
118
iframe . srcdoc = response . html ;
111
119
return iframe ;
112
120
}
113
121
122
+ /**
123
+ * @param {string } url
124
+ * @param {{ (discoveredUrl: string): void;} } callback
125
+ */
114
126
function getDiscoverUrl ( url , callback ) {
115
- let apiUrl = new URL ( `https://cors-anywhere.herokuapp.com/${ url } ` ) ;
116
- apiUrl = apiUrl . toString ( ) ;
127
+ let apiUrl = new URL ( `https://cors-anywhere.herokuapp.com/${ url } ` ) . toString ( ) ;
117
128
httpGetAsync ( apiUrl , function ( response ) {
118
129
let dom = document . createElement ( "html" ) ;
119
130
dom . innerHTML = response ;
@@ -123,6 +134,10 @@ function getDiscoverUrl(url, callback) {
123
134
} ) ;
124
135
}
125
136
137
+ /**
138
+ * @param {string } theUrl
139
+ * @param {{ (rawResponse: string): void } } callback
140
+ */
126
141
function httpGetAsync ( theUrl , callback ) {
127
142
var xmlHttp = new XMLHttpRequest ( ) ;
128
143
xmlHttp . onreadystatechange = function ( ) {
0 commit comments