{"id":3,"date":"2010-05-19T20:42:06","date_gmt":"2010-05-20T03:42:06","guid":{"rendered":"https:\/\/www.alexkorn.com\/blog\/?p=3"},"modified":"2011-05-14T09:59:02","modified_gmt":"2011-05-14T16:59:02","slug":"how-to-recover-deleted-javascript-files-using-the-cache-in-chrome-or-firefo","status":"publish","type":"post","link":"https:\/\/www.alexkorn.com\/blog\/2010\/05\/how-to-recover-deleted-javascript-files-using-the-cache-in-chrome-or-firefo\/","title":{"rendered":"How to recover deleted JavaScript files using the cache in Chrome or Firefox"},"content":{"rendered":"<p>So you&#8217;ve been writing some JavaScript files (or HTML files or images) and testing them in Google Chrome or Mozilla Firefox.  Unfortunately, you&#8217;ve deleted the JavaScript files, forgot to check them into the repository, didn&#8217;t make a backup, and they&#8217;re gone.  Don&#8217;t fear!  There is still hope!<\/p>\n<p>Your missing file is stored in your browser&#8217;s cache, which can be accessed by typing <code>about:cache<\/code> into the URL box in Chrome or <code>about:cache?device=disk<\/code> in Firefox.  This will list links to every file in your cache (and thus may take a moment to open).  Individual files can be accessed by going to <code>chrome:\/\/net-internals\/view-cache\/[[URL]]<\/code> in Chrome or <code>about:cache-entry?client=HTTP&sb=1&key=[[URL]]<\/code> in Firefox.<\/p>\n<p>The cached version will look like the following in Chrome:<\/p>\n<pre>\r\n[[URL]]\r\n<hr>HTTP\/1.1 200 OK\r\nDate: Wed, 19 May 2010 15:17:36 GMT\r\nServer: Apache\r\nLast-Modified: Wed, 19 May 2010 15:13:53 GMT\r\nETag: \"18aa1e6-1761-486f3e7ebae40\"\r\nAccept-Ranges: bytes\r\nContent-Length: 5985\r\nContent-Type: application\/javascript<hr>00000000:  fb  00  00  00  01  00  00  00  34  62  bc  3d  8a  e5  2d  00  ........4b.=..-.\r\n00000010:  ef  1a  be  3d  8a  e5  2d  00  e3  00  00  00  48  54  54  50  ...=..-.....HTTP\r\n00000020:  2f  31  2e  31  20  32  30  30  20  4f  4b  00  44  61  74  65  \/1.1 200 OK.Date\r\n00000030:  3a  20  57  65  64  2c  20  31  39  20  4d  61  79  20  32  30  : Wed, 19 May 20\r\n00000040:  31  30  20  31  35  3a  31  37  3a  33  36  20  47  4d  54  00  10 15:17:36 GMT.\r\n00000050:  53  65  72  76  65  72  3a  20  41  70  61  63  68  65  00  4c  Server: Apache.L\r\n00000060:  61  73  74  2d  4d  6f  64  69  66  69  65  64  3a  20  57  65  ast-Modified: We\r\n00000070:  64  2c  20  31  39  20  4d  61  79  20  32  30  31  30  20  31  d, 19 May 2010 1\r\n00000080:  35  3a  31  33  3a  35  33  20  47  4d  54  00  45  54  61  67  5:13:53 GMT.ETag\r\n00000090:  3a  20  22  31  38  61  61  31  65  36  2d  31  37  36  31  2d  : \"18aa1e6-1761-\r\n000000a0:  34  38  36  66  33  65  37  65  62  61  65  34  30  22  00  41  486f3e7ebae40\".A\r\n000000b0:  63  63  65  70  74  2d  52  61  6e  67  65  73  3a  20  62  79  ccept-Ranges: by\r\n000000c0:  74  65  73  00  43  6f  6e  74  65  6e  74  2d  4c  65  6e  67  tes.Content-Leng\r\n000000d0:  74  68  3a  20  35  39  38  35  00  43  6f  6e  74  65  6e  74  th: 5985.Content\r\n000000e0:  2d  54  79  70  65  3a  20  61  70  70  6c  69  63  61  74  69  -Type: applicati\r\n000000f0:  6f  6e  2f  6a  61  76  61  73  63  72  69  70  74  00  00      on\/javascript..\r\n<hr>00000000:  2f  2a  2a  0a  20  2a  20  40  61  75  74  68  6f  72  20  41  \/**. * @author A\r\n00000010:  6c  65  78  20  4b  6f  72  6e  20  3c  61  6c  65  78  6b  6f  lex Korn &lt;alexko\r\n<div style=\"display:block;text-align:center;\">...<\/div>\r\n<\/pre>\n<p>The last section (in both browsers) displays the hexadecimal representation and the text of your file.  The last column of this section displays newline characters as periods in many text editors, so we can&#8217;t use it to recover the file.  We&#8217;ll have to use the hexadecimal.  I wrote the following PHP script, which will extract and convert the hexadecimal representation into the text version:<\/p>\n<pre>\r\n&lt;?php\r\n$cacheString = ''; \/\/ COPY THE LAST SECTION IN HERE (Use \/'\/\\\\\\'\/g to escape quotation marks)\r\n\r\n$matches = array();\r\npreg_match_all('\/\\s[0-9a-f]{2}\\s\/', $cacheString, $matches);\r\nforeach ($matches[0] as $match)\r\n{\r\n    echo chr(hexdec($match));\r\n}\r\n?&gt;\r\n<\/pre>\n<p>Voil&agrave;!  Your once-lost code is now found!<\/p>\n<hr>\n<p>A couple notes:<\/p>\n<ul>\n<li>If your server has compression enabled (such as gzip), you&#8217;ll have to go through the extra step of unzipping the content<\/li>\n<li>Chrome version: 5.0.375.38 beta<\/li>\n<li>Firefox version: 3.6.3<\/li>\n<\/ul>\n<hr>\n<p>\n<strong>Update 2011\/05\/14<\/strong>: I changed the \\b&#8217;s in the RegEx to \\s&#8217;s as recommended in <a href=\"http:\/\/www.frozax.com\/blog\/2011\/05\/recover-file-google-chrome-cache-gzipped\/\">this post on recovering gzipped files<\/a>.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>How to recover a deleted JavaScript file using Chrome&#8217;s or Firefox&#8217;s cache and some regular expressions.<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5,37,36,7,6],"class_list":["post-3","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-google-chrome","tag-holmes","tag-javascript","tag-mozilla-firefox","tag-php"],"_links":{"self":[{"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/posts\/3","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/comments?post=3"}],"version-history":[{"count":0,"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/posts\/3\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/media?parent=3"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/categories?post=3"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.alexkorn.com\/blog\/wp-json\/wp\/v2\/tags?post=3"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}