- Reverted the broken openTranslateModal function to a working state. - Re-implemented the logic to correctly inject the <base> tag for relative paths and to ensure the translation notice is displayed correctly. - Ran biome to format the code and ensure there are no syntax errors.
68 lines
2 KiB
JavaScript
68 lines
2 KiB
JavaScript
// Page reading functionality
|
|
import { extractWebpageContent } from "./content.js";
|
|
import { processContentWithHermes, speakText } from "./tts.js";
|
|
|
|
// Function to read the entire page using Hermes
|
|
export async function readPageWithHermes() {
|
|
const content = extractWebpageContent();
|
|
|
|
// Create status indicator
|
|
const statusDiv = document.createElement("div");
|
|
statusDiv.style.position = "fixed";
|
|
statusDiv.style.top = "10px";
|
|
statusDiv.style.right = "10px";
|
|
statusDiv.style.padding = "10px";
|
|
statusDiv.style.background = "rgba(0,0,0,0.8)";
|
|
statusDiv.style.color = "white";
|
|
statusDiv.style.borderRadius = "5px";
|
|
statusDiv.style.zIndex = "1000";
|
|
document.body.appendChild(statusDiv);
|
|
|
|
statusDiv.textContent = "Processing content and generating speech...";
|
|
|
|
const processedContent = await processContentWithHermes(content);
|
|
|
|
// Generate TTS with default voice and 90% speed immediately
|
|
const { audio, blob } = await speakText(processedContent, "alloy", 0.9);
|
|
|
|
statusDiv.textContent = "Reading page... ";
|
|
|
|
let isPaused = false;
|
|
|
|
// Add pause/resume button
|
|
const pauseButton = document.createElement("button");
|
|
pauseButton.textContent = "Pause Reading";
|
|
pauseButton.style.marginLeft = "10px";
|
|
statusDiv.appendChild(pauseButton);
|
|
|
|
// Add download button
|
|
const downloadButton = document.createElement("button");
|
|
downloadButton.textContent = "Download MP3";
|
|
downloadButton.style.marginLeft = "10px";
|
|
downloadButton.onclick = () => {
|
|
const a = document.createElement("a");
|
|
a.href = URL.createObjectURL(blob);
|
|
a.download = `${document.title.replace(/\s+/g, "-").toLowerCase()}.mp3`;
|
|
a.click();
|
|
};
|
|
statusDiv.appendChild(downloadButton);
|
|
|
|
pauseButton.onclick = () => {
|
|
if (isPaused) {
|
|
audio.play();
|
|
pauseButton.textContent = "Pause Reading";
|
|
} else {
|
|
audio.pause();
|
|
pauseButton.textContent = "Resume Reading";
|
|
}
|
|
isPaused = !isPaused;
|
|
};
|
|
|
|
// Set up audio end handler
|
|
audio.onended = () => {
|
|
statusDiv.remove();
|
|
};
|
|
|
|
// Start playing immediately
|
|
audio.play();
|
|
}
|