Skip to content

Instantly share code, notes, and snippets.

@telecran-telecrit
Last active February 25, 2024 21:40
Show Gist options
  • Save telecran-telecrit/0e26b2a48e057f91f09fb031d6fae4f5 to your computer and use it in GitHub Desktop.
Save telecran-telecrit/0e26b2a48e057f91f09fb031d6fae4f5 to your computer and use it in GitHub Desktop.
Save rendered html page to clipboard
// Instead of unrendered content (Ctrl+U, Ctrl+A, Ctrl+C)
// See also https://gist.github.com/telecran-telecrit/4a392087d5cc241404c83bb32009a25f (imageToUri)
///////////////////////////////////////////////////////////////////
function isScriptExternal (scriptElement, oldSrc) {
if (!oldSrc) {
oldSrc = scriptElement.src;
}
return (!!oldSrc && (oldSrc[0] != '#' || oldSrc.startsWith('#external/')));
}
function getExtrnalContent (url) {
var IE=function(){var ret,isTheBrowser,actualVersion,jscriptMap,jscriptVersion;isTheBrowser=false;jscriptMap={"5.5":5.5,"5.6":6,"5.7":7,"5.8":8,"9":9,"10":10};
var jscriptVersion/*@cc_on=@_jscript_version@*/;if(jscriptVersion!==undefined){isTheBrowser=true;actualVersion=jscriptMap[jscriptVersion]}else if(/msie/.test(navigator.userAgent.toLowerCase())){isTheBrowser=true;actualVersion=11}else{isTheBrowser=false}ret={isTheBrowser:isTheBrowser,actualVersion:actualVersion};return ret}();
var b64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",b64re=/^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/,Base64={_keyStr:b64,encode:function r(e){var t,o,a,h,d,c,n,f="",C=0;for(e=Base64._utf8_encode(e);C<e.length;)t=e.charCodeAt(C++),o=e.charCodeAt(C++),a=e.charCodeAt(C++),h=t>>2,d=(3&t)<<4|o>>4,c=(15&o)<<2|a>>6,n=63&a,isNaN(o)?c=n=64:isNaN(a)&&(n=64),f=f+this._keyStr.charAt(h)+this._keyStr.charAt(d)+this._keyStr.charAt(c)+this._keyStr.charAt(n);return f},decode:function r(e){var t,o,a,h,d,c,n,f="",C=0;for(e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");C<e.length;)h=this._keyStr.indexOf(e.charAt(C++)),d=this._keyStr.indexOf(e.charAt(C++)),c=this._keyStr.indexOf(e.charAt(C++)),n=this._keyStr.indexOf(e.charAt(C++)),t=h<<2|d>>4,o=(15&d)<<4|c>>2,a=(3&c)<<6|n,f+=String.fromCharCode(t),64!=c&&(f+=String.fromCharCode(o)),64!=n&&(f+=String.fromCharCode(a));return Base64._utf8_decode(f)},_utf8_encode:function r(e){e=e.replace(/\r\n/g,"\n");for(var t="",o=0;o<e.length;o++){var a=e.charCodeAt(o);a<128?t+=String.fromCharCode(a):a>127&&a<2048?(t+=String.fromCharCode(a>>6|192),t+=String.fromCharCode(63&a|128)):(t+=String.fromCharCode(a>>12|224),t+=String.fromCharCode(a>>6&63|128),t+=String.fromCharCode(63&a|128))}return t},_utf8_decode:function r(e){for(var t="",o=0,a=c1=c2=0;o<e.length;)(a=e.charCodeAt(o))<128?(t+=String.fromCharCode(a),o++):a>191&&a<224?(t+=String.fromCharCode((31&a)<<6|63&(c2=e.charCodeAt(o+1))),o+=2):(t+=String.fromCharCode((15&a)<<12|(63&(c2=e.charCodeAt(o+1)))<<6|63&(c3=e.charCodeAt(o+2))),o+=3);return t},btoa:function r(e){e=String(e);for(var t,o,a,h,d="",c=0,n=e.length%3;c<e.length;){if((o=e.charCodeAt(c++))>255||(a=e.charCodeAt(c++))>255||(h=e.charCodeAt(c++))>255)throw TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");t=o<<16|a<<8|h,d+=b64.charAt(t>>18&63)+b64.charAt(t>>12&63)+b64.charAt(t>>6&63)+b64.charAt(63&t)}return n?d.slice(0,n-3)+"===".substring(n):d},atob:function r(e){if(e=String(e).replace(/[\t\n\f\r ]+/g,""),!b64re.test(e))throw TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");e+="==".slice(2-(3&e.length));for(var t,o,a,h="",d=0;d<e.length;)t=b64.indexOf(e.charAt(d++))<<18|b64.indexOf(e.charAt(d++))<<12|(o=b64.indexOf(e.charAt(d++)))<<6|(a=b64.indexOf(e.charAt(d++))),h+=64===o?String.fromCharCode(t>>16&255):64===a?String.fromCharCode(t>>16&255,t>>8&255):String.fromCharCode(t>>16&255,t>>8&255,255&t);return h}};
var xmlHTTP = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
if (window.location.href.startsWith('https:') && url.startsWith('http:')) {
url = 'https:' + (url.substring('https:'.length));
}
xmlHTTP.open('GET', url, false); // sync!
xmlHTTP.send(null);
if (xmlHTTP.status !== 200) {
throw new Error('Error loading by ajax: ' + url);
}
return '' + xmlHTTP.response;
}
function getInnerContent (scriptElement) {
if (!!scriptElement.innerHTML) {
return '' + scriptElement.innerHTML;
} else {
return '';
}
}
function setInnerContent (scriptElement, newContent) {
scriptElement.type = 'text/javascript';
scriptElement.innerHTML = newContent;
}
function scriptToInner (scriptElement) {
console.log('!scriptToInner:');
if (!scriptElement || !scriptElement.tagName || 'SCRIPT' !== scriptElement.tagName.toUpperCase()) {
console.warn('It is not script!');
return;
}
var oldSrc = scriptElement.src;
if (!!oldSrc && oldSrc[0] === '#') {
console.warn('Avoid repeat: ' + oldSrc);
return;
}
scriptElement.removeAttribute('src');
if (!isScriptExternal(scriptElement, oldSrc)) {
setInnerContent(scriptElement, '/*Patched inner:*/\n' + getInnerContent(scriptElement));
} else {
try {
scriptElement.src = '#external/' + oldSrc;
setInnerContent(scriptElement, '/*Patched external: "' + oldSrc + '"*/\n' + getExtrnalContent(oldSrc));
} catch (e5) {
setInnerContent(scriptElement, '/*!Patch error*/');
scriptElement.src = oldSrc;
}
}
}
let scripts = document.querySelectorAll('script');
for (let script of scripts) {
scriptToInner(script);
}
///////////////////////////////////////////////////////////////////
function stringifyStyleSheet (stylesheet) { // it ignores media
try {
if (!stylesheet || !stylesheet.cssRules) {
return '';
}
return Array.from(stylesheet.cssRules)
.map(rule => rule.cssText || "")
.join("\n")
} catch (e46634543) {
console.error(e46634543);
return '';
}
}
function getOldStyles () {
let res = [];
let i = 0;
for (let style of document.styleSheets) {
res[''+i+'#'+(style.href)] = stringifyStyleSheet(style);
++i;
}
return res;
}
function removeStyleElements () {
document.querySelectorAll('link[rel="stylesheet"]')
.forEach(el => el.parentNode.removeChild(el));
document.querySelectorAll('style')
.forEach(el => el.parentNode.removeChild(el));
}
function appendNewStyleelements (stylesArray, container) {
for (let i in stylesArray) {
container.insertAdjacentHTML("beforeend", '<style>/*' + (i.split('#')[1]) + '*/\n' + stylesArray[i] + '\n</style>');
}
}
function makeStylesInner () {
var oldStyles = getOldStyles();
removeStyleElements();
appendNewStyleelements(oldStyles, document.head);
}
makeStylesInner();
///////////////////////////////////////////////////////////////////
function preloadImages () {
imgs = document.getElementsByTagName("img");
for (let i=0, l=imgs.length; i<l; i++) {
imageToUri(imgs[i].src, function next (uri) {
imgs[i].src = uri;
});
}
}
function copyElement (element) {
try {
const htmlCode = element.innerHTML
const blobInput = new Blob([htmlCode], { type: 'text/plain' })
navigator.clipboard.write([new ClipboardItem({ 'text/plain': blobInput })])
console.log('Copied')
} catch (e) {
alert(e)
}
}
preloadImages();
setTimeout(function () {
copyElement(document.body);
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment