mirror of
https://github.com/browseros-ai/BrowserOS.git
synced 2026-05-13 23:53:25 +00:00
* feat: select text and pass to sidepanel * fix: lint issues * fix: persist selection across tabs * fix: review comments * fix: change when the selection is cleared * feat: sanitize url
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { selectedTextStorage } from '@/lib/selected-text/selectedTextStorage'
|
|
|
|
const MAX_SELECTED_TEXT_LENGTH = 5000
|
|
|
|
export default defineContentScript({
|
|
matches: ['*://*/*'],
|
|
runAt: 'document_idle',
|
|
async main() {
|
|
const response = await chrome.runtime.sendMessage({ type: 'get-tab-id' })
|
|
const tabId: number | undefined = response?.tabId
|
|
if (!tabId) return
|
|
|
|
const key = String(tabId)
|
|
|
|
document.addEventListener('mouseup', () => {
|
|
const text = window.getSelection()?.toString().trim()
|
|
|
|
if (text && text.length > 0) {
|
|
selectedTextStorage.getValue().then((map) => {
|
|
selectedTextStorage.setValue({
|
|
...map,
|
|
[key]: {
|
|
text: text.slice(0, MAX_SELECTED_TEXT_LENGTH),
|
|
pageUrl: window.location.href,
|
|
pageTitle: document.title,
|
|
tabId,
|
|
timestamp: Date.now(),
|
|
},
|
|
})
|
|
})
|
|
} else {
|
|
// User clicked without selecting — clear this tab's entry only
|
|
selectedTextStorage.getValue().then((map) => {
|
|
if (map[key]) {
|
|
const { [key]: _, ...rest } = map
|
|
selectedTextStorage.setValue(rest)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
},
|
|
})
|