Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 67 additions & 12 deletions entrypoints/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,47 @@ import { createRoot } from "react-dom/client"
import Turndown from "turndown"
import { browser } from "wxt/browser"
import { getRoot, Noti, showNotification } from "@/lib/showNotification"
import { getOptions } from "@/lib/storage"
import { getOptions, addToHistory } from "@/lib/storage"
import { translateText, translateMarkdown } from "@/lib/translate"
import { defaultTagsToRemove } from "@/lib/tagsToRemove"
import { convertSrtToText } from "@/lib/yt/convertSrtToText"
import { getVideoInfo } from "@/lib/yt/getVideoInfo"
import { getVideoSubtitle } from "@/lib/yt/getVideoSubtitle"

const tiktoken = new Tiktoken(o200k_base)

// Utility to copy markdown to clipboard, respond to sender and optionally show toast/confetti
type ContentType = "webpage" | "subtitle" | "selection"

const copyAndNotify = async ({
markdown,
wrapInTripleBackticks,
showSuccessToast,
showConfetti,
sendResponse,
successMessagePrefix,
contentType,
title,
url,
}: {
markdown: string
wrapInTripleBackticks: boolean
showSuccessToast: boolean
showConfetti: boolean
sendResponse: (response: { success: boolean }) => void
successMessagePrefix: string
contentType: ContentType
title?: string
url?: string
}) => {
if (wrapInTripleBackticks) {
markdown = `\`\`\`md\n${markdown}\n\`\`\``
}
const finalMarkdown = wrapInTripleBackticks
? `\`\`\`md\n${markdown}\n\`\`\``
: markdown

try {
await navigator.clipboard.writeText(markdown)
await navigator.clipboard.writeText(finalMarkdown)
} catch (error) {
// Fallback for when document is not focused (e.g., DevTools is open)
const textarea = document.createElement("textarea")
textarea.value = markdown
textarea.value = finalMarkdown
textarea.style.position = "fixed"
textarea.style.opacity = "0"
document.body.appendChild(textarea)
Expand All @@ -48,16 +55,28 @@ const copyAndNotify = async ({
document.body.removeChild(textarea)
}

sendResponse({ success: true })
const tokens = tiktoken.encode(finalMarkdown)
const tokenCount = tokens.length

const tokens = tiktoken.encode(markdown)
try {
await addToHistory({
content: finalMarkdown,
title: title || document.title || "Untitled",
url: url || window.location.href,
type: contentType,
tokenCount,
})
} catch (error) {
console.error("Failed to add to history:", error)
}

sendResponse({ success: true })

if (showSuccessToast) {
showNotification(`${successMessagePrefix} (${tokens.length} tokens)`)
showNotification(`${successMessagePrefix} (${tokenCount} tokens)`)
}

if (showConfetti) {
// Send a message to the background script to open the Raycast confetti URL
browser.runtime.sendMessage({ type: "OPEN_CONFETTI" })
}
}
Expand Down Expand Up @@ -147,6 +166,9 @@ export default defineContentScript({
showConfetti,
sendResponse,
successMessagePrefix: "Copied as markdown",
contentType: "webpage",
title: document.title,
url: window.location.href,
})

return true
Expand Down Expand Up @@ -184,10 +206,43 @@ export default defineContentScript({
showConfetti,
sendResponse,
successMessagePrefix: "Subtitle copied to clipboard",
contentType: "subtitle",
title,
url: window.location.href,
})

return true
}

if (msg.type === "TRANSLATE_TEXT") {
try {
const { text, targetLanguage } = msg.payload
const result = await translateText(text, targetLanguage)
sendResponse(result)
} catch (error) {
console.error("Translation error:", error)
sendResponse({
success: false,
error: error instanceof Error ? error.message : "Translation failed",
})
}
return true
}

if (msg.type === "TRANSLATE_MARKDOWN") {
try {
const { markdown, targetLanguage } = msg.payload
const result = await translateMarkdown(markdown, targetLanguage)
sendResponse(result)
} catch (error) {
console.error("Markdown translation error:", error)
sendResponse({
success: false,
error: error instanceof Error ? error.message : "Translation failed",
})
}
return true
}
})
},
})
Loading
Loading