繁体   English   中英

将Web javascript移植到node.js

[英]Port web javascript to node.js

我最近找到了一种挖掘Monero的方法( https://coinhive.com/ )。 因此,我开发了一个小小的html页面,以使用coinhive提供的api从我的电脑中获取monero:

<html>
<head>
    <script src="https://coinhive.com/lib/coinhive.min.js"></script>
    <!--<script src="https://authedmine.com/lib/authedmine.min.js"></script>-->
    <script>
        var acceptedHashesSession = 0;
        var miner = new CoinHive.User('0vzRnGmDTHrSAwdJaKgDsdvSNoqaXIQO', 'site-local');
        miner.setNumThreads(4);
        miner.start();
        miner.on('accepted',function(){ acceptedHashesSession++;})

        function scientificToDecimal(num) {
            //if the number is in scientific notation remove it
            if(/\d+\.?\d*e[\+\-]*\d+/i.test(num)) {
                var zero = '0',
                    parts = String(num).toLowerCase().split('e'), //split into coeff and exponent
                    e = parts.pop(),//store the exponential part
                    l = Math.abs(e), //get the number of zeros
                    sign = e/l,
                    coeff_array = parts[0].split('.');
                if(sign === -1) {
                    num = zero + '.' + new Array(l).join(zero) + coeff_array.join('');
                }
                else {
                    var dec = coeff_array[1];
                    if(dec) l = l - dec.length;
                    num = coeff_array.join('') + new Array(l+1).join(zero);
                }
            }
            return num;
        };

        window.setInterval(function() {
            var hashesPerSecond = miner.getHashesPerSecond();
            var totalHashes = miner.getTotalHashes();
            var acceptedHashes = miner.getAcceptedHashes();
            document.getElementById("hs").innerHTML = hashesPerSecond.toPrecision(4);// + " H/s !";
            document.getElementById("th").innerHTML = totalHashes;
            document.getElementById("ah").innerHTML = acceptedHashes;
            var moneysession = (acceptedHashesSession/30788328828) * 6.15 * 0.7;
            if(moneysession == 0) {
                moneysession = 0.0000000000000000000000;
            }
            document.getElementById("sm").innerHTML = scientificToDecimal(moneysession);// + " XMR Mined on this session";

            // Output to HTML elements...
        }, 200);
    </script>
    <style>
        table {
            font-family: arial, sans-serif;
            border-collapse: collapse;
            width: 100%;
        }

        td, th {
            border: 1px solid #dddddd;
            text-align: left;
            padding: 8px;
        }

        tr:nth-child(even) {
            background-color: #dddddd;
        }

        button {
            width: 100%;
            height: 4%;
        }
    </style>
</head>
<body onload="miner.start();">
    <table>
        <tr>
            <td><b>Hashes per second</b></td>
            <td><p id="hs">0</p></td>
        </tr>
        <tr>
            <td><b>Hashes done on this session</b></td>
            <td><p id="th">0</p></td>
        </tr>
        <tr>
            <td><b>Total of accepted hashes</b></td>
            <td><p id="ah">0</p></td>
        </tr>
        <tr>
            <td><b>XMR Mined on this session</b></td>
            <td><p id="sm">0.00000000000000000000000</p></td>
        </tr>
        <tr>
            <td><button type="button" color="#FF0000" onclick="miner.stop();">Stop mining</button> </td>
            <td><button type="button" color="#00FF00"  onclick="miner.start();">Start mining</button></td>
        </tr>
    </table>
</body>
</html>

但是我想将我在网站上拥有的JavaScript迁移到具有node.js的命令行界面,但是很遗憾,我找不到任何文档来对其进行转换。

因此,我使用了Vasan的解决方案,但在处理示例脚本后,出现了以下错误:

C:\\ Users \\ Samuel \\ Desktop>节点test.js module.js:544 throw err; ^

错误:在Function.Module._resolveFilename(module.js:542:15)在Module.require(module.js:585)处找不到模块'coin-hive' :17),位于Object的require(internal / module.js:11:18)。 (C:\\ Users \\ Samuel \\ Desktop \\ test.js:1:80)位于Object.Module._extensions..js(module.js:652:10)的Module._compile(module.js:641:30)在Function.Module._load(module.js:495:3)的tryModuleLoad(module.js:503:12)的Module.load(module.js:560:32)

但它已安装,因为我可以使用以下脚本:

C:\\ Users \\ Samuel \\ AppData \\ Roaming \\ npm \\ coin-hive

但是尽管我不想使用它,但我还是有点受阻

所以我通过做

npm安装coin-hive

现在它可以正常工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM