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