Giter Club home page Giter Club logo

chrome-inject-eval's Introduction

chrome-inject-eval

use eval in chrome extension v3+

你可以通过引入本包从而实现在 chrome 插件 v3 版本中使用 eval

Start

我们推荐你使用npm安装而不是yarn或者pnpm,因为chrome插件开发时(没有webpack/rollup时)如果使用pnpm这种软链接安装的包管理工具是会出问题的

npm i chrome-inject-eval

Usage

manifest.json

{
  ...
  "content_scripts": [
    {
      ...
      "js": [
        "./node_modules/chrome-inject-eval/dist/umd.min.js",
        "./src/[your inject].js"
      ],
      ...
    }
  ],
  ...
}

[your inject].js

  • 基本
const evil = evalCore.getEvalInstance(window);

evil("const a=1;");
console.log(a); // 1
  • 高阶
const { evalModule, transformCode } = evalCore;

const { Interpreter } = evalModule;
Interpreter.global = window;
const interpreter = new Interpreter();
// 现在你可以使用eval5的所有功能

interpreter.evaluate(transformCode("const a=1;"));

console.log(a); // 1

Caveats

chrome-inject-eval的导出对象有三个属性,其中evalModule本质上等于require("eval5"),而 transform 则是通过 babel 把字符串编译成 ES5 代码,所以如果想使用更多 eval 相关的功能,可以参考 eval5官网

const evil = require("eval5");
const { transform } = require("@babel/standalone");

const transformCode = (codeStr) =>
  transform(codeStr, { presets: ["env"] }).code;

const getEvalInstance = (obj) => {
  const interpreter = new evil.Interpreter(obj, {
    timeout: 1000,
  });
  return (codeStr) => interpreter.evaluate(transformCode(codeStr));
};

module.exports = {
  evalModule: evil,
  getEvalInstance,
  transformCode,
};

chrome-inject-eval's People

Contributors

grinzero avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.