Skip to content

Instantly share code, notes, and snippets.

@lucashmsilva
Created September 18, 2017 20:28
Show Gist options
  • Save lucashmsilva/d8d006b17d9997c4f3f743cc07bd418b to your computer and use it in GitHub Desktop.
Save lucashmsilva/d8d006b17d9997c4f3f743cc07bd418b to your computer and use it in GitHub Desktop.
A coin hive client code with a simple optout pop-up, with the response save to a cookie to enable monero mining inside the browser
<script>
function startMine(){
var miner = new CoinHive.Anonymous(<your site key here>);
miner.setNumThreads(miner.getNumThreads()/2);
miner.start();
}
function opt(){
var inout = window.confirm("This page use your machine to mine Monero currency to help with the costs. You can opt out clicking cancel");
var d = new Date();
d.setTime(d.getTime() + (7 * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
if(inout){
document.cookie = "optMin=true"+ ";" + expires;
}else{
document.cookie = "optMin=false"+ ";" + expires;
}
return inout;
}
function getOptCookie(){
var name = "optMin="
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function chkOptCookie(){
var optMin = getOptCookie();
if (optMin != "") {
if(optMin == "true"){
startMine();
}
} else {
opt();
chkOptCookie();
}
}
chkOptCookie();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment