/
app.html
128 lines (119 loc) · 3.81 KB
/
app.html
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
<!-- app.html -->
<!doctype html>
<html lang="en">
<head>
<script
id="miro-sdk2"
src="https://miro.com/app/static/sdk/v2/miro.js"
></script>
<link
rel="icon"
href="data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA="
/>
</head>
<body>
<script id="variables" src="variables.js"></script>
<script>
window.userInfo = null;
async function callApi(url, method, body, token) {
const reqHeaders = {
accept: "application/json",
"cache-control": "no-cache",
//'authorization': token //Uncomment this line if you are using an authorization token
};
const reqOptions = {
method: method,
headers: reqHeaders,
body: body,
mode: "cors",
};
const reqResponse = await fetch(url, reqOptions)
.then((res) => {
return res["json"]().then((data) => ({
status: res.status,
body: data,
}));
})
.catch((error) => {
return error;
});
return reqResponse;
}
async function triggerModal(seconds) {
if (await miro.board.ui.canOpenModal()) {
var timeStamp = Date.now();
await miro.board.ui.openModal({
url: `${MODAL_URL}?v=${timeStamp}`,
width: MODAL_WITH,
height: MODAL_HEIGHT,
fullscreen: false,
});
}
checkModal(seconds);
}
async function getMiroTermsModalFrame() {
const parentFrames = window.parent.frames;
let miroModalTerms = null;
for (let a = 0; a < parentFrames.length; a++) {
var sameOrigin;
try {
sameOrigin =
window.parent.frames[a].location.host == window.location.host;
} catch (e) {
sameOrigin = false;
}
if (sameOrigin) {
if (parentFrames[a].location.href.indexOf("/modal.html") !== -1) {
miroModalTerms = parentFrames[a];
return miroModalTerms;
}
}
}
return miroModalTerms;
}
var delay = (ms) => new Promise((res) => setTimeout(res, ms));
var holdScriptExecution = async (ms) => {
await delay(ms);
};
async function checkModal(seconds) {
let canOpenModal = await miro.board.ui.canOpenModal();
if (!canOpenModal) {
let miroModal = await getMiroTermsModalFrame();
if (miroModal === null) {
console.log(
`There is another modal currently open - retrying in ${seconds} second(s)`,
);
await holdScriptExecution(seconds * 1000);
return await checkModal(seconds);
} else {
console.log(
`Miro Terms Modal is still open - retrying in ${seconds} second(s)`,
);
await holdScriptExecution(seconds * 1000);
return await checkModal(seconds);
}
} else {
console.log("No modals currently open - Checking acceptance...");
const apiUrl =
API_ENDPOINT_BASE_URL + "/Prod/q?u=" + window.userInfo.id;
const query = await callApi(apiUrl, "GET", null);
if (query.status === 200) {
console.log("Terms Modal has been accepted - Ending actions");
return true;
} else {
triggerModal(seconds);
}
}
}
async function init() {
if (window.userInfo === null) {
window.userInfo = await miro.board.getUserInfo();
}
/* Interval in seconds in which the app will check if the terms modal has been accepted */
SECONDS_TO_WAIT = +SECONDS_TO_WAIT;
checkModal(SECONDS_TO_WAIT);
}
init();
</script>
</body>
</html>