Node.js - Kirim SBD ke Aktif Follower #6

Words
202
Reading
1 min
Listen
Play
9y

Teman teman steemians, hari ini saya mau membagikan script steem.js fungsi script ini untuk mengirimkan tips kepada semua Active Follower kita.

Intro

Banyak diantara teman teman ingin membagikan tip kepada followernya yang aktif di upvote atau komentar di postingannya, tapi kalau kita kirim manual dengan menggunakan wellet di steemit membutuhkan terlalu waktu. maka dari itu timbul inisiatif saya untuk membagikan script yang akan kita gunakan sebagai perantara kerja pengiman SBD ke semua follower aktif kita.

Coding

  1. Pertama sekali bukalah editor favorit teman teman, disini saya menggunakan notepad++, kemudian pastekan code berikut ini:
<html>
   <head>
      <link rel="stylesheet" href="css/styles.css">
      <script type="text/javascript" src="code/common.js"></script>
      <script type="text/javascript" src="code/tipper.js"></script>    
      <script src="https://cdn.steemjs.com/lib/latest/steem.min.js"></script>    
   </head>
   <body>
     
      <div class='link' align='center'><a target="_blank" href="https://steemit.com/@binjeeclick" style="">TIP/MESSAGE YOUR ACTIVE FOLLOWERS IN THEIR WALLET</a></div>
      <br />
      <div align='center'>

      </div>
      <table align='center'>
         <tr>
            <td colspan='3'>
               <hr />
            </td>
         </tr>
         <tr>
            <td style='width: 100px'><label for='accountName'>Account Name: </label></td>
            <td><input type='text' placeholder='Your Account Name' id='accountName' onchange='checkAccountName();' /></td>
            <td style='width: 110px'>
               <div class='error' id='accountName_err'></div>
            </td>
         </tr>
         <tr>
            <td><label for='activeKey'>Active Key: </label></td>
            <td><input type='password' placeholder='Your Active Key' id='activeKey' onchange='checkActiveKey();' /></td>
            <td>
               <div class='error' id='activeKey_err'></div>
            </td>
         </tr>
         <tr>
            <td><label for='tipAmount'>SBD Amount: </label></td>
            <td>
               <input type='number' min='0.001' value='0.001' id='tipAmount' onchange='checkTipAmount();' />
            </td>
            <td>
               <div class='error' id='tipAmount_err'></div>
            </td>
         </tr>
         <tr>
            <td><label for='inactiveHours'>Inactive Hours: </label></td>
            <td>
               <input type='number' min='1' placeholder='Acceptable Number of Inactive Hours' id='inactiveHours' onchange='checkInactiveHours();' />
            </td>
            <td>
               <div class='error' id='inactiveHours_err'></div>
            </td>
         </tr>
         <tr>
            <td><label for='tipMessage'>Tip Message: </label></td>
            <td><textarea rows='5' placeholder="Tip Message/Transfer Memo
Example: Celebrating 1000+ Followers! <3" id='tipMessage' onchange='checkTipMessage();'></textarea></td>
            <td>
               <div class='error' id='tipMessage_err'></div>
            </td>
         </tr>
         <tr>
            <td></td>
            <td></td>
         </tr>
         <tr>
            <td>&nbsp;</td>
            <td colspan='2'>
               <input type='button' id='calculateFee' value='Calculate Fee' onclick='calculateFee();' />
               <input type='button' id='transfer' value='Tip/Transfer' onclick='transferTheTips();' disabled='disabled' /> &nbsp;
               <span id='status'></span></span>
            </td>
         </tr>
         <tr>
            <td colspan='3'>
               <hr />
            </td>
         </tr>
         <tr>
            <td colspan='3'>
               <div class='output' id='result'></div>
            </td>
         </tr>
      </table>
   </body>
</html>

Simpan document tersebut dengan extensi .html misalnya index.html

untuk membahas script diatas bisa langsung hubungi saya di server NSC DISCORD

  1. Buka document baru pastekan code berikut:
