Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
JavaScript-Packer / escaping-characters-javascript.js
Created August 2, 2015 23:15
Escape HTML entities, JS safe escaping, URL encoding, etc. SOme functions used on http://www.whak.ca/packer/LZMA.htm
function esc_HTML(n) {
return n.replace(/[&\n\r "'<>\v\0]/g, function(n) {
return "&#" + n.charCodeAt(0) + ";";
});
}
function esc_URL(n) {
return n.replace(/[^\w\d]/g, function(n) {
return "%" + function(n, r, t) {
return t = t || "0", n += "", n.length < r ? Array(r - n.length + 1).join(t) + n : n;
<center><pre id=out style=font-size:32px></pre><script>function a(a){var b,f,c,d,e,q;for(g.innerHTML=a,b=m,c=n,d=o,e=p,q=a[0],a=(15&c+a[1])-c,f=4;f--;)if(e[b+q+d[c+a+f]]){if(q>1){for(f=4;f--;)e[b+d[c+f]]=1;for(f=0;i*k>f;f+=k){for(q=2;k>q;)q=e[f+q]?q+1:j;if(j>q)for(q=f;--q;)e[q+k]=e[q]}b=h/2,d=l[new Date%7]}q=a=0;break}for(b=[b+q,c+a,d,e],m=b[0],n=b[1],o=b[2],p=b[3],e="",a=o,b=0;i>b;b++){for(p[b],c=0;h>c;c++){for(f=b*k+c+2,d=f-m,f=".#"[p[f]],q=0;4>q;++q)f=a[n+q]==d?"@":f;e+=f}e+="<br>"}for(b=0;h>b;b++)e+="=";return e+"\n"}function b(){g.innerHTML=a([k,0]),setTimeout(b,f-=2)}var c,d,e,f,g=out,h=10,i=12,j=(8<<h)-1,k=2+h+1,l=[[1,2,14,15,1,2,14,15,1,2,14,15,1,2,14,15],[2,13,14,15,1,14,27,28,13,14,15,26,0,1,14,27],[0,13,14,15,1,2,14,27,13,14,15,28,1,14,26,27],[1,2,13,14,1,14,15,28,14,15,26,27,0,13,14,27],[0,1,14,15,2,14,15,27,13,14,27,28,1,13,14,26],[1,13,14,15,1,14,15,27,13,14,15,27,1,13,14,27],[13,14,15,16,2,15,28,41,26,27,28,29,1,14,27,40]],m=-2*k+h/2,n=0,o=l[new Date%7],p=[];for(c=0;i*k>c;++c)d=c%k,p[c]=2>d||d>
@JavaScript-Packer
JavaScript-Packer / Space-Invaders-game.html
Last active August 29, 2015 14:26
This is a miniature ascii text version of the classic Space Invaders game. Live demo on http://jsfiddle.net/6hb5yfbw/1/ (click on link to play). It is written in the smallest amount of JavaScript that I could squeeze it. Entire game is 527 bytes! The core logic of the game is a single function that fits in less than 140 bytes.
<h1><pre id=x><script>function a(a,b,f,j){for(j="",a=0;10>a;a++){for(b=0;16>b;b++)f=b==c&9==a?3:0,a==e&&(f=g&1<<b-d?1:f),a==e+1&&(f=w&1<<b-d?1:f),f=b==h&a==i?1==f?4:2:f,f=9>e-!w+1?f:5,j+=".#i^@*"[f];j+="\n"}x.innerHTML=j}function b(){var c=0,j=0,c=1<<h-d;g-=j=i==e?g&c:0,w-=c=i==e+1?w&c:0,i-=j|c?10:1,g|w?0:(d=4,e=0,g=w=Z),(g|w)&1<<15*(f>0)-d?(e++,f=-f):d+=f,a(),setTimeout(b,300)}var c=8,d=4,e=0,f=1,g=w=Z=85,h=0,i=-1;this.onkeydown=function(b){b=event.keyCode,c-=37==b&c>0,c+=39==b&15>c,38==b&0>i&&(h=c,i=8),a()},b()</script>
@JavaScript-Packer
JavaScript-Packer / repeater.js
Created July 31, 2015 09:39
Repeat a string x times, choose the number of repeated times...
function repeat(e,r) {
return Array(r + 1).join(e);
}
//alert(repeat("WHAK.com!\n", 4));
function encode_ascii85(a) {
var b, c, d, e, f, g, h, i, j, k;
for (!/[^\x00-\xFF]/.test(a), b = "\x00\x00\x00\x00".slice(a.length % 4 || 4), a += b,
c = [], d = 0, e = a.length; e > d; d += 4) f = (a.charCodeAt(d) << 24) + (a.charCodeAt(d + 1) << 16) + (a.charCodeAt(d + 2) << 8) + a.charCodeAt(d + 3),
0 !== f ? (k = f % 85, f = (f - k) / 85, j = f % 85, f = (f - j) / 85, i = f % 85,
f = (f - i) / 85, h = f % 85, f = (f - h) / 85, g = f % 85, c.push(g + 33, h + 33, i + 33, j + 33, k + 33)) :c.push(122);
return function(a, b) {
for (var c = b; c > 0; c--) a.pop();
}(c, b.length), "<~" + String.fromCharCode.apply(String, c) + "~>";
}
function decode_ascii85(a) {
var c, d, e, f, g, h = String, l = "length", w = 255, x = "charCodeAt", y = "slice", z = "replace";
for ("<~" === a[y](0, 2) && "~>" === a[y](-2), a = a[y](2, -2)[z](/\s/g, "")[z]("z", "!!!!!"),
c = "uuuuu"[y](a[l] % 5 || 5), a += c, e = [], f = 0, g = a[l]; g > f; f += 5) d = 52200625 * (a[x](f) - 33) + 614125 * (a[x](f + 1) - 33) + 7225 * (a[x](f + 2) - 33) + 85 * (a[x](f + 3) - 33) + (a[x](f + 4) - 33),
e.push(w & d >> 24, w & d >> 16, w & d >> 8, w & d);
return function(a, b) {
for (var c = b; c > 0; c--) a.pop();
}(e, c[l]), h.fromCharCode.apply(h, e);
}
@JavaScript-Packer
JavaScript-Packer / test-eval-edit-JavaScript.js
Created July 31, 2015 06:01
Eval for a JavaScript snippet or source codes in a new window (my way to sandbox away from any page). This is the function used for TEST JS button on http://www.whak.ca/packer/JavaScript.htm
function testJS(n, r) {
r = window.open("about:blank", "", "width=999,height=666");
r.document.write('\
<title>WHAK.com</title>\
<body>\
<i>\
<font size="2" color="blue">\
<u title="Click here to demonstrate the code (blue text) used to change this textarea/text box. Might come in handy for replacing a alert or document.write when you want to see results from your script." onclick="test.value=&quot;Hello World&quot;;">\
test.value="Hello World";\
</u>\
@JavaScript-Packer
JavaScript-Packer / how-many-bytes-in-string.js
Last active August 29, 2015 14:26
Pass any string of characters (binary, ascii, Chinese, etc) and it will return the bytes (formatted like 1,000,000) of characters (not just the character count, some characters are more than one byte). Used on http://www.whak.ca/packer/JavaScript.htm
function bytez(n) {
return n = unescape(encodeURI(n)).length, ("" + n).split(/(?=(?:\d{3})+(?:\.|$))/g).join(",");
}
@JavaScript-Packer
JavaScript-Packer / lzw-encoder-compress.js
Created July 31, 2015 05:45
LZW encoder, old compression used in GIF images. Also the function is used on http://www.whak.ca/packer/LZW(HAK).htm
function lzw_encode(u) {
var r, a, n = {}, t = (u + "").split(""), e = [], i = t[0], o = 256;
for (a = 1; a < t.length; a++) r = t[a], null != n[i + r] ? i += r :(e.push(i.length > 1 ? n[i] :i.charCodeAt(0)),
n[i + r] = o, o++, i = r);
for (e.push(i.length > 1 ? n[i] :i.charCodeAt(0)), a = 0; a < e.length; a++) e[a] = String.fromCharCode(e[a]);
return e.join("");
}
@JavaScript-Packer
JavaScript-Packer / base64-decoder-utf.js
Created July 31, 2015 05:20
Base64 decoder with UTF-8 decode of encoded ascii - unBase64 Button function for http://www.whak.ca/packer/LZW(HAK).htm
function a2b(r) {
function utf(r) {
for (var t = "", e = 0, o = c1 = c2 = 0; e < r.length; ) o = r.charCodeAt(e), 128 > o ? (t += String.fromCharCode(o),
e++) :o > 191 && 224 > o ? (c2 = r.charCodeAt(e + 1), t += String.fromCharCode((31 & o) << 6 | 63 & c2),
e += 2) :(c2 = r.charCodeAt(e + 1), c3 = r.charCodeAt(e + 2), t += String.fromCharCode((15 & o) << 12 | (63 & c2) << 6 | 63 & c3),
e += 3);
return t;
}
var t, X = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", e, o, a, h, n = "", c = 0;
for (r = r.replace(/[^A-Za-z0-9\+\/\=]/g, ""); c < r.length; ) t = X.indexOf(r.charAt(c++)),