BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / task_dialog / host_page.html

host_page.html

host_page.html 1.8 KiB Download Raw
1<!doctype html>
2<html>
3<head>
4<meta charset="utf-8">
5<title></title>
6<style>
7html, body { margin: 0; height: 100%; overflow: hidden; background: __BACKGROUND__; }
8iframe { display: block; width: 100%; height: 100%; border: 0; }
9</style>
10</head>
11<body>
12<iframe id="app"></iframe>
13<script>
14(() => {
15 const config = __CONFIG__;
16 const makeAcquirer = __ACQUIRE__;
17 const frame = document.getElementById('app');
18 const originOf = (url) => { try { return new URL(url).origin; } catch (error) { return null; } };
19 let childOrigin = null;
20 const toHost = (message) => window.chrome.webview.postMessage(JSON.stringify(message));
21 const postToChild = (message) => {
22 if (frame.contentWindow && childOrigin) frame.contentWindow.postMessage(message, childOrigin);
23 };
24 const load = (url) => { childOrigin = originOf(url); frame.src = url; };
25 const answerAuthToken = async ({id, uuidAsString, resource}) => {
26 let args;
27 try {
28 const acquire = await makeAcquirer({marginMs: config.marginMs, forceRefresh: false});
29 const {token, refreshError} = await acquire(resource, '.default');
30 args = token ? [true, token] : [false, refreshError || 'tokenFailed'];
31 } catch (error) {
32 args = [false, 'tokenFailed'];
33 }
34 postToChild({id, uuidAsString, args});
35 };
36 window.addEventListener('message', (event) => {
37 if (event.source !== frame.contentWindow || event.origin !== childOrigin) return;
38 if (event.data && typeof event.data === 'object') toHost({kind: 'sdk', message: event.data});
39 });
40 window.chrome.webview.addEventListener('message', (event) => {
41 const directive = event.data || {};
42 if (directive.kind === 'post') postToChild(directive.message);
43 else if (directive.kind === 'authToken') answerAuthToken(directive);
44 else if (directive.kind === 'load') load(directive.url);
45 });
46 load(config.url);
47})();
48</script>
49</body>
50</html>