Merge pull request #2 from Arinerron/patch-2
Add optional zero-width chars
This commit is contained in:
commit
8a9564d417
@ -45,6 +45,8 @@
|
||||
<textarea id='text' placeholder=''></textarea>
|
||||
</div><br><br>
|
||||
<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>
|
||||
<br><br>
|
||||
<div id='one'><button id='toggle' class='btn btn-primary'>Encode <i class='fa fa-lock'></i></button></div>
|
||||
|
40
main.js
40
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) );
|
||||
@ -125,8 +156,9 @@ $('#go').click(function(){
|
||||
}
|
||||
}
|
||||
// convert result to binary
|
||||
output = textToBin(btoa(input));
|
||||
$('#output').val(output.toString().replace(/1/g, " ").replace(/0/g, "\t"));
|
||||
|
||||
output = textToBin(input);
|
||||
$('#output').val(replaceAll(replaceAll(output.toString(), "1", one), "0", zero));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user