-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
115 lines (100 loc) · 3.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text File Editor</title>
</head>
<body>
<label for="fileName">Save As:</label>
<input type="text" id="fileName" placeholder="Enter file name">
<textarea id="textArea" rows="10" cols="30"></textarea><br>
<button onclick="saveText()">Save</button>
<button onclick="loadText()">Load</button>
<button onclick="loadTextbrowser()">LoadWithUI</button>
<label for="kindleFile">Load Kindle File:</label>
<input type="text" id="kindleFile" accept="text/plain">
<script>
// Function to set a cookie with the given name and value
function setCookie(name, value) {
document.cookie = `${name}=${value}; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/`;
}
// Function to get the value of a cookie by name
function getCookie(name) {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
if (cookie.startsWith(name + '=')) {
return cookie.substring(name.length + 1);
}
}
return null;
}
// Function to save the text to a cookie
function saveTextToCookies() {
var textToSave = document.getElementById('textArea').value;
var fileName = document.getElementById('fileName').value || 'textfile'; // Default to 'textfile' if no name is provided
setCookie(fileName, textToSave);
alert('Text saved to cookie!');
}
// Function to load the text from a cookie
function loadTextfromCookies() {
var fileName = document.getElementById('fileName').value || 'textfile'; // Default to 'textfile' if no name is provided
var savedText = getCookie(fileName);
if (savedText !== null) {
document.getElementById('textArea').value = savedText;
alert('Text loaded from cookie!');
} else {
alert('No saved text found.');
}
}
function loadTextbrowser() {
var input = document.createElement('input');
input.type = 'file';
input.accept = 'text/plain';
input.onchange = function (event) {
var file = event.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function (e) {
document.getElementById('textArea').value = e.target.result;
};
reader.readAsText(file);
};
input.click();
}
// Load the text from the cookie when the page loads
window.onload = function () {
loadTextfromCookies();
};
function saveText() {
saveTextToCookies();
var textToSave = document.getElementById('textArea').value;
var fileName = document.getElementById('fileName').value || 'textfile'; // Default to 'textfile' if no name is provided
var blob = new Blob([textToSave], { type: 'text/plain' });
var a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = fileName + '.txt';
a.click();
}
function loadText() {
loadTextfromCookies();
var input = document.getElementById('kindleFile');
var file = input.files[0];
if (file) {
var reader = new FileReader();
reader.onload = function (e) {
document.getElementById('textArea').value = e.target.result;
};
reader.readAsText(file);
}
}
// Load the text from the cookie when the page loads
window.onload = function () {
loadTextfromCookies();
};
</script>
</body>
</html>