|
| 1 | +import cssText from "data-text:~globals.css" |
| 2 | +import { useStorage } from "@plasmohq/storage/hook" |
| 3 | +import type { PlasmoCSConfig, PlasmoGetStyle } from "plasmo" |
| 4 | +import { useEffect, useState } from "react" |
| 5 | + |
| 6 | +import { Button } from "@/components/ui/button" |
| 7 | +import { MESSAGE_KEYS, STORAGE_KEYS } from "@/lib/constants" |
| 8 | +import { Quote } from "@/lib/lucide-icon" |
| 9 | +import { plasmoGlobalStorage } from "@/lib/plasmo-global-storage" |
| 10 | + |
| 11 | +export const config: PlasmoCSConfig = { |
| 12 | + matches: ["<all_urls>"], |
| 13 | + all_frames: true |
| 14 | +} |
| 15 | + |
| 16 | +export const getStyle: PlasmoGetStyle = () => { |
| 17 | + const style = document.createElement("style") |
| 18 | + style.textContent = cssText |
| 19 | + // Fix for :root variables in Shadow DOM |
| 20 | + // Replace :root with :host to ensure variables apply within the shadow tree |
| 21 | + style.textContent = style.textContent.replace(/:root/g, ":host") |
| 22 | + return style |
| 23 | +} |
| 24 | + |
| 25 | +const SelectionButton = () => { |
| 26 | + const [showButton, setShowButton] = useState(false) |
| 27 | + const [position, setPosition] = useState({ top: 0, left: 0 }) |
| 28 | + const [selectionText, setSelectionText] = useState("") |
| 29 | + |
| 30 | + const [isEnabled] = useStorage<boolean>( |
| 31 | + { |
| 32 | + key: STORAGE_KEYS.BROWSER.SHOW_SELECTION_BUTTON, |
| 33 | + instance: plasmoGlobalStorage |
| 34 | + }, |
| 35 | + true |
| 36 | + ) |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (!isEnabled) { |
| 40 | + setShowButton(false) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + const handleSelectionChange = () => { |
| 45 | + const selection = window.getSelection() |
| 46 | + const text = selection?.toString().trim() |
| 47 | + |
| 48 | + if (text && text.length > 0) { |
| 49 | + const range = selection?.getRangeAt(0) |
| 50 | + const rect = range?.getBoundingClientRect() |
| 51 | + |
| 52 | + if (rect) { |
| 53 | + setPosition({ |
| 54 | + top: rect.bottom + window.scrollY + 10, |
| 55 | + left: rect.right + window.scrollX - 30 |
| 56 | + }) |
| 57 | + setSelectionText(text) |
| 58 | + setShowButton(true) |
| 59 | + } |
| 60 | + } else { |
| 61 | + setShowButton(false) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + document.addEventListener("mouseup", handleSelectionChange) |
| 66 | + document.addEventListener("keyup", handleSelectionChange) |
| 67 | + |
| 68 | + return () => { |
| 69 | + document.removeEventListener("mouseup", handleSelectionChange) |
| 70 | + document.removeEventListener("keyup", handleSelectionChange) |
| 71 | + } |
| 72 | + }, [isEnabled]) |
| 73 | + |
| 74 | + const handleClick = async () => { |
| 75 | + try { |
| 76 | + await chrome.runtime.sendMessage({ |
| 77 | + type: MESSAGE_KEYS.BROWSER.ADD_SELECTION_TO_CHAT, |
| 78 | + payload: selectionText |
| 79 | + }) |
| 80 | + |
| 81 | + setShowButton(false) |
| 82 | + window.getSelection()?.removeAllRanges() |
| 83 | + } catch (error) { |
| 84 | + console.error("Failed to send selection:", error) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (!showButton || !isEnabled) return null |
| 89 | + |
| 90 | + return ( |
| 91 | + <div |
| 92 | + style={{ |
| 93 | + position: "absolute", |
| 94 | + top: position.top, |
| 95 | + left: position.left, |
| 96 | + zIndex: 2147483647 |
| 97 | + }}> |
| 98 | + <Button |
| 99 | + onClick={handleClick} |
| 100 | + variant="secondary" |
| 101 | + className="h-8 gap-2 rounded-lg px-3 shadow-lg transition-all hover:-translate-y-0.5 hover:shadow-xl" |
| 102 | + title="Add to Ollama Client"> |
| 103 | + <Quote className="size-3.5" /> |
| 104 | + <span className="text-xs font-medium">Ask Ollama client</span> |
| 105 | + </Button> |
| 106 | + </div> |
| 107 | + ) |
| 108 | +} |
| 109 | + |
| 110 | +export default SelectionButton |
0 commit comments