var app = 'tipper';
var followers = new Array();
var followers_ = new Array();
var followers__ = new Array();
var lastOne = '0';
var chunk = 1000;
var myAccount = 'binjeeclick';
var myMemo = 'Fee Tipper';
var myFee = 0.001 //Per Transaction
var totalFee = 0; //Total Fee
var feeFlag = false;

function appendHTML(html, resetFlag) {
   var div = document.createElement("div");
   div.innerHTML = html;

   if (resetFlag == null) {
      resetFlag = false;
   }
   if (resetFlag) {
      document.getElementById('result').innerHTML = "";
   }

   document.getElementById('result').appendChild(div);
   window.scrollTo(0, document.body.scrollHeight);
}


function checkAccountName(flag) {
   if (flag == null) {
      flag = false;
   }
   disableTransfer();
   var accountName = document.getElementById('accountName').value;
   steem.api.getAccounts([accountName], function (err, result) {
      if (result.length == 0) {
         var errorMessage = 'Not available!';
         document.getElementById('accountName_err').innerHTML = errorMessage;
         return false;
      } else {
         if (parseFloat(result[0].sbd_balance) < totalFee) {
            var errorMessage = 'Not enough SBD!';
            document.getElementById('accountName_err').innerHTML = errorMessage;
            document.getElementById('transfer').disabled = true;
            return false;
         } else {
            document.getElementById('accountName_err').innerHTML = '';
            if (flag && totalFee > myFee) {
               disableTransfer(false);
            }
            return true;
         }
      }
   });
}

function checkTipAmount() {
   disableTransfer();
   var tipAmount = document.getElementById('tipAmount').value;
   var errorMessage = '';
   document.getElementById('tipAmount_err').innerHTML = errorMessage;
   var reg = /^\d*[\.]\d*$/;
   var tip = parseFloat(tipAmount);
   if (!tipAmount.match(reg) || tip < 0.001 || tipAmount.split('.')[1].length > 3) {
      var errorMessage = 'Not valid!';
      document.getElementById('tipAmount_err').innerHTML = errorMessage;
      return false;
   } else {
      return true;
   }
}

function validate() {
   checkAccountName();
   checkActiveKey();
   checkTipAmount();
   checkInactiveHours();
   checkTipMessage();

   var elements = document.getElementsByClassName('error');
   for (e = 0; e < elements.length; e++) {
      if (elements[e].innerHTML != "") {
         return false;
      }
   }

   return true;
}

function calculateFee() {
   totalFee = myFee;
   if (validate()) {
      appendHTML("", true);
      disableAll(true);
      startLoading();
      var accountName = document.getElementById('accountName').value;
      getFollowers(accountName);
   }
}

function checkTipMessage() {
   var tipMessage = document.getElementById('tipMessage').value;
   var errorMessage = '';
   document.getElementById('tipMessage_err').innerHTML = errorMessage;
   if (tipMessage == null || tipMessage == '') {
      var errorMessage = 'Type something!';
      document.getElementById('tipMessage_err').innerHTML = errorMessage;
      return false;
   } else {
      return true;
   }
}

function checkInactiveHours() {
   disableTransfer();
   var inactiveHours = document.getElementById('inactiveHours').value;
   var errorMessage = '';
   document.getElementById('inactiveHours_err').innerHTML = errorMessage;
   var reg = /^[\d]\d*$/;
   var hours = parseInt(inactiveHours);
   if (!inactiveHours.match(reg) || hours < 1) {
      var errorMessage = 'Not valid!';
      document.getElementById('inactiveHours_err').innerHTML = errorMessage;
      return false;
   } else {
      return true;
   }
}

function checkActiveKey() {
   disableTransfer();
   var activeKey = document.getElementById('activeKey').value;
   var errorMessage = '';
   document.getElementById('activeKey_err').innerHTML = errorMessage;

   if (!steem.auth.isWif(activeKey)) {
      var errorMessage = 'False key!';
      document.getElementById('activeKey_err').innerHTML = errorMessage;
      return false;
   } else {
      var accountName = document.getElementById('accountName').value;
      steem.api.getAccounts([accountName], function (err, result) {
         if (result.length > 0) {
            var activeKey = document.getElementById('activeKey').value;
            var publicKey = result[0].active.key_auths[0][0];
            if (!steem.auth.wifIsValid(activeKey, publicKey)) {
               var errorMessage = 'Wrong key!';
               document.getElementById('activeKey_err').innerHTML = errorMessage;
               return false;
            }
         } else {
            return true;
         }
      });
   }
}

