Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert node.js code to regular browser javascript?

I don't know how to get a javascript file to work for web-browser functionalit, when it's coded as node.js.

The code in question is from a github graphenejs-lib. I want to convert this node.js code into js:

import {Apis} from "graphenejs-ws";
var {ChainStore} = require("graphenejs-lib");
Apis.instance("wss://bitshares.openledger.info/ws", true).init_promise.then((res) => {
    console.log("connected to:", res[0].network);
    ChainStore.init().then(() => {
        ChainStore.subscribe(updateState);
    });
});
let dynamicGlobal = null;
function updateState(object) {
    dynamicGlobal = ChainStore.getObject("2.1.0");
    console.log("ChainStore object update\n", dynamicGlobal ? dynamicGlobal.toJS() : dynamicGlobal);
}

There is another github from the same developer, steemjs-lib, that shows both the node.js use, and the browser use in the README default page.

I don't know how to make graphenejs-lib into browser javascript, like the steemjs-lib has been made into a regular javascript working version. I contact the dev, but have yet to receive a response.

I figured other people actually know how to do what the dev did for steemjs-lib, and get the graphenejs-lib to work in a browser.

Can you help me out? Thank you.

like image 970
KrNel Avatar asked Jan 18 '17 12:01

KrNel


2 Answers

You can use Browserify to help you with that:

Note that not all of the Node's APIs will be available in the browser.

like image 55
rsp Avatar answered Nov 03 '22 06:11

rsp


Use browserify. https://wzrd.in/ will package it for you if you just want to use that one library. https://wzrd.in/standalone/graphenejs-lib@latest

<script src="https://wzrd.in/standalone/graphenejs-lib@latest"></script>
<script>
  const {ChainStore} = graphenejsLib;
  ...
</script>
like image 6
Trevor Dixon Avatar answered Nov 03 '22 04:11

Trevor Dixon