|
|
|
|
const frameHtml = `<!doctype html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<title>Frame</title>
|
|
|
|
|
<style>
|
|
|
|
|
html, body, iframe {
|
|
|
|
|
margin: 0;
|
|
|
|
|
padding: 0;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<iframe srcdoc="" sandbox="allow-scripts"></iframe>
|
|
|
|
|
<script type="module">
|
|
|
|
|
const frame = document.getElementsByTagName('iframe')[0]
|
|
|
|
|
addEventListener('message', event => {
|
|
|
|
|
const d = event.data
|
|
|
|
|
if (Array.isArray(d) && d[0] === 'srcdoc') {
|
|
|
|
|
frame.srcdoc = d
|
|
|
|
|
} else {
|
|
|
|
|
frame.postMessage(event.data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>`
|
|
|
|
|
|
|
|
|
|
export class Page extends HTMLElement {
|
|
|
|
|
constructor() {
|
|
|
|
|
super()
|
|
|
|
|
const shadow = this.attachShadow({mode: 'open'})
|
|
|
|
|
this.textArea = document.createElement('textarea')
|
|
|
|
|
this.header = document.createElement('h1')
|
|
|
|
|
this.textArea.addEventListener('input', e => {
|
|
|
|
|
const path = this.getAttribute('path')
|
|
|
|
|
localStorage.setItem(
|
|
|
|
|
path,
|
|
|
|
|
e.target.value
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
const div = document.createElement('div')
|
|
|
|
|
div.appendChild(this.header)
|
|
|
|
|
div.appendChild(this.textArea)
|
|
|
|
|
shadow.appendChild(div)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
|
const path = this.getAttribute('path')
|
|
|
|
|
this.header.innerText = path
|
|
|
|
|
this.textArea.value = localStorage.getItem(
|
|
|
|
|
path
|
|
|
|
|
) ?? ''
|
|
|
|
|
const style = document.createElement('style')
|
|
|
|
|
style.textContent = `
|
|
|
|
|
textarea {
|
|
|
|
|
width: 80%;
|
|
|
|
|
height: 45vh;
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
this.shadowRoot.append(style)
|
|
|
|
|
if (path.startsWith('/sandbox/')) {
|
|
|
|
|
this.initFrame()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initFrame() {
|
|
|
|
|
const wrap = document.createElement('div')
|
|
|
|
|
this.shadowRoot.appendChild(wrap)
|
|
|
|
|
const tmp = document.createElement('iframe')
|
|
|
|
|
tmp.sandbox = "allow-same-origin allow-scripts"
|
|
|
|
|
const url = new URL(
|
|
|
|
|
'/-/frame', location.href
|
|
|
|
|
)
|
|
|
|
|
url.searchParams.set(
|
|
|
|
|
'csp',
|
|
|
|
|
"default-src 'self'"
|
|
|
|
|
)
|
|
|
|
|
url.searchParams.set('html', frameHtml)
|
|
|
|
|
tmp.src = url.href
|
|
|
|
|
wrap.insertAdjacentHTML(
|
|
|
|
|
'beforeend', tmp.outerHTML
|
|
|
|
|
)
|
|
|
|
|
const frames = wrap.getElementsByTagName('iframe')
|
|
|
|
|
this.frame = frames[frames.length - 1]
|
|
|
|
|
this.textArea.addEventListener('blur', e => {
|
|
|
|
|
const msg = ['srcdoc', e.target.value]
|
|
|
|
|
this.frame.contentWindow.postMessage(msg)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|