@garfish/browser-vm
TypeScript icon, indicating that this package has built-in type declarations

1.19.3 • Public • Published

@garfish/browser-vm

NPM version

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); // 1
console.log(sandbox.global.nameMap); // { a: 'tao' }

// Clear effect
sandbox.clearEffects();
console.log(nameMap); // { a: 'chen' }

// If clear all effects
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) {},
});

Readme

Keywords

Package Sidebar

Install

npm i @garfish/browser-vm

Weekly Downloads

1,682

Version

1.19.3

License

MIT

Unpacked Size

136 kB

Total Files

6

Last publish

Collaborators

  • reothues
  • aishiguang
  • cttrevor
  • shawzhou
  • jiadi0801
  • jsongo
  • rustle
  • zengkp