-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
409 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useState, useEffect } from 'react'; | ||
import Modal from './components/Modal'; | ||
import Logo from '@/public/logo.svg'; | ||
|
||
const getPageContext = () => { | ||
return { | ||
content: document.body.innerText, | ||
title: document.title, | ||
keywords: document.querySelector('meta[name="keywords"]')?.content, | ||
description: document.querySelector('meta[name="description"]')?.content, | ||
}; | ||
}; | ||
|
||
export default ({ ctx }) => { | ||
const [showPage, setShowPage] = useState(false); | ||
const [showButton, setShowButton] = useState(false); | ||
const [buttonPosition, setButtonPosition] = useState({ x: 0, y: 0 }); | ||
const [selection, setSelection] = useState(''); | ||
const [payload, setPayload] = useState({}); | ||
|
||
useEffect(() => { | ||
const handleSelection = e => { | ||
const selection = window.getSelection(); | ||
if (selection.toString().length > 0) { | ||
setSelection(selection.toString()); | ||
setShowButton(true); | ||
setButtonPosition({ x: e.clientX + 6, y: e.clientY + 8 }); | ||
} else { | ||
setShowButton(false); | ||
} | ||
}; | ||
|
||
document.addEventListener('mouseup', handleSelection); | ||
return () => document.removeEventListener('mouseup', handleSelection); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (showPage) { | ||
document.querySelector('html').style.overflow = 'hidden'; | ||
} else { | ||
document.querySelector('html').style.overflow = 'auto'; | ||
} | ||
}, [showPage]); | ||
|
||
const handleButtonClick = () => { | ||
setPayload({ selection, type: 'query', context: getPageContext() }); | ||
setShowPage(true); | ||
setShowButton(false); | ||
}; | ||
|
||
return ( | ||
<div className="fixed right-0 my-auto top-0 bottom-0 flex items-center"> | ||
<Modal | ||
visible={showPage} | ||
close={() => setShowPage(false)} | ||
payload={payload} | ||
/> | ||
<div | ||
style={{ | ||
left: `${buttonPosition.x}px`, | ||
top: `${buttonPosition.y}px`, | ||
boxShadow: '0 2px 10px 0 rgba(0,0,0,.1)', | ||
}} | ||
className={ | ||
'fixed z-[99998] p-[4px] cursor-pointer rounded-[4px] bg-white transition-all duration-300 transform hover:scale-105 ' + | ||
(showButton ? ' opacity-100' : 'opacity-0 hidden') | ||
} | ||
onClick={handleButtonClick} | ||
> | ||
<img src={Logo} className="!h-[16px] !w-[16px] " /> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useEffect } from 'react'; | ||
import { useState } from 'react'; | ||
|
||
export default function ({ close, payload, visible }) { | ||
const url = browser.runtime.getURL('ext.html') + '#web-copilot'; | ||
const [loaded, setLoaded] = useState(false); | ||
const iframeRef = useRef(null); | ||
|
||
useEffect(() => { | ||
if (!visible) { | ||
iframeRef.current.contentWindow.postMessage( | ||
JSON.stringify({ type: 'clear' }), | ||
'*' | ||
); | ||
return; | ||
} | ||
if (payload.type) { | ||
iframeRef.current.contentWindow.postMessage(JSON.stringify(payload), '*'); | ||
} | ||
}, [payload, loaded, visible]); | ||
return ( | ||
<div | ||
className={ | ||
'fixed inset-0 z-[99999] transform bg-black bg-opacity-70 filter backdrop-blur flex justify-center items-center h-full ' + | ||
(visible ? '' : 'translate-x-full translate-y-full') | ||
} | ||
onClick={close} | ||
> | ||
<iframe | ||
ref={iframeRef} | ||
onLoad={() => { | ||
setLoaded(true); | ||
}} | ||
className={ | ||
'border-none transition-opacity duration-300 outline-none rounded-[16px] shadow-xl bg-black overflow-hidden w-[512px] h-[80dvh] ' + | ||
(loaded ? 'opacity-100' : 'opacity-0') | ||
} | ||
src={url} | ||
></iframe> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import './style.css'; | ||
import ReactDOM from 'react-dom/client'; | ||
import App from './App'; | ||
|
||
export default defineContentScript({ | ||
matches: ['*://*/*'], | ||
cssInjectionMode: 'ui', | ||
|
||
async main(ctx) { | ||
console.log('content script injecting', ctx); | ||
const ui = await createShadowRootUi(ctx, { | ||
name: 'silo-ui', | ||
position: 'overlay', | ||
alignment: 'bottom-right', | ||
zIndex: 99999, | ||
anchor: 'body', | ||
append: 'first', | ||
onMount: container => { | ||
// Don't mount react app directly on <body> | ||
const wrapper = document.createElement('div'); | ||
wrapper.className = ''; | ||
container.append(wrapper); | ||
|
||
const root = ReactDOM.createRoot(wrapper); | ||
root.render(<App />); | ||
return { root, wrapper }; | ||
}, | ||
onRemove: elements => { | ||
elements?.root.unmount(); | ||
elements?.wrapper.remove(); | ||
}, | ||
}); | ||
|
||
ui.mount(); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
* { | ||
padding: 0; | ||
margin: 0; | ||
} | ||
|
||
body { | ||
padding: 16px; | ||
} | ||
|
||
.selected-search-box { | ||
z-index: 300; | ||
position: absolute; | ||
cursor: pointer; | ||
border: 0; | ||
background: #fff; | ||
box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.1); | ||
border-radius: 6px; | ||
padding: 10px 15px 9px 16px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@src/*": [ | ||
"./src/*" | ||
] | ||
}, | ||
"jsx": "react" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.