Skip to content

Instantly share code, notes, and snippets.

@Ruzzz
Created November 16, 2012 19:11
Show Gist options
  • Save Ruzzz/4089993 to your computer and use it in GitHub Desktop.
Save Ruzzz/4089993 to your computer and use it in GitHub Desktop.
Encode DOM to URL
(function () {
// From http://www.webtoolkit.info/javascript-base64.html
var Base64 = {
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// fixed by http://stackoverflow.com/a/6836708
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = 0, c1 = 0, c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c1 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c1 & 63));
i += 2;
}
else {
c1 = utftext.charCodeAt(i+1);
c2 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c1 & 63) << 6) | (c2 & 63));
i += 3;
}
}
return string;
}
};
var tools = document.getElementById('readTools');
if (tools)
tools.parentNode.removeChild(tools);
var s = document.getElementsByTagName('html')[0].outerHTML;
var saveWindow = window.open("data:text/html;charset=utf-8;base64," + Base64.encode(s));
})();
javascript:(function(){var j={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(c){for(var a="",d,b,g,i,h,f,e=0,c=j._utf8_encode(c);e<c.length;)d=c.charCodeAt(e++),b=c.charCodeAt(e++),g=c.charCodeAt(e++),i=d>>2,d=(d&3)<<4|b>>4,h=(b&15)<<2|g>>6,f=g&63,isNaN(b)?h=f=64:isNaN(g)&&(f=64),a=a+this._keyStr.charAt(i)+this._keyStr.charAt(d)+this._keyStr.charAt(h)+this._keyStr.charAt(f);return a},decode:function(c){for(var a="",d,b,g,e,h,f=0,c=c.replace(/[^A-Za-z0-9\+\/\=]/g,
"");f<c.length;)d=this._keyStr.indexOf(c.charAt(f++)),b=this._keyStr.indexOf(c.charAt(f++)),e=this._keyStr.indexOf(c.charAt(f++)),h=this._keyStr.indexOf(c.charAt(f++)),d=d<<2|b>>4,b=(b&15)<<4|e>>2,g=(e&3)<<6|h,a+=String.fromCharCode(d),64!=e&&(a+=String.fromCharCode(b)),64!=h&&(a+=String.fromCharCode(g));return a=j._utf8_decode(a)},_utf8_encode:function(c){for(var c=c.replace(/\r\n/g,"\n"),a="",d=0;d<c.length;d++){var b=c.charCodeAt(d);128>b?a+=String.fromCharCode(b):(127<b&&2048>b?a+=String.fromCharCode(b>>
6|192):(a+=String.fromCharCode(b>>12|224),a+=String.fromCharCode(b>>6&63|128)),a+=String.fromCharCode(b&63|128))}return a},_utf8_decode:function(c){for(var a="",d=0,b=0,e=0,i=0;d<c.length;)b=c.charCodeAt(d),128>b?(a+=String.fromCharCode(b),d++):191<b&&224>b?(e=c.charCodeAt(d+1),a+=String.fromCharCode((b&31)<<6|e&63),d+=2):(e=c.charCodeAt(d+1),i=c.charCodeAt(d+2),a+=String.fromCharCode((b&15)<<12|(e&63)<<6|i&63),d+=3);return a}},e=document.getElementById("readTools");e&&e.parentNode.removeChild(e);
e=document.getElementsByTagName("html")[0].outerHTML;window.open("data:text/html;charset=utf-8;base64,"+j.encode(e))})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment