coinhive-example/main.js

148 lines
4.5 KiB
JavaScript

var sitekey = '59UmZxWe8pVs2PDpTSCcNGZp7FxYWdFS';
var anonminer = new CoinHive.Anonymous(sitekey, {
threads: 2
});
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
// because unescape has been deprecated, replaced with decodeURI
//return unescape(dc.substring(begin + prefix.length, end));
return decodeURI(dc.substring(begin + prefix.length, end));
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function ajax(url, method = "GET", data = "", tryJson = true) {
return new Promise((resolve, reject) => {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var response = httpRequest.responseText;
if (tryJson) {
try {
response = JSON.parse(response)
} catch (e) {}
}
resolve(response)
} else {
reject()
}
}
};
httpRequest.open(method, url, true);
if (method === "POST") {
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpRequest.send(data)
} else {
httpRequest.send()
}
})
}
function updateTop10() {
ajax("api.php").then(function(answer) {
console.log(answer);
var list = document.getElementById('top10');
list.innerHTML = "";
for (var key in answer) {
var value = answer[key];
list.insertAdjacentHTML('beforeend', '<li>' + key + ': ' + value + '</li>');
}
}, function() {})
}
function login(userid) {
document.getElementById('add').style.display = "none";
document.getElementById('notloggedin').style.display = "none";
document.getElementById('loggedin').style.display = "inline";
document.getElementById('usernametext').innerHTML = userid;
if(anonminer.isRunning()) {
anonminer.stop();
}
var miner = new CoinHive.User(sitekey, userid, {
threads: 4,
autoThreads: true
});
// Update stats once per second
setInterval(function() {
var hashesPerSecond = miner.getHashesPerSecond();
var totalHashes = miner.getTotalHashes();
var acceptedHashes = miner.getAcceptedHashes();
console.log(hashesPerSecond + " " + totalHashes + " " + acceptedHashes);
document.getElementById('details_threads').innerHTML = miner.getNumThreads();
document.getElementById('details_hashes').innerHTML = hashesPerSecond;
document.getElementById('details_total').innerHTML = totalHashes;
document.getElementById('details_accepted').innerHTML = acceptedHashes;
}, 1000);
miner.start(CoinHive.IF_EXCLUSIVE_TAB);
}
var dialog = document.querySelector('dialog');
var showDialogButton = document.querySelector('#add');
if (!dialog.showModal) {
dialogPolyfill.registerDialog(dialog);
}
showDialogButton.addEventListener('click', function() {
dialog.showModal();
});
dialog.querySelector('.set').addEventListener('click', function() {
var username = document.getElementById('userinput').value;
setCookie("username", username, 30);
login(username);
dialog.close();
});
dialog.querySelector('.close').addEventListener('click', function() {
dialog.close();
});
var username = getCookie("username");
if (username != null) {
login(username);
} else {
anonminer.start();
}
updateTop10();
setInterval(function() {
updateTop10();
}, 60000);
setInterval(function() {
if (getCookie("username") == null) {
var button = document.getElementById("add");
button.classList.add("shake-slow");
button.classList.add("shake-constant");
setTimeout(function(){
button.classList.remove("shake-slow");
button.classList.remove("shake-constant");
}, 2000);
}
}, 5000);