diff --git a/index.html b/index.html index 97e80be..1d838ea 100755 --- a/index.html +++ b/index.html @@ -45,6 +45,8 @@

+ +



@@ -60,4 +62,4 @@
- \ No newline at end of file + diff --git a/main.js b/main.js index e6629a5..9a5e6d7 100755 --- a/main.js +++ b/main.js @@ -1,5 +1,22 @@ 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) { $('#copyFeedback').css('display', 'inherit'); $('#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 */ function binToText(str) { - var str = str.replace(/ /g, "1"); - var str = str.replace(/\t/g, "0"); + var str = replaceAll(replaceAll(str, one, "1"), zero, "0"); if(str.match(/[10]{8}/g)){ var wordFromBinary = str.match(/([10]{8}|\s+)/g).map(function(fromBinary){ return String.fromCharCode(parseInt(fromBinary, 2) ); @@ -126,7 +157,7 @@ $('#go').click(function(){ } // convert result to binary 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 { @@ -145,4 +176,4 @@ $('#go').click(function(){ $('#output').val(output.toString()); } $('#outputModal').modal(); -}); \ No newline at end of file +});