let isFirefox = typeof InstallTrigger !== "undefined"; // Firefox has `InstallTrigger` let browser = isFirefox ? window.browser : chrome; document.addEventListener("DOMContentLoaded", () => { const summarizeButton = document.getElementById("summarize"); const summaryDiv = document.getElementById("summary"); const openOptionsButton = document.getElementById("open-options"); const tokenCountDiv = document.createElement("div"); tokenCountDiv.id = "token-count"; tokenCountDiv.style.marginTop = "10px"; tokenCountDiv.style.fontStyle = "italic"; summarizeButton.parentNode.insertBefore( tokenCountDiv, summarizeButton.nextSibling ); // Correctly define systemPromptTextarea const systemPromptTextarea = document.getElementById("system-prompt"); summarizeButton.addEventListener("click", () => { summaryDiv.innerHTML = "
Summarizing...
"; console.log("Summarizing..."); tokenCountDiv.textContent = ""; summarizeButton.disabled = true; // Get the current tab content browser.tabs.query({ active: true, currentWindow: true }, (tabs) => { // First check if the current URL is a YouTube video const currentUrl = tabs[0].url; console.log("Current URL:", currentUrl); const isYouTubeUrl = currentUrl.includes("youtube.com/watch") || currentUrl.includes("youtu.be/") || currentUrl.includes("/watch?v="); console.log("Is YouTube URL:", isYouTubeUrl); if (isYouTubeUrl) { // Show a notification that we're processing a YouTube video summaryDiv.innerHTML = `Summarizing video content...
`; } browser.tabs.sendMessage( tabs[0].id, { action: "getContent" }, (response) => { console.log("Response:", response); if (browser.runtime.lastError) { handleError( "Error getting page content: " + browser.runtime.lastError.message ); return; } if (response && response.content) { const systemPrompt = systemPromptTextarea.value; var failedToFetchSubtitles = false; // Check if the content appears to be from YouTube var isYouTubeContent = response.content.includes("Title:") && response.content.includes("Transcript:") && (tabs[0].url.includes("youtube.com") || tabs[0].url.includes("youtu.be") || tabs[0].url.includes("/watch?v=")); console.log("Is YouTube Content:", isYouTubeContent); if (response.content.includes("NO TRANSCRIPT AVAILABLE")) { console.log( "Warning: No subtitles available for this video: setting isYouTubeContent to false" ); failedToFetchSubtitles = true; isYouTubeContent = false; } // Customize the prompt for YouTube videos const customizedPrompt = isYouTubeContent ? `${systemPrompt}\n\nThis is a YouTube video transcript. Please summarize the key points discussed in the video.` : systemPrompt; console.log("System prompt:", customizedPrompt); // Send message to background script for summarization browser.runtime.sendMessage( { action: "summarize", content: response.content, systemPrompt: customizedPrompt, }, (response) => { if (browser.runtime.lastError) { handleError( "Error during summarization: " + browser.runtime.lastError.message ); return; } if (response && response.summary) { let warningHtml = ""; if (response.chunkCount > 1) { warningHtml = `Error: ${errorMessage}
`; if (details) { summaryDiv.innerHTML += `${JSON.stringify(details, null, 2)}`; } summarizeButton.disabled = false; } const viewLogsButton = document.getElementById("view-logs"); // Only add the event listener if the button exists if (viewLogsButton) { // Add the same CSS class as other buttons viewLogsButton.className = "button"; let logsVisible = false; viewLogsButton.addEventListener("click", () => { const logsDiv = document.getElementById("logs-container"); // Toggle logs visibility if (logsVisible && logsDiv) { // Hide logs if they're currently visible logsDiv.remove(); logsVisible = false; viewLogsButton.textContent = "View Debug Logs"; return; } // Show logs browser.runtime.sendMessage({ action: "getLogs" }, (response) => { if (response && response.logs) { // Remove existing logs container if it exists if (logsDiv) { logsDiv.remove(); } // Create new logs container const newLogsDiv = document.createElement("div"); newLogsDiv.id = "logs-container"; newLogsDiv.style.marginTop = "20px"; newLogsDiv.style.borderTop = "1px solid #ccc"; newLogsDiv.innerHTML = "