feat: global sandbox
This commit is contained in:
parent
aa7622e953
commit
1f132bdfbd
4 changed files with 15 additions and 20 deletions
|
@ -1,7 +1,7 @@
|
|||
|
||||
import { ScoreInfo } from './scoreinfo'
|
||||
import { loadMscore, WebMscore } from './mscore'
|
||||
import { useTimeout, windowOpen, console, _Element } from './utils'
|
||||
import { useTimeout, windowOpen, console, attachShadow } from './utils'
|
||||
import i18n from './i18n'
|
||||
// @ts-ignore
|
||||
import btnListCss from './btn.css'
|
||||
|
@ -43,10 +43,6 @@ const cloneBtn = (btn: HTMLButtonElement) => {
|
|||
return n
|
||||
}
|
||||
|
||||
const attachShadow = (el: Element): ShadowRoot => {
|
||||
return _Element.prototype.attachShadow.call(el, { mode: 'closed' }) as ShadowRoot
|
||||
}
|
||||
|
||||
interface BtnOptions {
|
||||
readonly name: string;
|
||||
readonly action: BtnAction;
|
||||
|
|
|
@ -11,6 +11,6 @@
|
|||
// @match https://musescore.com/*/*
|
||||
// @license MIT
|
||||
// @copyright Copyright (c) 2019-2020 Xmader
|
||||
// @grant none
|
||||
// @grant unsafeWindow
|
||||
// @run-at document-start
|
||||
// ==/UserScript==
|
||||
|
|
20
src/utils.ts
20
src/utils.ts
|
@ -47,21 +47,19 @@ export const useTimeout = async <T> (promise: T | Promise<T>, ms: number): Promi
|
|||
})
|
||||
}
|
||||
|
||||
export const getSandboxWindow = (): Window => {
|
||||
if (typeof document === 'undefined') return {} as any as Window
|
||||
const iframe = document.createElement('iframe')
|
||||
iframe.style.display = 'none'
|
||||
document.body.append(iframe)
|
||||
const w = iframe.contentWindow
|
||||
return w as Window
|
||||
export const getUnsafeWindow = (): Window => {
|
||||
// eslint-disable-next-line no-eval
|
||||
return window.eval('window') as Window
|
||||
}
|
||||
|
||||
export const sandboxWindow = getSandboxWindow()
|
||||
export const console: Console = sandboxWindow['console']
|
||||
export const _Element: typeof Element = sandboxWindow['Element']
|
||||
export const console: Console = window.console // Object.is(window.console, unsafeWindow.console) == false
|
||||
|
||||
export const windowOpen: Window['open'] = (...args): Window | null => {
|
||||
return sandboxWindow.open(...args)
|
||||
return window.open(...args) // Object.is(window.open, unsafeWindow.open) == false
|
||||
}
|
||||
|
||||
export const attachShadow = (el: Element): ShadowRoot => {
|
||||
return Element.prototype.attachShadow.call(el, { mode: 'closed' }) as ShadowRoot
|
||||
}
|
||||
|
||||
export const waitForDocumentLoaded = (): Promise<void> => {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
|
||||
import { hookNative } from './anti-detection'
|
||||
import { console } from './utils'
|
||||
import { console, getUnsafeWindow } from './utils'
|
||||
|
||||
const CHUNK_PUSH_FN = /^function [^r]\(\w\){/
|
||||
|
||||
|
@ -86,9 +86,10 @@ export const [webpackGlobalOverride, onPackLoad] = (() => {
|
|||
}
|
||||
|
||||
// hook `webpackJsonpmusescore.push` as soon as `webpackJsonpmusescore` is available
|
||||
let jsonp = window['webpackJsonpmusescore']
|
||||
const _w = getUnsafeWindow()
|
||||
let jsonp = _w['webpackJsonpmusescore']
|
||||
let hooked = false
|
||||
Object.defineProperty(window, 'webpackJsonpmusescore', {
|
||||
Object.defineProperty(_w, 'webpackJsonpmusescore', {
|
||||
get () { return jsonp },
|
||||
set (v: WebpackJson) {
|
||||
jsonp = v
|
||||
|
|
Loading…
Reference in a new issue