Skip to content

Instantly share code, notes, and snippets.

View Atorui's full-sized avatar

Atorui

  • Madiun, Indonesia
View GitHub Profile
@Atorui
Atorui / textThrowInsideClosureSavedInVar.js
Created June 24, 2016 01:09
Just a test: throwing error inside a closure saved in a var will stop the program too.
var cok = (function() {
var jos = 1;
var sem = (function() {
if (jos !== 1) {
return 1;
} else {
throw new Error("Error iki lho bro")
}
}());
@Atorui
Atorui / JSONP.js
Last active June 22, 2016 15:32 — forked from nekman/JSONP.js
JSONP + Promise
/*
* From http://stackoverflow.com/a/22780569/141363 modified
* to return Promise.
*
* Usage: jsonp(url)
* .then(success)
* .catch(error);
*/
var jsonp = (function(global, body) {
'use strict';
@Atorui
Atorui / globalCanSeePrivate.js
Created June 22, 2016 14:13
An Example of Javascript User-Defined Global Method Can Read (and Write) Private Properties (or Method) Defined inside Closure
(function(WIN) {
var sem = 1;
WIN.sui = function() {
console.log(sem)
};
function cok() {
WIN.sui();
}
@Atorui
Atorui / refineJsonpResponseText.js
Created June 22, 2016 13:58
Process Response Text in JSONP format Performed via XMLHttpRequest. This script has been tested in the context of social media count only (further test in another context has not been performed), in which the response can be parsed using JSON.parse().
function refineJsonpResponseText(response, callbackName) {
var responseText = response.replace(/\s/g, ""),
tryFirstBorder = responseText.indexOf(callbackName + "({"),
isFirstBorder = tryFirstBorder !== -1,
tryEndBorder = responseText.lastIndexOf("})"),
firstBorder = isFirstBorder ? tryFirstBorder : responseText.indexOf(callbackName + "(\""),
endBorder = tryEndBorder !== -1 ? tryEndBorder : responseText.lastIndexOf("\");");
responseText = responseText.slice((firstBorder + 3), (endBorder + 1));
responseText = JSON.parse(responseText);
@Atorui
Atorui / xhrPromise.js
Created June 22, 2016 13:49
Wrap XMLHttpRequest in Promise
function xhrPromise(url) {
return new Promise(function(resolve, reject) {
var ajax = new XMLHttpRequest();
ajax.onload = function () {
if (this.status >= 200 && this.status < 300 || this.status == 304) {
resolve(this.response);
} else {
reject(this.statusText);
}
@Atorui
Atorui / application.html.erb
Created June 19, 2016 10:11 — forked from garethrees/application.html.erb
Boilerplate HTML <head> Section
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Make a DNS handshake with a foreign domain, so the connection goes faster when the user eventually needs to access it. This works well for loading in assets (like images) from another domain, or a JavaScript library from a CDN. -->
<link rel="dns-prefetch" href="//ajax.googleapis.com" />
<link rel="dns-prefetch" href="//s3.amazonaws.com" />
<!-- Make sure the latest version of IE is used -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
/*
* http://christian-fei.com/tutorials/how-to-lazy-load-disqus-comments/
*
* <div class="comments"></div>
*/
var comments = document.getElementsByClassName('comments')[0],
disqusLoaded = false;
function loadDisqus() {
@Atorui
Atorui / rAF.js
Created June 5, 2016 05:20 — forked from paulirish/rAF.js
requestAnimationFrame polyfill
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
@Atorui
Atorui / append-css-link-or-rule.js
Created June 2, 2016 12:23 — forked from chrisyip/append-css-link-or-rule.js
Dynamically add CSS link or rule to DOM w/ pure JavaScript
@Atorui
Atorui / get-social-shares
Created May 31, 2016 13:50 — forked from ihorvorotnov/get-social-shares
Get number of shares from social platforms
Facebook*:
https://api.facebook.com/method/links.getStats?urls=%%URL%%&format=json
+ works, returns shares, likes, comments and total
Twitter:
http://urls.api.twitter.com/1/urls/count.json?url=%%URL%%&callback=twttr.receiveCount
+ v1 API but still works
Reddit:
http://buttons.reddit.com/button_info.json?url=%%URL%%