Add zero-width chars

This commit is contained in:
Arinerron 2017-03-27 21:36:51 -07:00
parent ef0bbd6cd7
commit 7f57f6b9be
2 changed files with 38 additions and 5 deletions

View File

@ -45,6 +45,8 @@
<textarea id='text' placeholder=''></textarea> <textarea id='text' placeholder=''></textarea>
</div><br><br> </div><br><br>
<div class='center'> <div class='center'>
<label>Use Zero-Width Characters <input type='checkbox' id='useZeroWidthCharacters' checked></label>
<br><br>
<label>Use Encryption <input type='checkbox' id='useEncrypt' checked></label> <label>Use Encryption <input type='checkbox' id='useEncrypt' checked></label>
<br><br> <br><br>
<div id='one'><button id='toggle' class='btn btn-primary'>Encode <i class='fa fa-lock'></i></button></div> <div id='one'><button id='toggle' class='btn btn-primary'>Encode <i class='fa fa-lock'></i></button></div>
@ -60,4 +62,4 @@
</div> </div>
<script src='./main.js'></script> <script src='./main.js'></script>
</body> </body>
</html> </html>

39
main.js
View File

@ -1,5 +1,22 @@
var clipboard = new Clipboard('.btn'); var clipboard = new Clipboard('.btn');
var zero = '';
var one = '';
var z_zero = '';
var z_one = '';
var w_zero = ' ';
var w_one = '\t';
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
clipboard.on('success', function(e) { clipboard.on('success', function(e) {
$('#copyFeedback').css('display', 'inherit'); $('#copyFeedback').css('display', 'inherit');
$('#copyFeedback').css('color', 'green'); $('#copyFeedback').css('color', 'green');
@ -46,10 +63,24 @@ $('#toggle').click(function(){
}); });
$('#useZeroWidthCharacters').click(function(){
if (zero == w_zero)
{
zero = z_zero;
one = z_one;
}
else
{
zero = w_zero;
one = z_one;
}
});
/* based on stackoverflow.com/questions/14430633/how-to-convert-text-to-binary-code-in-javascript */ /* based on stackoverflow.com/questions/14430633/how-to-convert-text-to-binary-code-in-javascript */
function binToText(str) { function binToText(str) {
var str = str.replace(/ /g, "1"); var str = replaceAll(replaceAll(str, one, "1"), zero, "0");
var str = str.replace(/\t/g, "0");
if(str.match(/[10]{8}/g)){ if(str.match(/[10]{8}/g)){
var wordFromBinary = str.match(/([10]{8}|\s+)/g).map(function(fromBinary){ var wordFromBinary = str.match(/([10]{8}|\s+)/g).map(function(fromBinary){
return String.fromCharCode(parseInt(fromBinary, 2) ); return String.fromCharCode(parseInt(fromBinary, 2) );
@ -126,7 +157,7 @@ $('#go').click(function(){
} }
// convert result to binary // convert result to binary
output = textToBin(input); output = textToBin(input);
$('#output').val(output.toString().replace(/1/g, " ").replace(/0/g, "\t")); $('#output').val(replaceAll(replaceAll(output.toString(), "1", one), "0", zero));
} }
else else
{ {
@ -145,4 +176,4 @@ $('#go').click(function(){
$('#output').val(output.toString()); $('#output').val(output.toString());
} }
$('#outputModal').modal(); $('#outputModal').modal();
}); });