-
Notifications
You must be signed in to change notification settings - Fork 21
/
host.js
132 lines (115 loc) · 3.53 KB
/
host.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
import { hostResizer } from './plugins/iframe-resizer/host.js';
import { hostSyncCssVariable } from './plugins/sync-css-variable/host.js';
import { hostSyncDataAttrs } from './plugins/sync-data-attrs/host.js';
import { hostSyncFlags } from './plugins/sync-flags/host.js';
import { hostSyncFont } from './plugins/sync-font/host.js';
import { hostSyncIntl } from './plugins/sync-intl/host.js';
import { hostSyncLang } from './plugins/sync-lang/host.js';
import { hostSyncOslo } from './plugins/sync-oslo/host.js';
import { hostSyncTimezone } from './plugins/sync-timezone/host.js';
import { hostSyncTitle } from './plugins/sync-title/host.js';
import { PortWithServices } from './port/services.js';
const createIFrame = (src, frameId, height, allowFullScreen, allowMicrophone, allowCamera, allowScreenCapture, allowEncryptedMedia, allowAutoplay, allowClipboard) => {
const iframe = document.createElement('iframe');
iframe.width = '100%';
if (height || height === 0) {
iframe.style.height = height;
}
iframe.style.border = 'none';
iframe.style.overflow = 'hidden';
iframe.scrolling = 'no';
iframe.src = src;
if (frameId) {
iframe.id = frameId;
}
if (allowMicrophone || allowCamera || allowScreenCapture || allowEncryptedMedia || allowAutoplay || allowClipboard) {
const allow = [];
if (allowCamera) {
allow.push('camera *;');
}
if (allowMicrophone) {
allow.push('microphone *;');
}
if (allowScreenCapture) {
allow.push('display-capture *;');
}
if (allowEncryptedMedia) {
allow.push('encrypted-media *;');
}
if (allowAutoplay) {
allow.push('autoplay *;');
}
if (allowClipboard) {
allow.push('clipboard-write *;');
}
iframe.setAttribute('allow', allow.join(' '));
}
if (allowFullScreen) {
iframe.setAttribute('allowfullscreen', 'allowfullscreen');
}
return iframe;
};
const originRe = /^(http:\/\/|https:\/\/)[^/]+/i;
const tryGetOrigin = (url) => {
if (url && url.indexOf('//') === 0) {
url = window.location.protocol + url;
} else if (url && url.indexOf('/d2l/') === 0) {
return window.location.origin;
}
const match = originRe.exec(url);
return (match !== null) ? match[0] : null;
};
export class Host extends PortWithServices {
constructor(elementProvider, src, options) {
const origin = tryGetOrigin(src);
if (origin === null) throw new Error(`Unable to extract origin from ${src}`);
const parent = elementProvider();
if (parent === null) throw new Error('Could not find parent node');
options = options || {};
const iframe = createIFrame(
src,
options.id,
options.height,
options.allowFullScreen,
options.allowMicrophone,
options.allowCamera,
options.allowScreenCapture,
options.allowEncryptedMedia,
options.allowAutoplay,
options.allowClipboard
);
parent.appendChild(iframe);
super(iframe.contentWindow, origin, options);
this.iframe = iframe;
if (options.syncLang) {
this.use(hostSyncLang);
this.use(hostSyncIntl);
this.use(hostSyncTimezone);
this.use(hostSyncOslo);
}
this.use(hostSyncTitle({ page: !!options.syncPageTitle }));
if (!(options.height || options.height === 0) && options.resizeFrame !== false) {
this.use(hostResizer);
}
if (options.syncFont) {
this.use(hostSyncFont);
}
if (options.syncCssVariable) {
this.use(hostSyncCssVariable);
}
this.use(hostSyncDataAttrs);
this.use(hostSyncFlags);
}
async connect() {
return new Promise(resolve => {
this.onEvent('ready', async() => {
if (this._isConnected) {
return;
}
await super.connect();
resolve(this);
});
this.open();
});
}
}