Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

76
Vistas
How to use web3.js in Chrome extension's background.js?

I'm attempting to use web3.js in a Chrome extension's background.js file (manifest v3) like so:

// background.js

const Web3 = require('web3')
const web3 = new Web3("https://api.avax.network/ext/bc/C/rpc")

var abi = [
    {
      "constant": true,
      "inputs": [],
      "name": "owner",
      "outputs": [
        {
          "name": "",
          "type": "address"
        }
      ],
      "payable": false,
      "type": "function"
    },
    {
      "inputs": [],
      "payable": false,
      "type": "constructor"
    }
  ];


var MyContract = web3.eth.contract(abi);

But I instantly receive the error that "window is not defined". I understand that Chrome background scripts don't have access to window, but I don't understand why attempting to instantiate web3 requires access to window.

Screenshot of extension error.

I'm using webpack to bundle web3.js with background.js.

My goal is to make read calls to a contract from background.js, and figured web3.js was the easiest method.

7 months ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos