-
Notifications
You must be signed in to change notification settings - Fork 15
/
exolve-widget-creator.js
55 lines (51 loc) · 1.72 KB
/
exolve-widget-creator.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
/*
MIT License
Copyright (c) 2019 Viresh Ratnakar
See the full Exolve license notice in exolve-m.js.
*/
if (typeof exolveWidgets === 'undefined') {
exolveWidgets = {};
}
/**
* Expects an HTMLElement with id exolve or exolve-widget-placeholder
* to be present. Changes that element's id to something unique.
*
* puzzleText: exolve spec.
* url: url of exolve-widget.html
* height: optional height (in px) of iframe.
*/
function ExolveWidgetCreator(puzzleText, url, height=1500) {
this.id = "xlv" + Math.random().toString(36).substring(2, 15);
this.puzzleText = puzzleText;
this.sendPuzzleToExolveWidget = () => {
document.getElementById(
"exolve-iframe-" + this.id).contentWindow.postMessage(
this.puzzleText, "*");
};
this.receiveExolveReady = (event) => {
if (event.data == "EXOLVE-READY-" + this.id) {
this.sendPuzzleToExolveWidget();
window.removeEventListener("message", this.receiveExolveReady);
}
}
exolveWidgets[this.id] = this;
window.addEventListener("message", this.receiveExolveReady, false);
let placeholder = document.getElementById("exolve");
if (!placeholder) {
// Try legacy name.
placeholder = document.getElementById("exolve-widget-placeholder");
if (!placeholder) {
console.log('Did not find expected DIV with id: ' +
'exolve/exolve-widget-placeholder');
return;
}
}
placeholder.id = "exolve-widget-" + this.id;
placeholder.insertAdjacentHTML('beforeend', `
<iframe
src="${url}?id=${this.id}"
width="100%" height="${height}" allow="fullscreen"
id="exolve-iframe-${this.id}" frameborder="0"
style="display:block; border:none; margin-left:auto; margin-right:auto">
</iframe>`);
}