Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- // PD4 Tipping Utility
- // Created by Serlite
- //
- // Instructions:
- // - Paste code below into console
- // - Tipping controls will appear beneath chat interface
- // - Each line is considered a separate tip; either specify only a username and the base
- // amount will be used, or use the syntax "username value" to specify the tip amount:
- // User1
- // User2
- // User3 0.005
- // User4
- */
- loadjQuery();
- initializeTipTools();
- // Adds jQuery to document if needed
- function loadjQuery(){
- if (!window.jQuery){
- var script = document.createElement("SCRIPT");
- script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js';
- script.type = 'text/javascript';
- document.getElementsByTagName("head")[0].appendChild(script);
- }
- }
- // Creates GUI for tipping utility
- function initializeTipTools(){
- // Give time for jQuery to load
- if (!window.jQuery){
- setTimeout(initializeTipTools, 500);
- }
- else{
- jQuery(".chat").append(
- "<div style='padding:20px;flex-shrink:0;font-size:12px;'>"+
- "<p style='padding: 10px 0; font-size: 15px; border-top: 1px solid #EEE;'>Mass Tip Helper</p>"+
- "<textarea id='tip-name-input' style='border:1px solid #cfd8dc; border-radius:3px; width:100%; padding:11px 15px; font-size:12px;' placeholder='example_user_overridden 0.002'></textarea>"+
- "<label for='tip-amount-input' style='margin-top:10px;display:block;'>Base tip amount</label>"+
- "<input id='tip-amount-input' style='border:1px solid #cfd8dc; border-radius:3px; width:100%; padding:11px 15px; font-size:12px;' placeholder='0.00001000 min' type='text' />"+
- "<label for='tip-interval-input' style='margin-top:10px;display:block;'>Tip interval (seconds)</label>"+
- "<input id='tip-interval-input' style='border:1px solid #cfd8dc; border-radius:3px; width:100%; padding:11px 15px; font-size:12px;' placeholder='0' type='text' />"+
- "<label style='float:left;margin-top:21px;'>Private <input id='tip-private' style='vertical-align:middle;' type='checkbox' /></label>"+
- "<button id='tip-button' style='padding:10px 20px 12px; background:#4d5e80; box-shadow:inset 0 -2px 0 rgba(0,0,0,.2); color:#fff; border-radius:3px; font-weight:600; float:right; font-size:13px; margin-top:10px;'>Send Tips</button>"+
- "</div>"
- );
- jQuery("#tip-button").click(queueSendTips);
- }
- }
- function queueSendTips(){
- var tips = getNameValuePairs();
- var isPrivate = jQuery("#tip-private")[0].checked;
- var tipInterval = parseFloat(jQuery("#tip-interval-input").val());
- tipInterval = isNaN(tipInterval) ? 0 : tipInterval;
- // Early exit if no tips possible
- if (tips.length == 0){
- return;
- }
- if (confirm("You are about to send a total of " + calculateTotalAmount(tips) + " BTC to " + tips.length + " users. Is this correct?")){
- for (var i = 0; i < tips.length; i++){
- // Self-invoking function so tips[i] doesn't fall out of scope for the setTimeout()
- (function(i){
- setTimeout(function(){
- sendTip(tips[i], isPrivate);
- }, i * tipInterval * 1000);
- })(i);
- }
- }
- }
- function sendTip(tipObject, isPrivate){
- if (isPrivate){
- tipObject.hide = true;
- }
- try{
- tipObject.amount = Math.ceil(tipObject.amount * 100000000); // I wish we could just express things in actual BTC for the request!
- jQuery.post("https://api.primedice.com/api/tip?access_token=" + localStorage.getItem('token'), tipObject, function(response) {
- if (!response){
- console.log("Failed to tip " + tipObject.username + ": No response from server.");
- }
- else if (!response.user){
- console.log("Failed to tip " + tipObject.username + ": " + response);
- }
- });
- }
- catch(e){
- console.log("Error: " + e);
- }
- }
- function getNameValuePairs(){
- var baseAmount = jQuery("#tip-amount-input").val();
- var nameValues = jQuery("#tip-name-input").val().split("\n");
- var nameValuePairs = [];
- for (var i = 0; i < nameValues.length; i++){
- // Check for empty input
- if (nameValues[i] == ""){
- continue;
- }
- var valuePair = nameValues[i].split(" ");
- var amountSupplied = valuePair.length > 1;
- var tipAmount = amountSupplied ? valuePair[1] : baseAmount;
- // Some value checking so we don't bother tipping with invalid information
- if (!isNumber(tipAmount)){
- if (!amountSupplied){
- console.log("Invalid amount for " + valuePair[0] + ": No amount specified, and base amount (" + tipAmount + ") is missing/non-numeric.");
- }
- else{
- console.log("Invalid amount for " + valuePair[0] + ": Amount specified (" + tipAmount + ") is non-numeric.");
- }
- continue;
- }
- nameValuePairs.push(
- {
- username: valuePair[0],
- amount: tipAmount
- }
- );
- }
- return nameValuePairs;
- }
- function calculateTotalAmount(tips){
- var total = 0;
- for (var i = 0; i < tips.length; i++){
- total += Number(tips[i].amount);
- }
- return total.toFixed(8);
- }
- function isNumber(n) {
- return !isNaN(parseFloat(n)) && isFinite(n);
- }
Advertisement
Add Comment
Please, Sign In to add comment