function addFollowers(following, fresult) {
   if (followers.length > 1) {
      fresult.shift();
   }
   followers = followers.concat(fresult);
   lastOne = followers[followers.length - 1].follower;
   if (fresult.length != 0) {
      queryFollowers(following);
   } else {
      for (f_ in followers) {
         followers_.push(followers[f_].follower);
      }

      removeInactiveFollowers();
   }
}

function queryFollowers(following) {
   steem.api.getFollowers(following, lastOne, 'blog', chunk, function (err, result) {
      addFollowers(following, result);
   });
}

function getFollowers(following) {
   followers = new Array();
   followers_ = new Array();
   followers__ = new Array();
   lastOne = '0';
   queryFollowers(following);
}

function removeInactiveFollowers() {
   //Remove Dead Followers
   steem.api.getAccounts(followers_, function (err, result) {
      for (r in result) {
         var follower = result[r];
         var lastActive = new Date(follower.last_post);
         var today = new Date();
         var hours = document.getElementById('inactiveHours').value;
         if ((today.getUTCTime() - lastActive.getTime()) / (1000 * 60 * 60) <= hours) {
            followers__.push(follower.name);
         }
      }

      onInactiveFollowersRemoved();
   });
}

function onInactiveFollowersRemoved() {
   var tipAmount = document.getElementById('tipAmount').value;
   var tip = parseFloat(parseFloat(tipAmount).toFixed(3));
   totalFee = followers__.length * (myFee + tip);
   checkAccountName(true);

   appendHTML('Active Followers: ' + followers__.length, true);
   appendHTML('Total SBD: ' + totalFee.toFixed(3) + ' SBD');
   stopLoading();
   disableAll(false);
}

function disableAll(flag) {
   if (flag == null) {
      flag = true;
   }
   document.getElementById('accountName').disabled = flag;
   document.getElementById('activeKey').disabled = flag;
   document.getElementById('tipAmount').disabled = flag;
   document.getElementById('inactiveHours').disabled = flag;
   document.getElementById('tipMessage').disabled = flag;
   document.getElementById('calculateFee').disabled = flag;

   if (flag) {
      document.getElementById('transfer').disabled = flag;
   }
}

function disableTransfer(flag) {
   if (flag == null) {
      flag = true;
   }
   document.getElementById('transfer').disabled = flag;
}

var barId = 0;

function startLoading(milliseconds) {
   if (milliseconds == null) {
      milliseconds = true;
   }
   document.getElementById('status').innerHTML = 'PLEASE WAIT';
   barId = window.setInterval(loadBar, milliseconds);
}

function stopLoading(message) {
   if (message == null) {
      message = '';
   }
   window.clearInterval(barId);
   document.getElementById('status').innerHTML = message;
   document.getElementById('bar').innerHTML = '';
}

function loadBar() {
   var barValue = document.getElementById('bar').innerHTML;
   if (barValue.length == 3) {
      barValue = '';
   } else {
      barValue += '.';
   }

   document.getElementById('bar').innerHTML = barValue;
}

