Skip to content

Commit

Permalink
feat: sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Xmader committed Nov 26, 2020
1 parent 548f7c0 commit 566ec96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/btn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { ScoreInfo } from './scoreinfo'
import { loadMscore, WebMscore } from './mscore'
import { useTimeout, windowOpen, console, attachShadow } from './utils'
import { useTimeout, windowOpenAsync, console, attachShadow } from './utils'
import i18n from './i18n'
// @ts-ignore
import btnListCss from './btn.css'
Expand Down Expand Up @@ -116,7 +116,7 @@ export class BtnList {
/**
* replace the template button with the list of new buttons
*/
commit (mode: BtnListMode = BtnListMode.InPage): void {
async commit (mode: BtnListMode = BtnListMode.InPage): Promise<void> {
switch (mode) {
case BtnListMode.InPage: {
// fallback to BtnListMode.ExtWindow
Expand Down Expand Up @@ -148,7 +148,7 @@ export class BtnList {

case BtnListMode.ExtWindow: {
const div = this._commit()
const w = windowOpen('', undefined, 'resizable,width=230,height=270')
const w = await windowOpenAsync('', undefined, 'resizable,width=230,height=270')
// eslint-disable-next-line no-unused-expressions
w?.document.body.append(div)
window.addEventListener('unload', () => w?.close())
Expand Down Expand Up @@ -176,7 +176,7 @@ export namespace BtnAction {

export const openUrl = (url: UrlInput): BtnAction => {
return process(async (): Promise<any> => {
windowOpen(await normalizeUrlInput(url))
return windowOpenAsync(await normalizeUrlInput(url))
})
}

Expand All @@ -195,7 +195,7 @@ export namespace BtnAction {
btn.onclick = null
setText(i18n('PROCESSING')())

const w = windowOpen('') as Window
const w = await windowOpenAsync('') as Window
const txt = document.createTextNode(i18n('PROCESSING')())
w.document.body.append(txt)

Expand Down
19 changes: 17 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,30 @@ export const useTimeout = async <T> (promise: T | Promise<T>, ms: number): Promi
})
}

export const getSandboxWindowAsync = async (): Promise<Window> => {
if (typeof document === 'undefined') return {} as any as Window

return new Promise((resolve) => {
window.onmouseover = () => {
const iframe = document.createElement('iframe')
iframe.style.display = 'none'
document.body.append(iframe)
const w = iframe.contentWindow
window.onmouseover = null
resolve(w as Window)
}
})
}

export const getUnsafeWindow = (): Window => {
// eslint-disable-next-line no-eval
return window.eval('window') as Window
}

export const console: Console = (window || global).console // Object.is(window.console, unsafeWindow.console) == false

export const windowOpen: Window['open'] = (...args): Window | null => {
return window.open(...args) // Object.is(window.open, unsafeWindow.open) == false
export const windowOpenAsync = (...args: Parameters<Window['open']>): Promise<Window | null> => {
return getSandboxWindowAsync().then(w => w.open(...args))
}

export const attachShadow = (el: Element): ShadowRoot => {
Expand Down

0 comments on commit 566ec96

Please sign in to comment.