Last active
January 23, 2021 23:34
-
-
Save yohanesgultom/fa00934584ecac3b1a304f996574d804 to your computer and use it in GitHub Desktop.
Random javascripts (scripts?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Random javascripts (scripts?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Good old jquery form submit leveraging HTML5 validation | |
// Disabling buttons during async process | |
// Enabling buttons once async process completed | |
// Data Type flavors: FormData, JSON or URL-encoded | |
$('form').on('submit', function(e) { | |
let form = $(this) | |
e.preventDefault() | |
form.find('button').attr('disabled', true) | |
// form data | |
let data = new FormData(this) | |
let config = { | |
url: 'https://httpbin.org/post', | |
type: "POST", | |
data: data, | |
processData: false, | |
contentType: false, | |
} | |
if (data.get('dataType') == 'json') { | |
data = {} | |
form.serializeArray().map(x => data[x.name] = x.value, {}) | |
config = { | |
url: 'https://httpbin.org/post', | |
type: "POST", | |
data: JSON.stringify(data), | |
dataType: "json", | |
contentType: "application/json; charset=utf-8", | |
} | |
} else if (data.get('dataType') == 'urlEncoded') { | |
data = form.serialize() | |
console.log(data) | |
config = { | |
url: 'https://httpbin.org/post', | |
type: "POST", | |
data: data, | |
} | |
} | |
// submit | |
$.ajax(config).done(function(res) { | |
alert('Success. Yay') | |
console.log(res) | |
}) | |
.fail(function(err) { | |
alert('Failed. Booo') | |
console.log(err) | |
}) | |
.always(function() { | |
form[0].reset() | |
form.find('button').removeAttr('disabled') | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'); | |
const bitcoin = require("bitcoinjs-lib"); | |
const bitcoinNetwork = bitcoin.networks.testnet; | |
/** | |
* Send bitcoin in testnet using BlockCypher | |
* @param {number} amount - Bitcoin amount in BTC | |
* @param {string} to - output Bitcoin wallet address | |
* @param {string} from - input Bitcoin wallet address | |
* @param {string} wif | |
*/ | |
const sendBitcoin = function (amount, to, from, wif) { | |
let keys = bitcoin.ECPair.fromWIF(wif, bitcoinNetwork); | |
return new Promise(function (resolve, reject) { | |
// create tx skeleton | |
request.post({ | |
url: 'https://api.blockcypher.com/v1/btc/test3/txs/new', | |
body: JSON.stringify({ | |
inputs: [{ addresses: [ from ] }], | |
// convert amount from BTC to Satoshis | |
outputs: [{ addresses: [ to ], value: amount * Math.pow(10, 8) }] | |
}), | |
}, | |
function (err, res, body) { | |
if (err) { | |
reject(err); | |
} else { | |
let tmptx = JSON.parse(body); | |
// signing each of the hex-encoded string required to finalize the transaction | |
tmptx.pubkeys = []; | |
tmptx.signatures = tmptx.tosign.map(function (tosign, n) { | |
tmptx.pubkeys.push(keys.getPublicKeyBuffer().toString('hex')); | |
return keys.sign(new Buffer(tosign, 'hex')).toDER().toString('hex'); | |
}); | |
// sending back the transaction with all the signatures to broadcast | |
request.post({ | |
url: 'https://api.blockcypher.com/v1/btc/test3/txs/send', | |
body: JSON.stringify(tmptx), | |
}, | |
function (err, res, body) { | |
if (err) { | |
reject(err); | |
} else { | |
// return tx hash as feedback | |
let finaltx = JSON.parse(body); | |
resolve(finaltx.tx.hash); | |
} | |
} | |
); | |
} | |
} | |
); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from https://gist.github.com/anvk/5602ec398e4fdc521e2bf9940fd90f84 | |
function workMyCollection(arr) { | |
return arr.reduce((promise, item) => { | |
return promise | |
.then((result) => { | |
return asyncFunc(item); | |
}) | |
.catch(console.error); | |
}, Promise.resolve()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cropper = { w: 369, h: 390 }, | |
draw = SVG('cropper').size(cropper.w, cropper.h), | |
jawMask = draw.path('M 50 10 C 50 20 100 20 100 10 L 100 60 C 100 80 50 80 50 60 Z') | |
addHandlers(jawMask, [[0, 1, 2], [1, 5, 6], [2, 1, 2], [3, 5, 6]]) | |
function addHandlers(path, points) { | |
var array = path.array().value, | |
doc = path.doc(), | |
handlers = [] | |
// save points to be moved together | |
for (var i = 0; i < points.length; i++) { | |
var p = points[i], | |
x = array[p[0]][p[1]], | |
y = array[p[0]][p[2]], | |
rect = doc.rect(6, 6).data('point', p).fill('#fff').center(x, y).draggable().on('dragmove', function(e) { | |
p = this.data('point') | |
array[p[0]][p[1]] = e.detail.p.x | |
array[p[0]][p[2]] = e.detail.p.y | |
path.plot(array.join(' ')) | |
}) | |
handlers.push(rect.id()) | |
} | |
path.data('handlerIds', handlers) | |
path.on('dragmove', function(e) { | |
var ids = this.data('handlerIds'), | |
array = path.array().value | |
for (var i = 0; i < ids.length; i++) { | |
var handler = SVG.get(ids[i]), | |
p = handler.data('point'), | |
x = array[p[0]][p[1]], | |
y = array[p[0]][p[2]] | |
handler.center(x, y) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment