JavaScript Snippet - Specific Base64 Bookmarklet

Posted at

javascript:(function(text, mimetype){
mimetype = null === mimetype ? '' : mimetype; /* normalize variable value to STRING (in-case of 'prompt' error) */

text = btoa( /* ASCII(or byte string) to BASE64 */
unescape(encodeURIComponent( /* trick to make 'btoa' safe for Unicode::: converts any string to byte-string. */
text
))
);

text = mimetype + text;

console.dir( /* non trimmable output */
text
);

}(
document.querySelector('body').innerText
, prompt('mimetype?','data:text/plain;charset=UTF-8;base64,')
));


This one is VERY SPECIFIC to assist ME in some repeating tasks for one of my web-app(s),
taking a text-based file and converting it to base64 is quite-easy, but includes a lot of steps... (you can see the [c#] base64 console, by searching my-website..)

But, by copying the content of a text file (js, html, plain text, etc...) and pasting it to bin.eladkarako.com,
I can run the code above to output the text-only, unescaped-form (unmodified), with full Unicode support, to the console. No need of writing any files :]


I'm using console.dir as an alternative to console.log, since console.log will not trim the output :)



bookmarklet version:
javascript:(function(text,mimetype){mimetype=null===mimetype?"":mimetype;text=btoa(unescape(encodeURIComponent(text)));text=mimetype+text;console.dir(text)})(document.querySelector("body").innerText,prompt("mimetype?","data:text/plain;charset=UTF-8;base64,"));


icompile.eladkarako.com_base64_body_inner_text