-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.js
More file actions
131 lines (108 loc) · 4.58 KB
/
Copy pathasync.js
File metadata and controls
131 lines (108 loc) · 4.58 KB
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
if (localStorage.getItem("chatterID")) {
document.getElementById("chatterID").value = localStorage.getItem("chatterID");
}
if (localStorage.getItem("conversations")) {
document.getElementById("useSavedText").style.display = "inline-block";
}
const fileInput = document.getElementById('txtPicker');
const totalConversations = 100
let sortedMatrix = Array.from({ length: totalConversations }, () => []);
let rawFileContent = "";
fileName = "conversations.txt";
if (localStorage.getItem("fileName")) {
fileName = localStorage.getItem("fileName");
}
let pickedFile = false;
let conversationID = -1;
function useSavedText() {
rawFileContent = localStorage.getItem("conversations");
document.getElementById("useSavedText").style.display = "none";
document.getElementById("txtPicker").style.display = "none";
document.getElementById("chatterID").style.display = "none";
document.getElementById("getNextConversation").style.display = "inline-block";
pickedFile = true;
processText();
setTimeout(getNextConversation,10);
}
function processText() {
let content = rawFileContent.split(":::");
console.log(content)
for (var i = 0; i < content.length; i++) {
for (var j = 0; j < 100; j++) {
if (content[i].startsWith(j +"a") || content[i].startsWith(j +"s")) {
sortedMatrix[j].push(content[i].replace(j + "",""));
}
}
}
}
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0]; // Get the selected file
fileName = event.target.files[0].name;
localStorage.setItem("fileName",fileName);
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
rawFileContent = e.target.result;
processText();
};
reader.readAsText(file);
document.getElementById("txtPicker").style.display = "none";
document.getElementById("getNextConversation").style.display = "inline-block";
document.getElementById("useSavedText").style.display = "none";
document.getElementById("chatterID").style.display = "none";
pickedFile = true;
setTimeout(getNextConversation,100);
}
});
function getNextConversation() {
if (conversationID != -1) {
rawFileContent += "\n" + ":::" + conversationID + document.getElementById("chatterID").value + " " + document.getElementById("response").value;
localStorage.setItem("conversations",rawFileContent);
document.getElementById("response").value = "";
if (document.getElementById("conversationsLeft").innerHTML.startsWith("1")) {
document.getElementById("conversationsLeft").innerHTML = "ur done:o";
}
}
console.log(rawFileContent)
const listContainer = document.getElementById("currentConversation");
// 1. Clear the last conversation immediately
listContainer.innerHTML = "";
for (var j = conversationID+1; j < totalConversations; j++) {
if (!sortedMatrix[j][sortedMatrix[j].length-1].startsWith(document.getElementById("chatterID").value + " ")) {
document.getElementById("conversationsLeft").innerHTML = (totalConversations-j) + " to go:3";
conversationID = j;
sortedMatrix[j].forEach((item) => {
let li = document.createElement("li");
li.innerText = item;
listContainer.prepend(li); // Adds to the top instead of the bottom
});
break; // Exit loop after finding and displaying one conversation
}
}
}
function checkID() {
if ((document.getElementById("chatterID").value == "a" || document.getElementById("chatterID").value == "s") && !pickedFile) {
document.getElementById("txtPicker").style.display = "inline-block";
}
localStorage.setItem("chatterID",document.getElementById("chatterID").value);
}
function downloadTextFile() {
// 1. Create a Blob with the text content
// We specify the type as 'text/plain'
const blob = new Blob([rawFileContent], { type: 'text/plain' });
// 2. Create a temporary URL for the Blob
const url = window.URL.createObjectURL(blob);
// 3. Create a hidden <a> element
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
// 4. Set the name of the file to be downloaded
a.download = fileName;
// 5. Add to page, click it, and remove it
document.body.appendChild(a);
a.click();
// 6. Clean up: remove the link and revoke the URL to save memory
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}
setInterval(checkID, 50);