UNPKG

5.04 kBTypeScriptView Raw
1declare const _default: "<script>\nwindow.addEventListener('message', function(event) {\n const data = event.data;\n switch ( data.task ) { // postMessage tasks\n case 'print' :\n console.log(\"received message from frontend:\");\n console.log(data.message);\n break;\n case 'key' :\n const event = new KeyboardEvent('keydown', {key: data.key});\n document.dispatchEvent(event);\n break;\n case 'submit' :\n let button = document.createElement('input');\n button.setAttribute('type', 'submit');\n button.setAttribute('id', 'FORCE_FORM-submit');\n button.setAttribute('name', 'FORCE_FORM-submit');\n let form = document.getElementById('FORCE_FORM');\n form.appendChild(button);\n button.click();\n form.removeChild(button);\n break;\n case 'results' :\n if (typeof forceResults === \"function\") {\n forceResults(data.value);\n } else {\n forceDefaultResults(data.value);\n }\n break;\n }\n});\n\nfunction forceDefaultResults(results) {\n if (typeof results === 'string') results = JSON.parse(results);\n for(let key of Object.keys(results)) {\n let input = document.getElementsByName(key)[0];\n if(!input) {\n input = document.getElementById(key);\n if(!input) {\n continue;\n }\n }\n input.disabled = true;\n if(input.type === 'file') {\n // Skip files for now..\n continue;\n } else if(input.type === 'select-multiple') {\n Array.from(input.options).forEach(function(opt){\n if(results[key].includes(opt.value)) {\n opt.selected = true;\n }\n });\n input.dispatchEvent(new Event('change'))\n } else {\n input.value = results[key];\n if (input.type === 'select-one') {\n input.dispatchEvent(new Event('change'))\n } else {\n input.dispatchEvent(new Event('input'))\n }\n }\n }\n}\n\nfunction forceSubmit() {\n let button = document.createElement('input');\n button.setAttribute('type', 'submit');\n let form = document.getElementById('FORCE_FORM');\n form.appendChild(button);\n button.click();\n form.removeChild(button);\n}\n\nvar rtime;\nvar resizeCount = 0;\nvar delta = 100;\n\nfunction forceResize() {\n // wait for cooloff period\n if (!rtime || new Date() - rtime > delta) {\n resizeCount++;\n let body = document.body,\n html = document.documentElement;\n\n let height = Math.max(body.scrollHeight, body.offsetHeight,\n html.clientHeight, html.scrollHeight, html.offsetHeight);\n\n parent.postMessage({'task': 'setHeight', 'height': height}, '*');\n // start cooloff period when we resized more than 5 times\n if (resizeCount > 5) {\n resizeCount = 0;\n rtime = new Date();\n }\n }\n}\n\nfunction forceOnload() {\n forceResize();\n\n document.getElementById('FORCE_FORM').addEventListener(\"submit\", async function(event) {\n event.preventDefault();\n let values = {};\n if (typeof forceInput === \"function\") {\n values = await forceInput(event);\n } else {\n const inputs = event.target.elements;\n for (let index = 0; index < inputs.length; ++index) {\n if (!inputs[index].name.startsWith('FORCE_')) {\n let value;\n const field = inputs[index];\n const key = field.name || field.id;\n if (!key) continue;\n switch (field.type) {\n case 'checkbox':\n if (!field.checked) continue;\n value = field.value;\n break;\n case 'submit':\n // submit not yet supported\n continue;\n case 'file':\n delete(inputs[index])\n break;\n case 'radio':\n if (!field.checked) continue;\n value = field.value;\n break;\n case 'select-multiple':\n value = Array.from(inputs[index].options).filter(opt => opt.selected).map(opt => opt.value);\n break;\n default:\n value = inputs[index].value;\n }\n if (key in values) {\n if (Array.isArray(values[key])) {\n // We already have an array with values, push new value to array\n values[key].push(value);\n } else {\n // multiple values same key, create array\n values[key] = [values[key], value];\n }\n } else {\n values[key] = value;\n }\n }\n }\n }\n if (typeof forceBeforeSubmit === \"function\") {\n values = await forceBeforeSubmit(values, event);\n }\n parent.postMessage({'task': 'submit', 'values': values}, '*');\n return false;\n });\n}\n\n// Force onload and resize\nwindow.addEventListener(\"load\", forceOnload, false);\nwindow.addEventListener(\"resize\", forceResize, false);\nsetInterval(forceResize, 1000)\n</script>";
2export default _default;