research + download + storage wip

master
km0 3 years ago
parent e1560fee5f
commit 7369b38357

@ -10,6 +10,7 @@
</head> </head>
<body> <body>
<button id="save">Save</button> <button id="save">Save</button>
<button id="reset">Reset</button>
<div id="result"></div> <div id="result"></div>
<div id="process"></div> <div id="process"></div>
</body> </body>

@ -1,36 +1,85 @@
let recognition = new webkitSpeechRecognition() || new SpeechRecognition(); // GLOBAL VARIABLES
let textStorage = localStorage.getItem("speech");
let allTheInterim = ""; let allTheInterim = "";
let finalTranscripts = "";
let interimTranscripts = "";
let speech = document.getElementById("result"); let speech = document.getElementById("result");
let process = document.getElementById("process"); let process = document.getElementById("process");
// TEXT STORAGE
let textStorage = localStorage.getItem("speech");
speech.innerHTML = textStorage;
// RESET STORAGE
let resetStorage = document.getElementById("reset");
resetStorage.addEventListener("click", () => {
// Reset everything
allTheInterim = "";
finalTranscripts = "";
interimTranscripts = "";
speech.innerHTML = "";
textStorage = "";
localStorage.setItem("speech", "");
});
// SAVE FILE
let saveButton = document.getElementById("save");
saveButton.addEventListener("click", () => {
download("speech.html", localStorage.getItem("speech"));
});
function download(filename, text) {
var element = document.createElement("a");
element.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(text));
element.setAttribute("download", filename);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// START LISTENING
startConverting(); startConverting();
function startConverting() { function startConverting() {
if ("webkitSpeechRecognition" in window) { if ("webkitSpeechRecognition" in window) {
let speechRecognizer = new webkitSpeechRecognition() || new SpeechRecognition(); let speechRecognizer = new webkitSpeechRecognition() || new SpeechRecognition();
speechRecognizer.continuous = true;
speechRecognizer.continuous = true;
speechRecognizer.interimResults = true; speechRecognizer.interimResults = true;
speechRecognizer.lang = "en-US"; speechRecognizer.lang = "en-US";
speechRecognizer.start(); speechRecognizer.start();
let finalTranscripts = ""; finalTranscripts = "";
speechRecognizer.addEventListener("end", function () { // LISTENERS
// ON END
speechRecognizer.onend = function () {
console.log("Speech recognition service disconnected"); console.log("Speech recognition service disconnected");
speechRecognizer.start(); speechRecognizer.start();
}); };
// ON SOUND START
speechRecognizer.onsoundstart = function () { speechRecognizer.onsoundstart = function () {
console.log("Some sound is being received"); console.log("Some sound is being received");
}; };
// ON ERROR
speechRecognizer.onerror = function (event) {};
// ON RESULT
speechRecognizer.onresult = function (event) { speechRecognizer.onresult = function (event) {
let interimTranscripts = ""; interimTranscripts = "";
for (let i = event.resultIndex; i < event.results.length; i++) { for (let i = event.resultIndex; i < event.results.length; i++) {
let transcript = event.results[i][0].transcript; let transcript = event.results[i][0].transcript;
@ -51,18 +100,11 @@ function startConverting() {
speech.innerHTML = final; speech.innerHTML = final;
textStorage += finalTranscripts; textStorage = final;
console.log(textStorage);
localStorage.setItem("speech", textStorage); localStorage.setItem("speech", final);
}; };
speechRecognizer.onerror = function (event) {};
} else { } else {
speech.innerHTML = "oi va su chrome pirla"; speech.innerHTML = "At the moment this works only in Chrome, sorry";
} }
} }
let saveButton = document.getElementById("save");
saveButton.addEventListener("click", () => {
console.log("we");
console.log(localStorage.getItem("speech"));
});

Binary file not shown.
Loading…
Cancel
Save