function transferTheTips() {
   var myFee_ = (followers__.length * myFee).toFixed(3) + " SBD";
   var confirm_ = confirm("This is a clientside application, which means if you close this window, the application will stop without completeing all the transactions. The first transaction will be @binjeeclick fee which is " + myFee_ + ". All transfers are non-refundable. Are you sure you want to continue?");

   if (confirm_) {
      disableAll();
      startLoading();

      var activeKey = document.getElementById('activeKey').value;
      var accountName = document.getElementById('accountName').value;

      appendHTML("
"
); steem.broadcast.transfer(activeKey, accountName, myAccount, myFee_, myMemo, function (err, result) { if (err == null) { appendHTML("Transfered " + myFee_ + " To @binjeeclick."); var activeKey = document.getElementById('activeKey').value; var accountName = document.getElementById('accountName').value; var tipMessage = document.getElementById('tipMessage').value; var tipAmount = document.getElementById('tipAmount').value; tipAmount = parseFloat(tipAmount).toFixed(3) + " SBD"; bulkTransfer(accountName, activeKey, tipAmount, tipMessage); } else { appendHTML("Transfering " + myFee_ + " To @binjee Failed."); disableAll(false); console.log(err); stopLoading(); } }); } } function bulkTransfer(accountName, activeKey, tipAmount, tipMessage, index) { if (index == null) { index = 0; } if (index == followers__.length) { appendHTML("
"
); appendHTML("ALL DONE! :]"); stopLoading(); return; } else { steem.broadcast.transfer(activeKey, accountName, followers__[index], tipAmount, tipMessage, function (err, result) { if (err == null) { appendHTML("Transfered " + tipAmount + " To @" + followers__[index] + "."); } else { followers__.push(followers__[index]); appendHTML("Transfering " + tipAmount + " To @" + followers__[index] + " Failed. Will try this transaction again a bit later."); console.log(err); } bulkTransfer(accountName, activeKey, tipAmount, tipMessage, index + 1); }); } }

Simpan document tersebut dengan extensi .js dengan nama tipper.js

  1. buka document baru pastekan code berikut:
window.onload = function () {
   var link = 'https://rawgit.com/msg768/mysteemit/master/';
   if (!window.location.href.startsWith(link)) {
      //alert("Please don't steal my work/code! Thanks...");
      //window.location.href = link+app+'.html';
   } else {
      if (app == 'sbdraffle') {
         start();
      }
   }

   //steem.config.set('websocket', 'wss://steemd.steemit.com');
   //steem.config.set('websocket', 'wss://steemd.steemitdev.com');
   //steem.config.set('websocket', 'wss://gtg.steem.house:8090');
   //steem.config.set('websocket', 'wss://seed.bitcoiner.me');
   //steem.config.set('websocket', 'wss://this.piston.rocks');
   //steem.config.set('websocket', 'wss://node.steem.ws');
}

Number.prototype.zeroPad = function (length) {
   length = length || 2; // defaults to 2 if no parameter is passed
   return (new Array(length).join('0') + this).slice(length * -1);
};

Date.prototype.getUTCTime = function () {
   return this.getTime() + (this.getTimezoneOffset() * 60000);
};

Date.prototype.getUTCString = function () {
   return this.getUTCFullYear() + '-' + (this.getUTCMonth() + 1).zeroPad(2) + '-' + (this.getUTCDate()).zeroPad(2) + 'T' + (this.getUTCHours()).zeroPad(2) + ':' + (this.getUTCMinutes()).zeroPad(2) + ':' + (this.getUTCSeconds()).zeroPad(2);
};

Date.prototype.getSteemString = function () {
   return this.getFullYear() + '-' + (this.getMonth() + 1).zeroPad(2) + '-' + (this.getDate()).zeroPad(2) + 'T' + (this.getHours()).zeroPad(2) + ':' + (this.getMinutes()).zeroPad(2) + ':' + (this.getSeconds()).zeroPad(2);
};

function customRand(seed) {
   var x = Math.sin(seed) * 10000;
   return x - Math.floor(x);
}

Simpan dengan nama common.js
Note: Script diatas di buat oleh @msg768

Simpan semua script diatas dalam satu folder beri nama terserah anda misalnya app dalam folder app buatlah sebuah folder baru dengan nama code dalam folder app isinya index.html dan folder code dalam folder code isi nya tipper.js dan common.js

  1. jalankan file index.html di browser anda bila semua berjalan lancar anda akan melihat tampilan seper ini

Credit: binjeeclick@binjeeclick


Salam hangat dari NSC

nanggroe.png

Upvote dan resteem @binjeeclick Bergabunglah bersama Komunitas Nanggroe Steemit Community di Discord dengan klik link ini https://discord.gg/7cgkwtS


Node.js - Kirim SBD ke Aktif Follower #6 | Ecency