Skip to content

Instantly share code, notes, and snippets.

@chrisyip
chrisyip / encrypt-decrypt.js
Last active July 6, 2017 12:29 — forked from adrianbravo/encrypt-decrypt.js
Basic Node.js crypto cipher/decipher example.
'use strict'
const crypto = require('crypto')
const key = 'salt_from_the_user_document'
const plaintext = '56d008a27c36552a0f97b291'
const cipher = crypto.createCipher('aes-256-cbc', key)
const decipher = crypto.createDecipher('aes-256-cbc', key)
let encryptedPassword = cipher.update(plaintext, 'utf8', 'base64')
encryptedPassword += cipher.final('base64')
@chrisyip
chrisyip / .eslintrc
Created February 19, 2016 16:44
Configuration file for ESLint v2
{
"parserOptions": {
"ecmaVersion": 6
},
"env": {
"es6": true,
"node": true
},
@chrisyip
chrisyip / stack-loader.js
Last active March 4, 2016 07:41
Batch module loader for node.js.
'use strict'
const addtionalExts = []
const fs = require('fs')
const path = require('path')
function find (root, p, re, recursive) {
if (root) {
p = path.resolve(root, p)
}
@chrisyip
chrisyip / wechat-fluid-badge.js
Last active January 6, 2016 11:05
Add badge for WeChat w/ Fluid
setInterval(updateDockBadge, 500)
function updateDockBadge() {
var dots = document.querySelectorAll('.nav_view .chat_item .avatar .web_wechat_reddot_middle')
var badge = [].reduce.call(dots, function(pv, cv) {
var badge = Math.floor(cv.textContent)
if (isNaN(badge)) {
return 0
@chrisyip
chrisyip / progress-bar.js
Created August 29, 2015 07:53
SVG progress bar
class ProgressBar {
constructor (viewSize) {
this.viewSize = viewSize
this.circleStrokeColor = '#249fff'
this.circleStrokeWidth = 5
this.circleFillColor = 'transparent'
let svg = this.svg = this.createSVG()
svg.setAttribute('width', viewSize)
@chrisyip
chrisyip / safeApply.js
Last active August 29, 2015 14:21
Safer $apply for angular, suppress $digest already in progress error
angular.module('myApp', [])
.run(function ($rootScope, $exceptionHandler) {
$rootScope.$safeApply = function (exp) {
var phase = (this.$root || this).$$phase
if (phase !== '$apply' && phase !== '$digest') {
this.$apply(exp)
} else {
try {
this.$eval(exp)
} catch (ex) {
@chrisyip
chrisyip / console.trace.js
Created May 16, 2015 16:24
console.trace
// http://stackoverflow.com/questions/6715571/how-to-get-result-of-console-trace-as-string-in-javascript-with-chrome-or-fire
function getStackTrace () {
var obj = {}
// Remove arguments.callee from trace list
Error.captureStackTrace(obj, getStackTrace)
return obj.stack
}
console.log(getStackTrace())
@chrisyip
chrisyip / tower
Created April 20, 2015 04:35
Script for opening Git Tower
#!/usr/bin/env bash
if [ -x "/Applications/Tower.app" ] || [ -x "$HOME/Applications/Tower.app" ]; then
tower="Tower"
else
echo "Tower not found!"
exit 2
fi
if [ -z "$1" ]; then
@chrisyip
chrisyip / iojs.sh
Last active February 16, 2016 06:03
Get latest installed node from nvm path
#!/usr/bin/env sh
if [ -d "$(brew --prefix)/Cellar" ]; then
node=`find /usr/local/Cellar -name node | grep iojs/.*/bin/node`
if [ ! -z "$node" ]; then
$node $@
else
echo "io.js not installed"
fi
else
@chrisyip
chrisyip / who_uses_my_ports.sh
Created December 16, 2014 18:29
List ports being used
sudo lsof -i -P | grep -i "listen"