[英]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.