Skip to content

Instantly share code, notes, and snippets.

@daniellizik-sc
Created June 15, 2016 19:46
Show Gist options
  • Save daniellizik-sc/66a88d9959f2c3ba8d529db36aff1858 to your computer and use it in GitHub Desktop.
Save daniellizik-sc/66a88d9959f2c3ba8d529db36aff1858 to your computer and use it in GitHub Desktop.
Save a static html page with relative resource links converted
javascript:"use strict";!function(t,e,n){var o=function(t){if(!("undefined"==typeof t||"undefined"!=typeof navigator&&/MSIE [1-9]\./.test(navigator.userAgent))){var e=t.document,n=function(){return t.URL||t.webkitURL||t},o=e.createElementNS("http://www.w3.org/1999/xhtml","a"),r="download"in o,i=function(t){var e=new MouseEvent("click");t.dispatchEvent(e)},a=/constructor/i.test(t.HTMLElement),c=function(e){(t.setImmediate||t.setTimeout)(function(){throw e},0)},u="application/octet-stream",l=4e4,d=function(t){var e=function(){"string"==typeof t?n().revokeObjectURL(t):t.remove()};setTimeout(e,l)},f=function(t,e,n){e=[].concat(e);for(var o=e.length;o--;){var r=t["on"+e[o]];if("function"==typeof r)try{r.c$(t,n||t)}catch(i){c(i)}}},s=function(t){return/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(t.type)?new Blob([String.fromCharCode(65279),t],{type:t.type}):t},p=function h(e,c,l){l||(e=s(e));var p,h=this,w=e.type,m=w===u,v=function(){f(h,"writestart progress write writeend".split(" "))},y=function(){if(m&&a&&t.FileReader){var o=new FileReader;return o.onloadend=function(){var e=o.result;t.location.href="data:attachment/file"+e.slice(e.search(/[,;]/)),h.readyState=h.DONE,v()},o.readAsDataURL(e),void(h.readyState=h.INIT)}if(p||(p=n().createObjectURL(e)),m)t.location.href=p;else{var r=t.open(p,"_blank");r||(t.location.href=p)}h.readyState=h.DONE,v(),d(p)};return h.readyState=h.INIT,r?(p=n().createObjectURL(e),void setTimeout(function(){o.href=p,o.download=c,i(o),v(),d(p),h.readyState=h.DONE})):void y()},w=p.prototype,m=function(t,e,n){return new p(t,e||t.name||"download",n)};return"undefined"!=typeof navigator&&navigator.msSaveOrOpenBlob?function(t,e,n){return e=e||t.name||"download",n||(t=s(t)),navigator.msSaveOrOpenBlob(t,e)}:(w.abort=function(){},w.readyState=w.INIT=0,w.WRITING=1,w.DONE=2,w.error=w.onwritestart=w.onprogress=w.onwrite=w.onabort=w.onerror=w.onwriteend=null,m)}}("undefined"!=typeof self&&self||"undefined"!=typeof window&&window||(void 0).content);"undefined"!=typeof module&&module.exports?module.exports.saveAs=o:"undefined"!=typeof define&&null!==define&&null!==define.amd&&define([],function(){return o});var r=t();e(r,"script").forEach(function(t){return t.parentNode.removeChild(t)}),e(r,"link").forEach(function(t){return n(t,"href")}),e(r,"img").forEach(function(t){return n(t,"src")});var i="<!doctype HTML>"+r.outerHTML,a="data:text/html;charset=UTF-8;base64,",c=btoa(unescape(encodeURIComponent(i))),u="\n Enter number corresponding to desired option:\n\n 1. Download as html file\n 2. Download as encoded base64 text file\n 3. Open as base64 data url in new tab (must allow popups on this page)\n ",l=prompt(u),d={1:function(){return o(new Blob([i],{type:"text/html;charset=utf-8"}),window.location.host+".html")},2:function(){return o(new Blob([c],{type:"text/plain"}),window.location.host+".txt")},3:function(){return window.open(a+c)}};null!==l&&l!==!1&&void 0!==l&&d[l]()}(function(){var t=(document.createDocumentFragment(),document.createElement("html"));return t.innerHTML=document.documentElement.innerHTML,t},function(t,e){return[].slice.call(t.querySelectorAll(e))},function(t,e){var n=t.getAttribute(e).trim();/\/\/|~\//.test(n.slice(0,2))?n=window.location.protocol+"//"+n.slice(2):"/"===n.charAt(0)?n=window.location.origin+"/"+n.slice(1):"http"!==n.slice(0,4)&&(n=window.location.origin+"/"+n);var o=(window.location.host.split("/"),n.split("/")),r=o.slice(3),i=o[0]+"//"+o[2]+"/"+r.filter(function(t){return""!==t&&".."!==t}).join("/");t.setAttribute(e,i)});
(($clone, $, $modifyPath) => {
'use strict';
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
const saveAs=function(e){"use strict";if(typeof e==="undefined"||typeof navigator!=="undefined"&&/MSIE [1-9]\./.test(navigator.userAgent)){return}var t=e.document,n=function(){return e.URL||e.webkitURL||e},r=t.createElementNS("http://www.w3.org/1999/xhtml","a"),o="download"in r,i=function(e){var t=new MouseEvent("click");e.dispatchEvent(t)},a=/constructor/i.test(e.HTMLElement),f=function(t){(e.setImmediate||e.setTimeout)(function(){throw t},0)},u="application/octet-stream",s=1e3*40,d=function(e){var t=function(){if(typeof e==="string"){n().revokeObjectURL(e)}else{e.remove()}};setTimeout(t,s)},c=function(e,t,n){t=[].concat(t);var r=t.length;while(r--){var o=e["on"+t[r]];if(typeof o==="function"){try{o.c$(e,n||e)}catch(i){f(i)}}}},l=function(e){if(/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)){return new Blob([String.fromCharCode(65279),e],{type:e.type})}return e},p=function(t,f,s){if(!s){t=l(t)}var p=this,v=t.type,w=v===u,m,y=function(){c(p,"writestart progress write writeend".split(" "))},h=function(){if(w&&a&&e.FileReader){var r=new FileReader;r.onloadend=function(){var t=r.result;e.location.href="data:attachment/file"+t.slice(t.search(/[,;]/));p.readyState=p.DONE;y()};r.readAsDataURL(t);p.readyState=p.INIT;return}if(!m){m=n().createObjectURL(t)}if(w){e.location.href=m}else{var o=e.open(m,"_blank");if(!o){e.location.href=m}}p.readyState=p.DONE;y();d(m)};p.readyState=p.INIT;if(o){m=n().createObjectURL(t);setTimeout(function(){r.href=m;r.download=f;i(r);y();d(m);p.readyState=p.DONE});return}h()},v=p.prototype,w=function(e,t,n){return new p(e,t||e.name||"download",n)};if(typeof navigator!=="undefined"&&navigator.msSaveOrOpenBlob){return function(e,t,n){t=t||e.name||"download";if(!n){e=l(e)}return navigator.msSaveOrOpenBlob(e,t)}}v.abort=function(){};v.readyState=v.INIT=0;v.WRITING=1;v.DONE=2;v.error=v.onwritestart=v.onprogress=v.onwrite=v.onabort=v.onerror=v.onwriteend=null;return w}(typeof self!=="undefined"&&self||typeof window!=="undefined"&&window||this.content);if(typeof module!=="undefined"&&module.exports){module.exports.saveAs=saveAs}else if(typeof define!=="undefined"&&define!==null&&define.amd!==null){define([],function(){return saveAs})}
const clone = $clone();
$(clone, 'script').forEach(el => el.parentNode.removeChild(el));
$(clone, 'link').forEach(el => $modifyPath(el, 'href'));
$(clone, 'img').forEach(el => $modifyPath(el, 'src'));
const html = '<!doctype HTML>' + clone.outerHTML;
const prefix = 'data:text/html;charset=UTF-8;base64,';
const b64 = btoa(unescape(encodeURIComponent(html)));
const ask = `
Enter number corresponding to desired option:
1. Download as html file
2. Download as encoded base64 text file
3. Open as base64 data url in new tab (must allow popups on this page)
`;
const option = prompt(ask);
const options = {
'1': () => saveAs(new Blob([html], {type: 'text/html;charset=utf-8'}), window.location.host + '.html'),
'2': () => saveAs(new Blob([b64], {type: 'text/plain'}), window.location.host + '.txt'),
'3': () => window.open(prefix + b64)
};
if (option !== null && option !== false && option !== undefined) {
options[option]();
}
})(
() => {
let clone = document.createDocumentFragment();
let html = document.createElement('html');
html.innerHTML = document.documentElement.innerHTML;
return html;
},
(doc, sel) => [].slice.call(doc.querySelectorAll(sel)),
(el, attr) => {
var resource = el.getAttribute(attr).trim();
if (/\/\/|~\//.test(resource.slice(0, 2))) {
resource = window.location.protocol + '//' + resource.slice(2);
} else if (resource.charAt(0) === '/') {
resource = window.location.origin + '/' + resource.slice(1);
} else if (resource.slice(0, 4) !== 'http') {
resource = window.location.origin + '/' + resource;
}
var stack = window.location.host.split('/');
var split = resource.split('/');
var parts = split.slice(3);
var clean = split[0] + '//' + split[2] + '/' + parts.filter(p => p !== '' && p !== '..').join('/');
el.setAttribute(attr, clean);
}
);
"use strict";!function(t,e,n){var o=function(t){if(!("undefined"==typeof t||"undefined"!=typeof navigator&&/MSIE [1-9]\./.test(navigator.userAgent))){var e=t.document,n=function(){return t.URL||t.webkitURL||t},o=e.createElementNS("http://www.w3.org/1999/xhtml","a"),r="download"in o,i=function(t){var e=new MouseEvent("click");t.dispatchEvent(e)},a=/constructor/i.test(t.HTMLElement),c=function(e){(t.setImmediate||t.setTimeout)(function(){throw e},0)},u="application/octet-stream",l=4e4,d=function(t){var e=function(){"string"==typeof t?n().revokeObjectURL(t):t.remove()};setTimeout(e,l)},f=function(t,e,n){e=[].concat(e);for(var o=e.length;o--;){var r=t["on"+e[o]];if("function"==typeof r)try{r.c$(t,n||t)}catch(i){c(i)}}},s=function(t){return/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(t.type)?new Blob([String.fromCharCode(65279),t],{type:t.type}):t},p=function h(e,c,l){l||(e=s(e));var p,h=this,w=e.type,m=w===u,v=function(){f(h,"writestart progress write writeend".split(" "))},y=function(){if(m&&a&&t.FileReader){var o=new FileReader;return o.onloadend=function(){var e=o.result;t.location.href="data:attachment/file"+e.slice(e.search(/[,;]/)),h.readyState=h.DONE,v()},o.readAsDataURL(e),void(h.readyState=h.INIT)}if(p||(p=n().createObjectURL(e)),m)t.location.href=p;else{var r=t.open(p,"_blank");r||(t.location.href=p)}h.readyState=h.DONE,v(),d(p)};return h.readyState=h.INIT,r?(p=n().createObjectURL(e),void setTimeout(function(){o.href=p,o.download=c,i(o),v(),d(p),h.readyState=h.DONE})):void y()},w=p.prototype,m=function(t,e,n){return new p(t,e||t.name||"download",n)};return"undefined"!=typeof navigator&&navigator.msSaveOrOpenBlob?function(t,e,n){return e=e||t.name||"download",n||(t=s(t)),navigator.msSaveOrOpenBlob(t,e)}:(w.abort=function(){},w.readyState=w.INIT=0,w.WRITING=1,w.DONE=2,w.error=w.onwritestart=w.onprogress=w.onwrite=w.onabort=w.onerror=w.onwriteend=null,m)}}("undefined"!=typeof self&&self||"undefined"!=typeof window&&window||(void 0).content);"undefined"!=typeof module&&module.exports?module.exports.saveAs=o:"undefined"!=typeof define&&null!==define&&null!==define.amd&&define([],function(){return o});var r=t();e(r,"script").forEach(function(t){return t.parentNode.removeChild(t)}),e(r,"link").forEach(function(t){return n(t,"href")}),e(r,"img").forEach(function(t){return n(t,"src")});var i="<!doctype HTML>"+r.outerHTML,a="data:text/html;charset=UTF-8;base64,",c=btoa(unescape(encodeURIComponent(i))),u="\n Enter number corresponding to desired option:\n\n 1. Download as html file\n 2. Download as encoded base64 text file\n 3. Open as base64 data url in new tab (must allow popups on this page)\n ",l=prompt(u),d={1:function(){return o(new Blob([i],{type:"text/html;charset=utf-8"}),window.location.host+".html")},2:function(){return o(new Blob([c],{type:"text/plain"}),window.location.host+".txt")},3:function(){return window.open(a+c)}};null!==l&&l!==!1&&void 0!==l&&d[l]()}(function(){var t=(document.createDocumentFragment(),document.createElement("html"));return t.innerHTML=document.documentElement.innerHTML,t},function(t,e){return[].slice.call(t.querySelectorAll(e))},function(t,e){var n=t.getAttribute(e).trim();/\/\/|~\//.test(n.slice(0,2))?n=window.location.protocol+"//"+n.slice(2):"/"===n.charAt(0)?n=window.location.origin+"/"+n.slice(1):"http"!==n.slice(0,4)&&(n=window.location.origin+"/"+n);var o=(window.location.host.split("/"),n.split("/")),r=o.slice(3),i=o[0]+"//"+o[2]+"/"+r.filter(function(t){return""!==t&&".."!==t}).join("/");t.setAttribute(e,i)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment