diff --git a/src/gm.ts b/src/gm.ts index fd76ebe..83dd851 100644 --- a/src/gm.ts +++ b/src/gm.ts @@ -8,6 +8,9 @@ declare const GM: { /** https://www.tampermonkey.net/documentation.php#GM_registerMenuCommand */ registerMenuCommand (name: string, fn: () => any, accessKey?: string): Promise; + + /** https://github.com/Tampermonkey/tampermonkey/issues/881#issuecomment-639705679 */ + addElement (tagName: K, properties: Record): Promise; } export const _GM = GM diff --git a/src/meta.js b/src/meta.js index 21c6464..46a3d90 100644 --- a/src/meta.js +++ b/src/meta.js @@ -14,5 +14,6 @@ // @copyright Copyright (c) 2019-2020 Xmader // @grant unsafeWindow // @grant GM.registerMenuCommand +// @grant GM.addElement // @run-at document-start // ==/UserScript== diff --git a/src/utils.ts b/src/utils.ts index d29970e..d08f4fe 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,6 @@ import isNodeJs from 'detect-node' +import { isGmAvailable, _GM } from './gm' export const escapeFilename = (s: string): string => { return s.replace(/[\s<>:{}"/\\|?*~.\0\cA-\cZ]+/g, '_') @@ -60,6 +61,13 @@ export const useTimeout = async (promise: T | Promise, ms: number): Promi export const getSandboxWindowAsync = async (targetEl: Element | undefined = undefined): Promise => { if (typeof document === 'undefined') return {} as any as Window + if (isGmAvailable('addElement')) { + // create iframe using GM_addElement API + const iframe = await _GM.addElement('iframe', {}) + iframe.style.display = 'none' + return iframe.contentWindow as Window + } + if (!targetEl) { return new Promise((resolve) => { // You need ads in your pages, right?