Skip to content

Instantly share code, notes, and snippets.

View bpceee's full-sized avatar
💭
In meditation

Ken Bi bpceee

💭
In meditation
  • Auckland, New Zealand
View GitHub Profile
@bpceee
bpceee / index.html
Last active October 20, 2016 06:10
set caret(cursor) position in contenteditable element (div)
<html>
<!-- http://jsfiddle.net/timdown/vXnCM/-->
<head>
<title>Get/Set Caret in Textarea Example</title>
<script>
function test() {
var el = document.getElementById('editable');
var range = document.createRange();
@bpceee
bpceee / gist:9bc0c8536eae94143f4486e4e47a5fc3
Last active October 19, 2016 09:12
Get/set Cursor In Html TextArea
<html>
<!-- http://snipplr.com/view/5144/getset-cursor-in-html-textarea/ -->
<head>
<title>Get/Set Caret in Textarea Example</title>
<script>
function doGetCaretPosition (ctrl) {
var CaretPos = 0;
// IE Support
if (document.selection) {
@bpceee
bpceee / gist:e447405a777a3c67b5a5
Created December 28, 2015 06:28 — forked from Mithrandir0x/gist:3639232
Difference between Service, Factory and Provider in AngularJS
// Source: https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc
// jsFiddle: http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/
// author: Pawel Kozlowski
var myApp = angular.module('myApp', []);
//service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!"
@bpceee
bpceee / printPrototypeChain.js
Created December 13, 2015 09:09 — forked from LukeChannings/printPrototypeChain.js
prints the prototype chain for a constructed object.
/**
* prints the prototype chain for a constructed object.
* @param {Object} a constructed object.
* @param asArray {Boolean} if true, the chain will be returned as an array.
*/
function printPrototypeChain(instance, asArray) {
var chain = []
while (instance = Object.getPrototypeOf(instance)) {
@bpceee
bpceee / dropzone-directive.js
Created October 24, 2015 03:49 — forked from compact/dropzone-directive.js
AngularJS directive for Dropzone.js
/**
* An AngularJS directive for Dropzone.js, http://www.dropzonejs.com/
*
* Usage:
*
* <div ng-app="app" ng-controller="SomeCtrl">
* <button dropzone="dropzoneConfig">
* Drag and drop files here or click to upload
* </button>
* </div>
@bpceee
bpceee / gist:bbaef12e0853b6165cd1
Last active August 29, 2015 14:28
Node.js 中使用 request 请求 GBK 页面
var request = require('request');
var Iconv = require('iconv').Iconv;
var iconv = new Iconv('GBK', 'UTF-8//TRANSLIT//IGNORE');
request.get({
url: 'http://foo.bar',
encoding: null
}, function(err, res, body) {
console.log(iconv.convert(body).toString());
});
@bpceee
bpceee / uri.js
Last active August 29, 2015 14:27 — forked from jlong/uri.js
URI Parsing with Javascript
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
@bpceee
bpceee / gist:0a58e4148e296439b255
Created July 26, 2015 14:14
calculate the SVG Path for an arc (of a circle)
http://stackoverflow.com/questions/5736398/how-to-calculate-the-svg-path-for-an-arc-of-a-circle
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
var angleInRadians = angleInDegrees * Math.PI / 180.0;
var x = centerX + radius * Math.cos(angleInRadians);
var y = centerY + radius * Math.sin(angleInRadians);
return [x,y];
}
@bpceee
bpceee / partialfun.js
Created October 15, 2014 03:51
partial function
function partial(f) {
var args = Array.prototype.slice.call(arguments, 1)
return function() {
var remainingArgs = Array.prototype.slice.call(arguments)
return f.apply(null, args.concat(remainingArgs))
}
}
@bpceee
bpceee / azurecros.js
Last active August 29, 2015 14:07
set azure cros
var crypto = require('crypto');
var request = require('request');
var setCors = function (MY_ACCOUNT_URL, MY_ACCOUNT_NAME, accountKey) {
var MY_CORS_XML =
'<?xml version="1.0" encoding="utf-8"?>'+
'<StorageServiceProperties>'+
'<Cors>'+
'<CorsRule>'+