feat: highlighted text as input

This commit is contained in:
Timothy J. Baek 2024-05-22 23:14:51 -07:00
parent 7282c32565
commit 97a4897da2
5 changed files with 39 additions and 15 deletions

View File

@ -1,10 +1,21 @@
// chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// // read changeInfo data and do something with it (like read the url)
// if (changeInfo.url) {
// // do something here
// chrome.tabs.sendMessage(tabId, {
// message: "urlChange",
// url: changeInfo.url,
// });
// }
// });
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log(request, sender);
const id = sender.tab.id;
if (request.method == "getSelection") {
chrome.scripting
.executeScript({
target: { tabId: id, allFrames: true },
func: () => {
return window.getSelection().toString();
},
})
.then((res) => {
console.log(res);
sendResponse({ data: res[0]["result"] });
});
} else {
sendResponse({});
}
return true;
});

View File

@ -23,4 +23,4 @@ function injectCSS(file) {
// Inject the CSS and JS files
injectCSS(chrome.runtime.getURL("extension/dist/style.css"));
injectScript(chrome.runtime.getURL("extension/dist/main.js"), "body");
// injectScript(chrome.runtime.getURL("extension/dist/main.js"), "body");

File diff suppressed because one or more lines are too long

View File

@ -6,7 +6,7 @@ export const SpotlightSearch = () => {
// Toggle the menu when Space+Shift is pressed
useEffect(() => {
const down = (e) => {
const down = async (e) => {
if (
e.key === " " &&
(e.metaKey || e.ctrlKey) &&
@ -14,6 +14,15 @@ export const SpotlightSearch = () => {
) {
e.preventDefault();
setOpen((open) => !open);
const response = await chrome.runtime.sendMessage({
method: "getSelection",
});
if (response?.data ?? false) {
setSearchValue(response.data);
}
setTimeout(() => {
const inputElement = document.getElementById(
"open-webui-search-input"
@ -40,6 +49,8 @@ export const SpotlightSearch = () => {
setSearchValue("");
window.open(`http://localhost:8080/?q=${searchValue}`, "_blank");
setOpen(false);
};
return open ? (

View File

@ -18,7 +18,8 @@
"<all_urls>"
],
"js": [
"content.js"
"content.js",
"extension/dist/main.js"
]
}
],
@ -36,6 +37,7 @@
}
],
"permissions": [
"storage"
"storage",
"scripting"
]
}