UNPKG

4.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.default = `<script>
4window.addEventListener('message', function(event) {
5 const data = event.data;
6 switch ( data.task ) { // postMessage tasks
7 case 'print' :
8 console.log("received message from frontend:");
9 console.log(data.message);
10 break;
11 case 'key' :
12 const event = new KeyboardEvent('keydown', {key: data.key});
13 document.dispatchEvent(event);
14 break;
15 case 'submit' :
16 let button = document.createElement('input');
17 button.setAttribute('type', 'submit');
18 button.setAttribute('id', 'FORCE_FORM-submit');
19 button.setAttribute('name', 'FORCE_FORM-submit');
20 let form = document.getElementById('FORCE_FORM');
21 form.appendChild(button);
22 button.click();
23 form.removeChild(button);
24 break;
25 case 'results' :
26 if (typeof forceResults === "function") {
27 forceResults(data.value);
28 } else {
29 forceDefaultResults(data.value);
30 }
31 break;
32 }
33});
34
35function forceDefaultResults(results) {
36 if (typeof results === 'string') results = JSON.parse(results);
37 for(let key of Object.keys(results)) {
38 let input = document.getElementsByName(key)[0];
39 if(!input) {
40 input = document.getElementById(key);
41 if(!input) {
42 continue;
43 }
44 }
45 input.disabled = true;
46 if(input.type === 'file') {
47 // Skip files for now..
48 continue;
49 } else if(input.type === 'select-multiple') {
50 Array.from(input.options).forEach(function(opt){
51 if(results[key].includes(opt.value)) {
52 opt.selected = true;
53 }
54 });
55 input.dispatchEvent(new Event('change'))
56 } else {
57 input.value = results[key];
58 if (input.type === 'select-one') {
59 input.dispatchEvent(new Event('change'))
60 } else {
61 input.dispatchEvent(new Event('input'))
62 }
63 }
64 }
65}
66
67function forceSubmit() {
68 let button = document.createElement('input');
69 button.setAttribute('type', 'submit');
70 let form = document.getElementById('FORCE_FORM');
71 form.appendChild(button);
72 button.click();
73 form.removeChild(button);
74}
75
76var rtime;
77var resizeCount = 0;
78var delta = 100;
79
80function forceResize() {
81 // wait for cooloff period
82 if (!rtime || new Date() - rtime > delta) {
83 resizeCount++;
84 let body = document.body,
85 html = document.documentElement;
86
87 let height = Math.max(body.scrollHeight, body.offsetHeight,
88 html.clientHeight, html.scrollHeight, html.offsetHeight);
89
90 parent.postMessage({'task': 'setHeight', 'height': height}, '*');
91 // start cooloff period when we resized more than 5 times
92 if (resizeCount > 5) {
93 resizeCount = 0;
94 rtime = new Date();
95 }
96 }
97}
98
99function forceOnload() {
100 forceResize();
101
102 document.getElementById('FORCE_FORM').addEventListener("submit", async function(event) {
103 event.preventDefault();
104 let values = {};
105 if (typeof forceInput === "function") {
106 values = await forceInput(event);
107 } else {
108 const inputs = event.target.elements;
109 for (let index = 0; index < inputs.length; ++index) {
110 if (!inputs[index].name.startsWith('FORCE_')) {
111 let value;
112 const field = inputs[index];
113 const key = field.name || field.id;
114 if (!key) continue;
115 switch (field.type) {
116 case 'checkbox':
117 if (!field.checked) continue;
118 value = field.value;
119 break;
120 case 'submit':
121 // submit not yet supported
122 continue;
123 case 'file':
124 delete(inputs[index])
125 break;
126 case 'radio':
127 if (!field.checked) continue;
128 value = field.value;
129 break;
130 case 'select-multiple':
131 value = Array.from(inputs[index].options).filter(opt => opt.selected).map(opt => opt.value);
132 break;
133 default:
134 value = inputs[index].value;
135 }
136 if (key in values) {
137 if (Array.isArray(values[key])) {
138 // We already have an array with values, push new value to array
139 values[key].push(value);
140 } else {
141 // multiple values same key, create array
142 values[key] = [values[key], value];
143 }
144 } else {
145 values[key] = value;
146 }
147 }
148 }
149 }
150 if (typeof forceBeforeSubmit === "function") {
151 values = await forceBeforeSubmit(values, event);
152 }
153 parent.postMessage({'task': 'submit', 'values': values}, '*');
154 return false;
155 });
156}
157
158// Force onload and resize
159window.addEventListener("load", forceOnload, false);
160window.addEventListener("resize", forceResize, false);
161setInterval(forceResize, 1000)
162</script>`;
163//# sourceMappingURL=templateScript.js.map
\No newline at end of file