Files
page-assist/src/entries/background.ts

213 lines
6.0 KiB
TypeScript
Raw Normal View History

2024-03-23 14:44:05 +05:30
import { getOllamaURL, isOllamaRunning } from "../services/ollama"
import { browser } from "wxt/browser"
2024-08-03 23:00:57 +05:30
import { clearBadge, streamDownload } from "@/utils/pull-ollama"
2024-07-14 23:13:48 +05:30
2024-03-23 14:44:05 +05:30
export default defineBackground({
main() {
2024-08-03 23:00:57 +05:30
let isCopilotRunning: boolean = false
browser.runtime.onMessage.addListener(async (message) => {
2024-03-23 14:44:05 +05:30
if (message.type === "sidepanel") {
2024-07-14 23:13:48 +05:30
await browser.sidebarAction.open()
2024-03-23 14:44:05 +05:30
} else if (message.type === "pull_model") {
const ollamaURL = await getOllamaURL()
2024-03-23 14:44:05 +05:30
const isRunning = await isOllamaRunning()
2024-03-23 14:44:05 +05:30
if (!isRunning) {
2024-05-12 11:38:01 +05:30
setBadgeText({ text: "E" })
setBadgeBackgroundColor({ color: "#FF0000" })
setTitle({ title: "Ollama is not running" })
2024-03-23 14:44:05 +05:30
setTimeout(() => {
clearBadge()
}, 5000)
}
2024-03-23 14:44:05 +05:30
await streamDownload(ollamaURL, message.modelName)
}
})
2024-08-03 23:00:57 +05:30
browser.runtime.onConnect.addListener((port) => {
if (port.name === "pgCopilot") {
isCopilotRunning = true
port.onDisconnect.addListener(() => {
isCopilotRunning = false
})
}
})
2024-05-12 11:38:01 +05:30
if (import.meta.env.BROWSER === "chrome") {
chrome.action.onClicked.addListener((tab) => {
2024-07-14 23:13:48 +05:30
chrome.tabs.create({ url: chrome.runtime.getURL("/options.html") })
})
} else {
browser.browserAction.onClicked.addListener((tab) => {
console.log("browser.browserAction.onClicked.addListener")
browser.tabs.create({ url: browser.runtime.getURL("/options.html") })
})
}
2024-07-14 23:13:48 +05:30
const contextMenuTitle = {
webUi: browser.i18n.getMessage("openOptionToChat"),
sidePanel: browser.i18n.getMessage("openSidePanelToChat")
}
const contextMenuId = {
webUi: "open-web-ui-pa",
sidePanel: "open-side-panel-pa"
}
browser.contextMenus.create({
2024-07-14 23:13:48 +05:30
id: contextMenuId["sidePanel"],
title: contextMenuTitle["sidePanel"],
2024-08-03 23:00:57 +05:30
contexts: ["page", "selection"]
})
browser.contextMenus.create({
id: "summarize-pa",
title: "Summarize",
contexts: ["selection"]
})
browser.contextMenus.create({
id: "explain-pa",
title: "Explain",
contexts: ["selection"]
})
browser.contextMenus.create({
id: "rephrase-pa",
title: "Rephrase",
contexts: ["selection"]
})
2024-08-03 23:00:57 +05:30
browser.contextMenus.create({
id: "translate-pg",
title: "Translate",
contexts: ["selection"]
})
// browser.contextMenus.create({
// id: "custom-pg",
// title: "Custom",
// contexts: ["selection"]
// })
if (import.meta.env.BROWSER === "chrome") {
2024-08-03 23:00:57 +05:30
browser.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === "open-side-panel-pa") {
2024-07-14 23:13:48 +05:30
chrome.sidePanel.open({
tabId: tab.id!
})
} else if (info.menuItemId === "open-web-ui-pa") {
browser.tabs.create({
url: browser.runtime.getURL("/options.html")
})
2024-08-03 23:00:57 +05:30
} else if (info.menuItemId === "summarize-pa") {
chrome.sidePanel.open({
tabId: tab.id!
})
// this is a bad method hope somone can fix it :)
setTimeout(async () => {
await browser.runtime.sendMessage({
from: "background",
type: "summary",
text: info.selectionText
})
}, isCopilotRunning ? 0 : 5000)
} else if (info.menuItemId === "rephrase-pa") {
chrome.sidePanel.open({
tabId: tab.id!
})
setTimeout(async () => {
await browser.runtime.sendMessage({
type: "rephrase",
from: "background",
text: info.selectionText
})
}, isCopilotRunning ? 0 : 5000)
} else if (info.menuItemId === "translate-pg") {
chrome.sidePanel.open({
tabId: tab.id!
})
setTimeout(async () => {
await browser.runtime.sendMessage({
type: "translate",
from: "background",
text: info.selectionText
})
}, isCopilotRunning ? 0 : 5000)
} else if (info.menuItemId === "explain-pa") {
chrome.sidePanel.open({
tabId: tab.id!
})
setTimeout(async () => {
await browser.runtime.sendMessage({
type: "explain",
from: "background",
text: info.selectionText
})
}, isCopilotRunning ? 0 : 5000)
} else if (info.menuItemId === "custom-pg") {
chrome.sidePanel.open({
tabId: tab.id!
})
setTimeout(async () => {
await browser.runtime.sendMessage({
type: "custom",
from: "background",
text: info.selectionText
})
}, isCopilotRunning ? 0 : 5000)
}
})
browser.commands.onCommand.addListener((command) => {
switch (command) {
case "execute_side_panel":
chrome.tabs.query(
{ active: true, currentWindow: true },
async (tabs) => {
const tab = tabs[0]
chrome.sidePanel.open({
tabId: tab.id!
})
}
)
break
default:
break
}
})
}
if (import.meta.env.BROWSER === "firefox") {
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "open-side-panel-pa") {
browser.sidebarAction.toggle()
2024-07-14 23:13:48 +05:30
} else if (info.menuItemId === "open-web-ui-pa") {
browser.tabs.create({
url: browser.runtime.getURL("/options.html")
})
}
})
browser.commands.onCommand.addListener((command) => {
switch (command) {
case "execute_side_panel":
browser.sidebarAction.toggle()
break
default:
break
}
})
}
2024-03-23 14:44:05 +05:30
},
persistent: true
})