@@ -6,17 +6,17 @@ const words_replace = ["enter", "imes", "ai", "ober", "ufat"];
66
77const verifyCharacters = ( text ) => {
88 /* Regex que permite solo letras minusculas, sin acentos, sin caracteres especiales */
9- const regex = / ^ [ a - z \s ] * $ /
10- return regex . test ( text ) ;
11- }
9+ const regex = / ^ [ a - z \s ] * $ / ;
10+ return text && regex . test ( text ) ;
11+ } ;
1212
1313const encrypText = ( text ) => {
1414 for ( let i = 0 ; i < character_to_replace . length ; i ++ ) {
1515 text = text . replaceAll ( character_to_replace [ i ] , words_replace [ i ] ) ;
1616 }
1717 console . debug ( `Encrypted text: ${ text } ` ) ;
1818 return text ;
19- }
19+ } ;
2020
2121const decryptText = ( text ) => {
2222 for ( let i = 0 ; i < words_replace . length ; i ++ ) {
@@ -25,26 +25,40 @@ const decryptText = (text) => {
2525 console . debug ( `Decrypted text: ${ text } ` ) ;
2626
2727 return text ;
28- }
28+ } ;
2929
3030const encrypt = ( ) => {
31- const text = document . getElementById ( "text-to-encrypt" ) . value . trim ( ) ;
31+ resetStyleTexarea ( "text-to-encrypt" ) ;
32+ resetStyleButton ( "encrypt-button" ) ;
33+ resetStyleButton ( "decrypt-button" ) ;
34+
35+ const text = document . getElementById ( "text-to-encrypt" ) . value . trim ( ) . toLowerCase ( ) ;
3236 console . debug ( `Text to encrypt: ${ text } ` ) ;
37+ document . getElementById ( "text-to-encrypt" ) . value = text ;
3338
3439 if ( ! verifyCharacters ( text ) ) {
35- alert ( "Texto invalido" ) ;
40+ console . error ( "Text invalid" ) ;
41+ document . getElementById ( "notification" ) . innerHTML = "Solo se admiten caracteres alfabeticos sin acentos." ;
42+ document . getElementById ( "text-to-encrypt" ) . classList . replace ( "w3-border-black" , "w3-border-red" ) ;
43+ document . getElementById ( "text-to-encrypt" ) . classList . add ( "w3-animate-opacity" ) ;
3644 return ;
3745 }
3846
3947 const encryptedText = encrypText ( text ) ;
4048 document . getElementById ( "encrypted-text" ) . value = encryptedText ;
49+ setSuccessStyleButton ( "encrypt-button" ) ;
4150
4251 return ;
43- }
52+ } ;
4453
4554const decrypt = ( ) => {
46- const text = document . getElementById ( "text-to-encrypt" ) . value . trim ( ) ;
55+ resetStyleTexarea ( "encrypted-text" ) ;
56+ resetStyleButton ( "decrypt-button" ) ;
57+ resetStyleButton ( "encrypt-button" ) ;
58+
59+ const text = document . getElementById ( "text-to-encrypt" ) . value . trim ( ) . toLowerCase ( ) ;
4760 console . debug ( `Text to decrypt: ${ text } ` ) ;
61+ document . getElementById ( "text-to-encrypt" ) . value = text ;
4862
4963 if ( ! verifyCharacters ( text ) ) {
5064 alert ( "Texto invalido" ) ;
@@ -53,17 +67,38 @@ const decrypt = () => {
5367
5468 const decryptedText = decryptText ( text ) ;
5569 document . getElementById ( "encrypted-text" ) . value = decryptedText ;
56-
70+ setSuccessStyleButton ( "decrypt-button" ) ;
5771 return ;
58- }
72+ } ;
5973
6074
75+ const setSuccessStyleButton = ( idElement ) => {
76+ document . getElementById ( idElement ) . classList . add ( "w3-border" ) ;
77+ document . getElementById ( idElement ) . classList . add ( "w3-border-green" ) ;
78+ document . getElementById ( idElement ) . classList . remove ( "w3-dark-grey" ) ;
79+ document . getElementById ( idElement ) . classList . add ( "w3-green" ) ;
80+ document . getElementById ( idElement ) . classList . add ( "w3-animate-opacity" ) ;
81+
82+ } ;
83+
84+ const resetStyleButton = ( idElement ) => {
85+ document . getElementById ( idElement ) . classList . remove ( "w3-border" ) ;
86+ document . getElementById ( idElement ) . classList . remove ( "w3-border-green" ) ;
87+ document . getElementById ( idElement ) . classList . remove ( "w3-green" ) ;
88+ document . getElementById ( idElement ) . classList . add ( "w3-dark-grey" ) ;
89+ } ;
90+
91+ const resetStyleTexarea = ( idElement ) => {
92+ document . getElementById ( idElement ) . classList . replace ( "w3-border-red" , "w3-border-black" ) ;
93+ document . getElementById ( idElement ) . classList . remove ( "w3-animate-opacity" ) ;
94+ } ;
95+
6196const copyToClipboard = ( ) => {
6297 if ( navigator . clipboard ) {
6398 const str = document . getElementById ( "encrypted-text" ) . value . trim ( ) ;
6499 navigator . clipboard . writeText ( str )
65100 . then ( ( ) => console . log ( "Text copied to clipboard" ) )
66- . catch ( err => console . log ( ' Something went wrong' , err ) ) ;
101+ . catch ( err => console . log ( " Something went wrong" , err ) ) ;
67102 } else {
68103 alert ( "API clipboard not supported" ) ;
69104 console . log ( "API clipboard not supported" ) ;
@@ -85,4 +120,12 @@ window.onload = () => {
85120 const copyButton = document . getElementById ( "copy-button" ) ;
86121 copyButton . onclick = copyToClipboard ;
87122
88- }
123+ const textToEncrypt = document . getElementById ( "text-to-encrypt" ) ;
124+ textToEncrypt . onkeyup = ( ) => {
125+ document . getElementById ( "notification" ) . innerHTML = "" ;
126+ document . getElementById ( "encrypted-text" ) . value = "" ;
127+
128+ resetStyleButton ( "encrypt-button" ) ;
129+ resetStyleButton ( "decrypt-button" ) ;
130+ } ;
131+ } ;
0 commit comments