Last active
June 8, 2019 08:02
-
-
Save Klustre/b90580a0e3310f80c3133103b504e15b to your computer and use it in GitHub Desktop.
Encode an image for use in ScriptUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Courtesy of @joonaspaakko | |
const picker = document.querySelector('input') | |
picker.oninput = function() { | |
const file = picker.files[0] | |
getBinary( file, function( binary ) { | |
const string = | |
`var dialog= new Window ("dialog"); | |
var image = "${binary}"; | |
dialog.add ("image", undefined, File.decode(image) ); | |
dialog.show();` | |
document.body.innerHTML = string | |
}) | |
} | |
function getBinary( file, callback ) { | |
const reader = new FileReader() | |
reader.readAsBinaryString( file ) | |
reader.onload = function(){ | |
callback( encodeURIComponent( reader.result ) ) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Encode an image for use in ScriptUI</title> | |
</head> | |
<body> | |
<input type="file" /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment