UNPKG

mock-violentmonkey

Version:

Mock violentmonkey's globals for testing userscripts

57 lines 1.81 kB
import { JSDOM } from 'jsdom'; import { getBaseUrl } from './base-url.js'; import { VMStorage } from './vm-storage.js'; import { XMLHttpRequest } from './xmlhttprequest/index.js'; const storedJSDOMs = new VMStorage(() => new JSDOM('<!doctype html>', { url: getBaseUrl().href, })); /** @internal */ const getJSDOM = () => storedJSDOMs.get(true); const getWindow = () => getJSDOM().window; /** * Load the page at the given url to the dom */ const loadURLToDom = async (url) => { // Convert to a URL instance to throw early on obviously invalid urls url = new URL(url).href; const xhr = new XMLHttpRequest({ base: getBaseUrl(), }); await new Promise((resolve, reject) => { xhr.addEventListener('load', () => { loadStringToDom(xhr.responseBuffer.toString()); resolve(); }); xhr.addEventListener('error', reject); xhr.open('get', new URL(url).href); xhr.send(); }); }; /** * Set document.documentElement.outerHTML to passed string */ const loadStringToDom = (outerHTML) => { getWindow().document.documentElement.innerHTML = outerHTML; }; const builtinGlobals = new Set([ 'URL', 'Blob', ]); /** * Enable a global dom value. * This allows you to enable only what you need. */ function enableDomGlobal(key) { if (builtinGlobals.has(key)) { console.warn('Warning(mock-violentmonkey):' + ` enableDomGlobal("${key}") is not necessary anymore.` + ` Please use the built-in version of \`${key}\` instead.`); } Object.defineProperty(globalThis, key, { get: () => getWindow()[key], }); } enableDomGlobal('window'); enableDomGlobal('document'); export { getWindow, getJSDOM, loadURLToDom, loadStringToDom, enableDomGlobal }; //# sourceMappingURL=dom.js.map