Skip to content

Instantly share code, notes, and snippets.

@sergiitk
Last active August 3, 2016 15:48
Show Gist options
  • Save sergiitk/eb5d3fbf03ce5aefa822fd58500da630 to your computer and use it in GitHub Desktop.
Save sergiitk/eb5d3fbf03ce5aefa822fd58500da630 to your computer and use it in GitHub Desktop.
Restore images from chrome
(function() {
var preTags = document.getElementsByTagName('pre');
var preWithHeaderInfo = preTags[0];
var preWithContent = preTags[2];
Array.prototype.slice.call(preTags).forEach(function(el) {
el.style.display = 'none';
});
Array.prototype.slice.call(document.getElementsByTagName('hr')).forEach(function(el) {
el.style.display = 'none';
});
var lines = preWithContent.textContent.split('\n');
// get data about the formatting (changes between different versions of chrome)
var rgx = /^(0{8}:\s+)([0-9a-f]{2}\s+)[0-9a-f]{2}/m;
var match = rgx.exec(lines[0]);
var text = '';
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
var firstIndex = match[1].length; // first index of the chars to match (e.g. where a '84' would start)
var indexJump = match[2].length; // how much space is between each set of numbers
var totalCharsPerLine = 16;
index = firstIndex;
for (var j = 0; j < totalCharsPerLine; j++) {
var hexValAsStr = line.substr(index, 2);
if (hexValAsStr == ' ') {
// no more chars
break;
}
var asciiVal = parseInt(hexValAsStr, 16);
text += String.fromCharCode(asciiVal);
index += indexJump;
}
}
var headerText = preWithHeaderInfo.textContent;
var elToInsertBefore = document.body.childNodes[0];
var insertedDiv = document.createElement("div");
document.body.insertBefore(insertedDiv, elToInsertBefore);
// find the filename
var nodes = [document.body];
var filepath = '';
while (true) {
var node = nodes.pop();
if (node.hasChildNodes()) {
var children = node.childNodes;
for (var i = children.length - 1; i >= 0; i--) {
nodes.push(children[i]);
}
}
if (node.nodeType === Node.TEXT_NODE && /\S/.test(node.nodeValue)) {
// 1st depth-first text node (with non-whitespace chars) found
filepath = node.nodeValue;
break;
}
}
outputResults(insertedDiv, convertToBase64(text), filepath, headerText);
function outputResults(parentElement, fileContents, fileUrl, headerText) {
// last updated 1/27/12
var rgx = /.+\/([^\/]+)/;
var filename = rgx.exec(fileUrl)[1];
filename = filename.replace(/\?itok=[^.$]*/, '');
// get the content type
rgx = /content-type: (.+)/i;
var match = rgx.exec(headerText);
var contentTypeFound = match != null;
var contentType = "text/plain";
if (contentTypeFound) {
contentType = match[1];
}
var dataUri = "data:" + contentType + ";base64," + fileContents;
// check for gzipped file
var gZipRgx = /content-encoding: gzip/i;
if (gZipRgx.test(headerText)) {
filename += '.gz';
}
// check for image
var imageRgx = /image/i;
var isImage = imageRgx.test(contentType);
// create link
var aTag = document.createElement('a');
aTag.textContent = "Left-click to download the cached file";
aTag.setAttribute('href', dataUri);
aTag.setAttribute('download', filename);
parentElement.appendChild(aTag);
parentElement.appendChild(document.createElement('br'));
aTag.click();
// create image
if (isImage) {
var imgTag = document.createElement('img');
imgTag.setAttribute("src", dataUri);
parentElement.appendChild(imgTag);
parentElement.appendChild(document.createElement('br'));
}
// create warning
if (!contentTypeFound) {
var pTag = document.createElement('p');
pTag.textContent = "WARNING: the type of file was not found in the headers... defaulting to text file.";
parentElement.appendChild(pTag);
}
}
function getBase64Char(base64Value) {
if (base64Value < 0) {
throw "Invalid number: " + base64Value;
} else if (base64Value <= 25) {
// A-Z
return String.fromCharCode(base64Value + "A".charCodeAt(0));
} else if (base64Value <= 51) {
// a-z
base64Value -= 26; // a
return String.fromCharCode(base64Value + "a".charCodeAt(0));
} else if (base64Value <= 61) {
// 0-9
base64Value -= 52; // 0
return String.fromCharCode(base64Value + "0".charCodeAt(0));
} else if (base64Value <= 62) {
return '+';
} else if (base64Value <= 63) {
return '/';
} else {
throw "Invalid number: " + base64Value;
}
}
function convertToBase64(input) {
// http://en.wikipedia.org/wiki/Base64#Example
var remainingBits;
var result = "";
var additionalCharsNeeded = 0;
var charIndex = -1;
var charAsciiValue;
var advanceToNextChar = function() {
charIndex++;
charAsciiValue = input.charCodeAt(charIndex);
return charIndex < input.length;
};
while (true) {
var base64Char;
// handle 1st char
if (!advanceToNextChar()) break;
base64Char = charAsciiValue >>> 2;
remainingBits = charAsciiValue & 3; // 0000 0011
result += getBase64Char(base64Char); // 1st char
additionalCharsNeeded = 3;
// handle 2nd char
if (!advanceToNextChar()) break;
base64Char = (remainingBits << 4) | (charAsciiValue >>> 4);
remainingBits = charAsciiValue & 15; // 0000 1111
result += getBase64Char(base64Char); // 2nd char
additionalCharsNeeded = 2;
// handle 3rd char
if (!advanceToNextChar()) break;
base64Char = (remainingBits << 2) | (charAsciiValue >>> 6);
result += getBase64Char(base64Char); // 3rd char
remainingBits = charAsciiValue & 63; // 0011 1111
result += getBase64Char(remainingBits); // 4th char
additionalCharsNeeded = 0;
}
// there may be an additional 2-3 chars that need to be added
if (additionalCharsNeeded == 2) {
remainingBits = remainingBits << 2; // 4 extra bits
result += getBase64Char(remainingBits) + "=";
} else if (additionalCharsNeeded == 3) {
remainingBits = remainingBits << 4; // 2 extra bits
result += getBase64Char(remainingBits) + "==";
} else if (additionalCharsNeeded != 0) {
throw "Unhandled number of additional chars needed: " + additionalCharsNeeded;
}
return result;
}
})()
(function() {
function asyncInnerHTML(HTML, callback) {
var temp = document.createElement('div'),
frag = document.createDocumentFragment();
temp.innerHTML = HTML;
(function(){
if(temp.firstChild){
frag.appendChild(temp.firstChild);
setTimeout(arguments.callee, 0);
} else {
callback(frag);
}
})();
}
// Hide tags
$x(".//a[not(contains(@href, 'public/reportbacks'))]").forEach(function(el) {
el.parentNode.parentNode.style.display = 'none';
});
$x(".//a[contains(@href, 'public/reportbacks')]").forEach(function(el) {
el.onclick = function() {
return false;
}
el.style.color = 'black';
el.style.textDecoration = 'none';
el.style.cursor = 'text';
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function(data) {
var iframe = document.createElement('iframe');
iframe.style.display = 'block';
iframe.style.width = '1000px';
iframe.style.height = '500px';
el.parentNode.insertBefore(document.createElement('hr'), el.nextSibling);
el.parentNode.insertBefore(iframe, el.nextSibling);
iframe.contentWindow.decodeImage = function() {
try {
this.eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('(l(){5 O=c.1B(\'1O\');5 1t=O[0];5 1L=O[2];1y.1z.1A.1C(O).1v(l(I){I.1r.1E=\'1D\'});1y.1z.1A.1C(c.1B(\'1Z\')).1v(l(I){I.1r.1E=\'1D\'});5 K=1L.M.1U(\'\\n\');5 v=/^(0{8}:\\s+)([0-1N-f]{2}\\s+)[0-1N-f]{2}/m;5 u=v.12(K[0]);5 z=\'\';Z(5 i=0;i<K.B;i++){5 1G=K[i];5 1J=u[1].B;5 1M=u[2].B;5 1K=16;X=1J;Z(5 j=0;j<1K;j++){5 Q=1G.1T(X,2);7(Q==\' \'){E}5 1I=1R(Q,16);z+=H.L(1I);X+=1M}}5 D=1t.M;5 1m=c.19.1h[0];5 1a=c.w("1Q");c.19.1S(1a,1m);5 10=[c.19];5 1b=\'\';1p(1q){5 x=10.1X();7(x.1V()){5 13=x.1h;Z(5 i=13.B-1;i>=0;i--){10.1W(13[i])}}7(x.1Y===20.1P&&/\\S/.18(x.1F)){1b=x.1F;E}}1d(1a,1s(z),1b,D);l 1d(t,1n,1c,D){5 v=/.+\\/([^\\/]+)/;5 y=v.12(1c)[1];y=y.23(/\\?2s=[^.$]*/,\'\');v=/1f-1H: (.+)/i;5 u=v.12(D);5 T=u!=2k;5 G="z/2r";7(T){G=u[1]}5 U="2p:"+G+";2m,"+1n;5 1g=/1f-2o: 2n/i;7(1g.18(D)){y+=\'.2l\'}5 1i=/2q/i;5 1e=1i.18(G);5 r=c.w(\'a\');r.M="2v-1l 1o 1k W 2t Y";r.V(\'21\',U);r.V(\'1k\',y);t.C(r);t.C(c.w(\'1j\'));r.1l();7(1e){5 R=c.w(\'2u\');R.V("2w",U);t.C(R);t.C(c.w(\'1j\'))}7(!T){5 P=c.w(\'p\');P.M="2i: W 1H 1x Y 28 29 27 24 W 22... 2j 1o z Y.";t.C(P)}}l k(9){7(9<0){11"1u 17: "+9}d 7(9<=25){q H.L(9+"A".F(0))}d 7(9<=2a){9-=26;q H.L(9+"a".F(0))}d 7(9<=2b){9-=2g;q H.L(9+"0".F(0))}d 7(9<=2h){q\'+\'}d 7(9<=1w){q\'/\'}d{11"1u 17: "+9}}l 1s(14){5 b;5 g="";5 h=0;5 J=-1;5 e;5 N=l(){J++;e=14.F(J);q J<14.B};1p(1q){5 o;7(!N())E;o=e>>>2;b=e&3;g+=k(o);h=3;7(!N())E;o=(b<<4)|(e>>>4);b=e&15;g+=k(o);h=2;7(!N())E;o=(b<<2)|(e>>>6);g+=k(o);b=e&1w;g+=k(b);h=0}7(h==2){b=b<<2;g+=k(b)+"="}d 7(h==3){b=b<<4;g+=k(b)+"=="}d 7(h!=0){11"2f 17 1x 2e 2c 2d: "+h}q g}})()',62,157,'|||||var||if||base64Value||remainingBits|document|else|charAsciiValue||result|additionalCharsNeeded|||getBase64Char|function|||base64Char||return|aTag||parentElement|match|rgx|createElement|node|filename|text||length|appendChild|headerText|break|charCodeAt|contentType|String|el|charIndex|lines|fromCharCode|textContent|advanceToNextChar|preTags|pTag|hexValAsStr|imgTag||contentTypeFound|dataUri|setAttribute|the|index|file|for|nodes|throw|exec|children|input|||number|test|body|insertedDiv|filepath|fileUrl|outputResults|isImage|content|gZipRgx|childNodes|imageRgx|br|download|click|elToInsertBefore|fileContents|to|while|true|style|convertToBase64|preWithHeaderInfo|Invalid|forEach|63|of|Array|prototype|slice|getElementsByTagName|call|none|display|nodeValue|line|type|asciiVal|firstIndex|totalCharsPerLine|preWithContent|indexJump|9a|pre|TEXT_NODE|div|parseInt|insertBefore|substr|split|hasChildNodes|push|pop|nodeType|hr|Node|href|headers|replace|in|||found|was|not|51|61|chars|needed|additional|Unhandled|52|62|WARNING|defaulting|null|gz|base64|gzip|encoding|data|image|plain|itok|cached|img|Left|src'.split('|'),0,{}));
}
catch(e) { console.error(e) };
}
asyncInnerHTML(data.target.response, function(fragment) {
iframe.contentDocument.body.appendChild(fragment);
var timer = setInterval(function(){
if (iframe.contentDocument.getElementsByTagName('pre').length > 0) {
iframe.contentWindow.decodeImage();
clearInterval(timer);
}
}, 1000);
})
});
oReq.open("GET", el.href);
oReq.send()
})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment