javascript - 如何使用 javascript 将 textarea 转换为显示其他内容(例如 base64 解密)?
问题描述
我正在尝试在 HTML 中创建一个解密页面。我可以获得输入,并将其显示为输出,但我对如何更改中间的文本感到困惑。到目前为止,这是我的代码:http: //jsfiddle.net/RZwmX/1。
HTML:
<textarea name="CipherText" id="CipherText" placeholder="Enter ciphertext here:"></textarea>
<div id="prevCom"></div>
Javascript:
wpcomment.onkeyup = wpcomment.onkeypress = function(){
wpcomment=btoa(wpcomment);
document.getElementById('prevCom').innerHTML = this.value;
}
//////////////////////////////////////
//from: https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript
// Define the string
var string = 'Hello World!';
// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"
// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
我从https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript获取代码。Base64 只是一个例子。我想知道的是如何进行过渡。在我的第 3 行 javascript 的 JSFiddle 中,我想到了如何实现转换,但这不起作用。
解决方案
我会做这样的事情:
const input = document.querySelector('#encoded-input')
const output = document.querySelector('#decoded-output')
input.oninput = e => {
try {
output.innerText = atob(e.target.value)
} catch (error) {
output.innerText = e.target.value
? 'Please enter a valid Base64 encoded value.'
: ''
}
}
<textarea id="encoded-input"></textarea>
<div id="decoded-output"></div>
推荐阅读
- javascript - chrome 浏览器中 sourceIndex 的 Javascript 替换
- python - 在 centos 7 上安装 gnuhealth 客户端时出现问题
- python - Python,使用np.dstack时出错,无法解决
- python - 如何在后台(Windows)中使用 selenium chromedriver 运行 python 脚本?
- javascript - 在 Thymeleaf 模板中包含 JS-Libraries 时拒绝执行脚本
- weblogic - com.rsa.jsafe.JSAFE_PaddingException:WebLogic Server 中的无效填充
- c# - 当按日期净值的总值为零时如何从列表中删除
- reactjs - 使用 React SPFx 将当前登录的用户 ID 存储在变量中
- node.js - 使用 PRN 堆栈获取对 Express 服务器的请求停止
- c# - Active Directory Docker - Windows 身份验证模式