Skip to content

Instantly share code, notes, and snippets.

@dun4n
dun4n / initArray.js
Last active August 29, 2015 14:12
Create an Array with values from 1 to N
function f(N) {
return Array.apply(null, {length: N}).map(Number.call, Number);
}
@dun4n
dun4n / replacer.js
Created March 5, 2014 10:43
#JavaScript simple String match / replace from arguments
String.prototype.replacer = function() {
var args = arguments
return this.replace(/\{([0-9]*)\}/g, function(match, parameter) {
return args[parseInt(parameter)]
})
}
var hello = "Hello {0} {1}!"
hello.replacer("John", "Doe") // "Hi John Doe!"
@dun4n
dun4n / vcard.html
Last active November 4, 2024 15:26
#JavaScript vcard generator example
<!doctype html>
<html>
<head>
<script type="text/javascript" src="vcard2.js"></script>
</head>
<body>
<script type="text/javascript">
// With helper methods
var fooBar = vCard.create(vCard.Version.FOUR)
fooBar.addFormattedname("Mr Foo Bar")
@dun4n
dun4n / csv.html
Created February 21, 2014 13:00
Export #JavaScript Array as CSV
<!doctype html>
<html>
<head>
<script type="text/javascript">
var languages = [ // source: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
['Feb 2014', 'Feb 2013', 'Programming Language', 'Ratings', 'Change'],
[1, 2, 'C', '18.334%', '+1.25%'],
[2, 1, 'Java', '17.316%', '-1.07%'],
[3, 3, 'Objective-C', '11.341%', '+1.54%'],
[4, 4, 'C++', '6.892%', '-1.87%'],
@dun4n
dun4n / generator.js
Last active August 29, 2015 13:56
Incremental ID Generator
function Generator() {
var ac = arguments.callee
return !ac.c && (ac.c = 0) || ac.c++
}
@dun4n
dun4n / github.csp.html
Last active January 4, 2016 04:39
Use CSP to detect if a user is logged in to Github.com.
<!DOCTYPE html>
<html>
<head>
<meta content="img-src https://github.com/login;" http-equiv="Content-Security-Policy">
<script type="text/javascript">
var islogged = true
window.onload = function() { console.log("Logged in to Github : " + islogged) }
</script>
@dun4n
dun4n / bypass.csp.html
Last active March 22, 2024 14:11
Bypass the CSP "unsafe-eval"
<!DOCTYPE html>
<html>
<head>
<meta content="script-src 'self';" http-equiv="Content-Security-Policy">
<script type="text/javascript" src="csp.js"></script>
</head>
</html>
@dun4n
dun4n / serialize.js
Created January 14, 2014 10:17
Serialize / Deserialize literal object.
function serialize(obj) {
return JSON.stringify(obj, function (key, value) {
return typeof value === 'function' ? value.toString() : value;
})
}
function deserialize(str) {
return JSON.parse(str, function (key, value) {
if (value && typeof value === "string" && value.substr(0,8) == "function") {
var body = value.substring(value.indexOf('{') + 1, value.lastIndexOf('}'))
@dun4n
dun4n / uri.config.js
Last active January 3, 2016 05:29
Retrieve a JSON config object from URI parameter
var config = (function() {
var s = document.getElementsByTagName('script');
var uri = Array.prototype.slice.call(s).slice(-1)[0].src.match(/\?.*/g)
return uri.length > 0 ? JSON.parse('{"' + decodeURI(uri[0].substr(1)).replace(/"/g, '\\"').replace(/&/g, '","').replace(/\=/g,'":"') + '"}') : {}
})()
/*
<script type="text/javascript" src="script.js?foo=bar"></script>
@dun4n
dun4n / async.eval.js
Last active January 2, 2016 09:09
Aynsc Javascript "eval"
var blob = new Blob(["some code"], {"type": "application/javascript"});
var script = document.createElement('script');
script.src = URL.createObjectURL(blob);
document.body.appendChild(script);