@garfish/browser-vm
Usage
import vmSandbox from '@garfish/browser-vm';
let nameMap = { a: 'chen' };
const sandbox = new vmSandbox({
namespace: 'app',
el: () => document.body,
modules: [
() => ({
override: { nameMap },
recover() {
nameMap = { a: 'chen' };
},
}),
],
});
sandbox.execScript(`
window.x = 1;
console.log(window.nameMap); // { a: 'chen' }
window.nameMap.a = 'tao';
`);
console.log(sandbox.global.x);
console.log(sandbox.global.nameMap);
sandbox.clearEffects();
console.log(nameMap);
sandbox.reset();
Hooks
const sandbox = new vmSandbox({
namespace: 'app',
el: () => document.body,
});
sandbox.hooks.usePlugin({
stared(fakeWindow) {},
closed() {},
beforeClearEffect() {},
afterClearEffect() {},
beforeInvoke(url, env, options) {},
afterInvoke(url, env, options) {},
invokeError(err, url, env, options) {},
appendNode(parentNode, oldNode, convertedNode, tagName) {},
});