-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
133 lines (123 loc) · 4.21 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text and JSON Converter</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
padding: 20px;
text-align: center;
}
.textarea-container {
display: flex;
flex-direction: column;
align-items: center;
margin: 10px;
}
textarea {
width: 300px;
height: 300px;
margin: 0;
padding: 10px;
}
button {
padding: 10px 15px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
margin-top: 10px;
}
button:hover {
background-color: #45a049;
}
#container {
display: flex;
justify-content: center;
align-items: start;
}
#buttons {
display: flex;
flex-direction: column;
align-items: center;
margin: 10px;
}
#copySuccess {
color: green;
display: none;
}
</style>
<script>
function convertToJSON() {
var inputText = document.getElementById("inputText").value;
var parts = inputText.split("### ");
var conversation = [];
for (var part of parts) {
if (part.trim().startsWith("Human:")) {
var text = part.trim().substring("Human:".length).trim();
conversation.push({ "speaker": "Human", "text": text });
} else if (part.trim().startsWith("Assistant:")) {
var text = part.trim().substring("Assistant:".length).trim();
conversation.push({ "speaker": "Assistant", "text": text });
}
}
var jsonText = JSON.stringify({ "conversation": conversation }, null, 2);
document.getElementById("outputText").value = jsonText;
}
function convertBackToText() {
var jsonText = document.getElementById("outputText").value;
var obj = JSON.parse(jsonText);
var text = "";
obj.conversation.forEach(function(part) {
text += "### " + part.speaker + ": " + part.text + "\n";
});
document.getElementById("inputText").value = text.trim();
}
function copyToClipboard(elementId) {
var copyText = document.getElementById(elementId);
copyText.select();
document.execCommand("copy");
// Show copy success message
var successMsg = document.getElementById("copySuccess");
successMsg.style.display = "block";
setTimeout(function() {
successMsg.style.display = "none";
}, 2000); // Hide after 2 seconds
}
function convertToJsonl() {
try {
var jsonInput = document.getElementById("outputText").value;
var parsedJson = JSON.parse(jsonInput);
// Convert the entire JSON object to a single-line string
var singleLineJson = JSON.stringify(parsedJson);
document.getElementById("outputText").value = singleLineJson;
} catch (e) {
alert("Invalid JSON input. Please correct it and try again.");
}
}
</script>
</head>
<body>
<h2>Text and JSON Converter</h2>
<div id="container">
<div class="textarea-container">
<textarea id="inputText" placeholder="Enter text here..."></textarea>
<button onclick="copyToClipboard('inputText')">Copy Text</button>
</div>
<div id="buttons">
<button onclick="convertToJSON()">Convert to JSON →</button>
<button onclick="convertBackToText()">← Convert Back to Text</button>
<button onclick="convertToJsonl()">Convert to Single-Line JSON</button>
</div>
<div class="textarea-container">
<textarea id="outputText" placeholder="JSON output will appear here..."></textarea>
<button onclick="copyToClipboard('outputText')">Copy JSON</button>
</div>
</div>
<div id="copySuccess">Copied to clipboard!</div>
</body>
</html>
</html>