Skip to content

Instantly share code, notes, and snippets.

View sonnyp's full-sized avatar
🛠️

Sonny sonnyp

🛠️
View GitHub Profile
@sonnyp
sonnyp / fileInputSupport.js
Created April 4, 2012 14:19
On Safari mobile file input are disabled, this function check whether it's supported or not.
function fileInputSupport() {
var input = document.createElement('input');
input.type = 'file';
return !input.disabled;
}
@sonnyp
sonnyp / ASAP.js
Last active October 10, 2015 04:47
Execute some DOM related operation when document is ready
var whenDocumentIsReady = function(callback) {
if (document.readyState !== 'loading')
callback.call(document);
else
document.addEventListener('DOMContentLoaded', callback);
};
@sonnyp
sonnyp / hidescrollbars.js
Created September 17, 2012 16:32
hide scrollbars
var hideScrollbars = function(aElement) {
if (aElement.clientWidth === aElement.offsetWidth)
return;
aElement.style.overflow = 'hidden';
if ('onwheel' in scrollbox) {
scrollbox.addEventListener('wheel', function(e) {
this.scrollTop += e.deltaY;
this.scrollLeft += e.deltaX;
// This code co-ordinates tabs from the same domain to elect one of them as a
// "master", and allow them to broadcast messages to each other.
function WindowController () {
var now = Date.now(),
ping = 0;
try {
ping = +localStorage.getItem( 'ping' ) || 0;
} catch ( error ) {}
if ( now - ping > 45000 ) {
this.becomeMaster();
/*
A simple new-line delimited JSON protocol with upgrades.
Receiving Usage:
protocol = require('./frame-protocol');
// parsing data
parser = protocol.Parser();
@sonnyp
sonnyp / .editorconfig
Created January 8, 2016 23:19
.editorconfig
# EditorConfig is awesome: http://EditorConfig.org
root = true
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
@sonnyp
sonnyp / iife.js
Last active April 13, 2016 13:18
JavaScript IIFE for Node.js and browsers
// global will refer to global for Node.js and window for browsers
;(function (global) { // ; in case the file is concatenated with a non ; line ending
'use strict'
// your code
}(typeof global !== 'undefined' ? global : this)) // `this`doesn't refer to global in Node.js module
@sonnyp
sonnyp / HTML5.html
Last active April 17, 2016 16:14
HTML5 template
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>TITLE</title>
<meta name="description" content="$DESCRIPTION">
<meta name="author" content="$AUTHOR">
<meta name="keywords" content="$KEYWORDS">
@sonnyp
sonnyp / bind.xml
Created June 6, 2016 14:41
prosody bind issue
<!-- OUT -->
<iq to="localhost" type="set" id="40924263533590377" xmlns="jabber:client">
<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/>
</iq>
<!-- IN -->
<iq id="40924263533590377" type="error" xmlns="jabber:client" from="localhost">
<error type="cancel">
<service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error>
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, (c) => {
return `%${c.charCodeAt(0).toString(16)}`;
});
}
function urlEncode(params) {
let data = '';
Object.keys(params).forEach((key, idx, { length }) => {
const k = fixedEncodeURIComponent(key.toString());