Skip to content

Instantly share code, notes, and snippets.

@luisenriquecorona
Created November 2, 2019 01:17
Show Gist options
  • Save luisenriquecorona/5528c505d20379070d5c4ab842b40355 to your computer and use it in GitHub Desktop.
Save luisenriquecorona/5528c505d20379070d5c4ab842b40355 to your computer and use it in GitHub Desktop.
Any data type that can be handled as a string by JavaScript can be sent. Here are functions that will take strings for JavaScript code, CSS styles, and images and convert them into resources the browser can use:
function handleImageData(data, mimeType) {
var img = document.createElement('img');
img.src = 'data:' + mimeType + ';base64,' + data;
return img;
}
function handleCss(data) {
var style = document.createElement('style');
style.type = 'text/css';
var node = document.createTextNode(data);
style.appendChild(node);
document.getElementsByTagName('head')[0].appendChild(style);
}
function handleJavaScript(data) {
eval(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment