-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathapp.js
308 lines (249 loc) · 11.3 KB
/
app.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
var im = iframemanager();
const MAPS_API_KEY = '';
var config = {
onChange: ({changedServices, eventSource}) => {
console.log(changedServices, eventSource)
},
currLang: 'en',
// autoLang: true,
services : {
youtube : {
category: 'analytics',
embedUrl: 'https://www.youtube-nocookie.com/embed/{data-id}',
iframe : {
allow : 'accelerometer; encrypted-media; gyroscope; picture-in-picture; fullscreen;',
},
languages : {
en : {
notice: 'This content is available <a href="https://www.youtube-nocookie.com/embed/{data-id}">here</a>. By showing the external content you accept the <a rel="noreferrer noopener" href="https://www.youtube.com/t/terms" target="_blank">terms and conditions</a> of youtube.com.',
loadBtn: 'Load once',
loadAllBtn: "Don't ask again"
}
}
},
dailymotion : {
embedUrl: 'https://www.dailymotion.com/embed/video/{data-id}',
// Use dailymotion api to obtain thumbnail
thumbnailUrl: async (id, setThumbnail) => {
const url = `https://api.dailymotion.com/video/${id}?fields=thumbnail_large_url`;
const response = await (await fetch(url)).json();
const thumbnailUrl = response.thumbnail_large_url;
thumbnailUrl && setThumbnail(thumbnailUrl);
},
iframe : {
allow : 'accelerometer; encrypted-media; gyroscope; picture-in-picture; fullscreen;'
},
languages : {
en : {
notice: 'This content is hosted by a third party. By showing the external content you accept the <a rel="noreferrer noopener" href="#link_dailymotion" target="_blank">terms and conditions</a> of dailymotion.com.',
loadBtn: 'Load once',
loadAllBtn: "Don't ask again"
}
}
},
twitch : {
embedUrl: `https://player.twitch.tv/?{data-id}&parent=${location.hostname}`,
iframe : {
allow : 'accelerometer; encrypted-media; gyroscope; picture-in-picture; fullscreen;',
},
languages : {
en : {
notice: 'This content is hosted by a third party. By showing the external content you accept the <a rel="noreferrer noopener" href="#link_twitch" target="_blank">terms and conditions</a> of twitch.com.',
loadBtn: 'Load once',
loadAllBtn: "Don't ask again"
}
}
},
leaflet: {
/**
*
* @param {HTMLDivElement} div
*/
onAccept: async (div, setIframe) => {
const leafletLoaded = await loadScript('https://unpkg.com/leaflet@1.9.3/dist/leaflet.js');
const leafletReady = leafletLoaded && await im.childExists({childProperty: 'L'});
if(!leafletReady)
return;
const mapCoordinates = JSON.parse(div.dataset.mapCoordinates);
const markerCoordinates = (div.dataset.mapMarkers || '').split(';')
const map = L.map(div.lastElementChild.firstElementChild).setView(mapCoordinates, 13);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
for(const coordinates of markerCoordinates)
coordinates && L.marker(JSON.parse(coordinates)).addTo(map);
// Manually toggle show placeholder
div.classList.add('show-ph');
},
/**
*
* @param {HTMLDivElement} serviceDiv
*/
onReject: (iframe, serviceDiv) => {
// remove: div[data-service] > div[placeholder] > div.leaflet-map
serviceDiv.lastElementChild.firstElementChild.remove();
},
languages : {
en : {
notice: 'This content is hosted by a third party. By showing the external content you accept the <a rel="noreferrer noopener" href="#link_twitch" target="_blank">terms and conditions</a> of twitch.com.',
loadBtn: 'Load once',
loadAllBtn: "Don't ask again"
}
}
},
twitter : {
onAccept: async (div, setIframe) => {
await loadScript('https://platform.twitter.com/widgets.js');
await im.childExists({childProperty: 'twttr'});
const tweet = await twttr.widgets.createTweet(div.dataset.id, div.firstElementChild);
tweet && setIframe(tweet.firstChild);
},
onReject: async (iframe, serviceDiv, showNotice) => {
await im.childExists({parent: serviceDiv});
showNotice();
serviceDiv.querySelector('.twitter-tweet').remove();
},
languages : {
en : {
notice: 'This content is hosted by a third party. By showing the external content you accept the <a rel="noreferrer noopener" href="https://www.youtube.com/t/terms" target="_blank">terms and conditions</a> of twitter.com.',
loadBtn: 'Load once',
loadAllBtn: "Don't ask again"
}
}
},
'facebook-post' : {
embedUrl : 'https://www.facebook.com/plugins/post.php?{data-id}',
iframe : {
allow : 'accelerometer; encrypted-media; gyroscope; picture-in-picture; fullscreen; web-share;',
},
languages : {
en : {
notice: 'This content is hosted by a third party. By showing the external content you accept the <a rel="noreferrer noopener" href="#link_twitch" target="_blank">terms and conditions</a> of twitch.com.',
loadBtn: 'Load once',
loadAllBtn: "Don't ask again"
}
}
},
googlemaps: {
embedUrl: 'https://www.google.com/maps/embed?pb={data-id}',
iframe: {
allow : 'picture-in-picture; fullscreen;'
},
languages: {
en : {
notice: 'This content is hosted by a third party. By showing the external content you accept the <a rel="noreferrer noopener" href="https://cloud.google.com/maps-platform/terms" target="_blank">terms and conditions</a> of Google Maps.',
loadBtn: 'Load once',
loadAllBtn: "Don't ask again"
}
}
},
googlemapsapi: {
onAccept: async (div, setIframe) => {
await loadScript(`https://maps.googleapis.com/maps/api/js?key=${MAPS_API_KEY}`);
await im.childExists({childProperty: 'google'});
// The location of Uluru
const uluru = {
lat: parseInt(div.dataset.mapsLat),
lng: parseInt(div.dataset.mapsLng)
};
// The map, centered at Uluru
const map = new google.maps.Map(div.querySelector('.map'), {
zoom: 4,
center: uluru
});
if(div.dataset.mapsMarker === ''){
// The marker, positioned at Uluru
const marker = new google.maps.Marker({
position: uluru,
map: map,
});
}
if(div.dataset.mapsStreetview === ''){
const panorama = new google.maps.StreetViewPanorama(div.querySelector('.map'), {
position: uluru,
pov: {
heading: 34,
pitch: 10,
},
}
);
map.setStreetView(panorama);
}
await im.childExists({parent: div}) && setIframe(div.querySelector('iframe'));
},
onReject: async (iframe, serviceDiv, showNotice) => {
await im.childExists({parent: serviceDiv});
showNotice();
serviceDiv.querySelector('.map').remove();
},
languages : {
en : {
notice: 'This content is hosted by a third party. By showing the external content you accept the <a rel="noreferrer noopener" href="https://www.youtube.com/t/terms" target="_blank">terms and conditions</a> of twitter.com.',
loadBtn: 'Load once',
loadAllBtn: "Don't ask again"
}
}
},
instagram: {
onAccept: async (div, setIframe) => {
await loadScript('https://www.instagram.com/embed.js');
await im.childExists({childProperty: 'instgrm'}) && instgrm.Embeds.process();
await im.childExists({parent: div}) && setIframe(div.querySelector('iframe'));
},
onReject: (iframe) => {
iframe && iframe.remove();
},
languages: {
en : {
notice: 'This content is hosted by a third party. By showing the external content you accept the <a rel="noreferrer noopener" href="https://vimeo.com/terms" target="_blank">terms and conditions</a> of vimeo.com.',
loadBtn: 'Load once',
loadAllBtn: "Don't ask again"
}
}
},
vimeo: {
embedUrl: 'https://player.vimeo.com/video/{data-id}',
iframe: {
allow : 'fullscreen; picture-in-picture;'
},
thumbnailUrl: async (dataId, setThumbnail) => {
const url = `https://vimeo.com/api/v2/video/${dataId}.json`;
const response = await (await fetch(url)).json();
const thumbnailUrl = response[0].thumbnail_large;
thumbnailUrl && setThumbnail(thumbnailUrl);
},
languages: {
en : {
notice: 'This content is hosted by a third party. By showing the external content you accept the <a rel="noreferrer noopener" href="https://vimeo.com/terms" target="_blank">terms and conditions</a> of vimeo.com.',
loadBtn: 'Load once',
loadAllBtn: "Don't ask again"
}
}
}
}
};
im.run(config);
/**
* Dynamically load script (append to head)
* @param {string} src
* @returns {Promise<boolean>} promise
*/
function loadScript(src) {
/**
* @type {HTMLScriptElement}
*/
let script = document.querySelector('script[src="' + src + '"]');
return new Promise((resolve) => {
if(script)
return resolve(true);
script = document.createElement('script');
script.onload = () => resolve(true);
script.onerror = () => {
/**
* Remove script from dom if error is thrown
*/
script.remove();
resolve(false);
};
script.src = src;
document.head.appendChild(script);
});
};