console
const version = "[0.1]"
const inputer = document.getElementById('input')
const outputer = document.getElementById('output')
function encode() {
output()
if (document.getElementById('b64').checked) {
console.log(inputer.innerText)
output(window.btoa(window.encodeURIComponent(inputer.innerHTML)));
return
}
let input = inputer.innerText.split("");
let ans = [];
for (var i = 0; i < input.length; i++) {
let binStr = input[i].charCodeAt().toString(2);
let tempGene = '';
if (binStr.length > 8) {
output("<red>[编码错误] 不能出现汉字</red>");
return;
}
if (binStr.length < 8) {
binStr = binStr.padStart(8, '0')
binStr[0] = Math.floor(2 * Math.random()).toString();
for (let i = 0; i < 4; i++) {
switch (binStr[2 * i] + binStr[2 * i + 1]) {
case '00':
tempGene += 'A'
break;
case '01':
tempGene += 'U'
break;
case '10':
tempGene += 'C'
break;
case '11':
tempGene += 'G'
break;
default:
tempGene += 'O'
break;
}
}
}
ans.push(tempGene);
}
output(ans.join(''));
}
function decode() {
if (inputer.innerText == '') {
return;
}
output()
if (document.getElementById('b64').checked) {
output(decodeURI(window.atob(window.decodeURIComponent(inputer.innerText))));
return
}
let pieces = inputer.innerText.replace(/(.{4})/g, '$1,').split(',');
if (pieces.slice(-1)[0] == '') {
pieces.pop();
}
let ans = [];
if (pieces.slice(-1)[0].length != 4) {
output('<red>[解码错误] 不符合基因规范(末位点不可读):' +
pieces.slice(-1)[0] + '</red>');
return;
}
pieces.forEach(element => {
let tempArr = '';
for (i of element) {
switch (i.toUpperCase()) {
case 'A':
tempArr += '00'
break;
case 'U':
tempArr += '01'
break;
case 'C':
tempArr += '10'
break;
case 'G':
tempArr += '11'
break;
default:
output('<red>[解码错误] 未知的碱基:' + i + '</red>');
return;
}
}
ans.push(String.fromCharCode(
parseInt(tempArr.slice(1).replace(/\b(0+)/gi, ""), 2)
));
});
if (ans != '') {
output(ans.join(''));
}
}
function output(html = '') {
outputer.innerHTML = html;
}
function exchange() {
let temp = outputer.innerText;
output(inputer.innerText);
inputer.innerText = temp;
}
function vibration() {
navigator.vibrate = navigator.vibrate ||
navigator.webkitVibrate ||
navigator.mozVibrate ||
navigator.msVibrate;
if (navigator.vibrate) {
console.log("支持震动!")
}
}
function run() {
decode(inputer.innerText)
let cmd = outputer.innerHTML
output()
if (cmd[0] != '$') {
return
}
eval(cmd.slice(1))
}
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>在上面的框输入要编码的玩意儿</h1>
<div>
<br />
<p id="input" contenteditable="true"></p>
<button onclick="encode()">编码↓</button>
<button onclick="decode()">解码↓</button>
<button onclick="run()">⚡翻译(exe)↓</button>
<input type="checkbox" id="b64">Base64模式</input>
<p id="output" contenteditable="true"></p>
<button onclick="exchange()">上下内容交换</button>
</div>
</body>
</html>
* {
font-family: consolas;
}
p {
font-size: 16px;
width: 20em;
min-height: 10em;
border: solid 1px black;
word-wrap: break-word;
word-break: normal;
}
textarea {
font-size: 16px;
width: 20em;
min-height: 10em;
}
red {
color: red;
}
div{
margin-left: 1em
}