diff --git a/package.json b/package.json index f7d9ebd..7f7ac74 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,10 @@ "scripts": { "dev": "cross-env TARGET=chrome wxt", "dev:firefox": "cross-env TARGET=firefox wxt -b firefox", + "dev:edge": "cross-env TARGET=chrome wxt -b edge", "build": "cross-env TARGET=chrome wxt build", "build:firefox": "cross-env TARGET=firefox wxt build -b firefox", + "build:edge": "cross-env TARGET=chrome wxt build -b edge", "zip": "cross-env TARGET=chrome wxt zip", "zip:firefox": "cross-env TARGET=firefox wxt zip -b firefox", "compile": "tsc --noEmit", diff --git a/src/entries/background.ts b/src/entries/background.ts index 8803bf2..885c19a 100644 --- a/src/entries/background.ts +++ b/src/entries/background.ts @@ -35,15 +35,9 @@ export default defineBackground({ } }) - if (import.meta.env.BROWSER === "chrome") { - chrome.action.onClicked.addListener((tab) => { - chrome.tabs.create({ url: chrome.runtime.getURL("/options.html") }) - }) - } else { - browser.browserAction.onClicked.addListener((tab) => { - browser.tabs.create({ url: browser.runtime.getURL("/options.html") }) - }) - } + chrome.action.onClicked.addListener((tab) => { + chrome.tabs.create({ url: chrome.runtime.getURL("/options.html") }) + }) const contextMenuTitle = { webUi: browser.i18n.getMessage("openOptionToChat"), @@ -91,176 +85,98 @@ export default defineBackground({ contexts: ["selection"] }) - if (import.meta.env.BROWSER === "chrome") { - browser.contextMenus.onClicked.addListener(async (info, tab) => { - if (info.menuItemId === "open-side-panel-pa") { - chrome.sidePanel.open({ - tabId: tab.id! + browser.contextMenus.onClicked.addListener(async (info, tab) => { + if (info.menuItemId === "open-side-panel-pa") { + chrome.sidePanel.open({ + tabId: tab.id! + }) + } else if (info.menuItemId === "open-web-ui-pa") { + browser.tabs.create({ + url: browser.runtime.getURL("/options.html") + }) + } 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 }) - } else if (info.menuItemId === "open-web-ui-pa") { - browser.tabs.create({ - url: browser.runtime.getURL("/options.html") + }, 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 }) - } else if (info.menuItemId === "summarize-pa") { - chrome.sidePanel.open({ - tabId: tab.id! + }, 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 }) - // 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) + }, isCopilotRunning ? 0 : 5000) + } else if (info.menuItemId === "explain-pa") { + chrome.sidePanel.open({ + tabId: tab.id! + }) - } else if (info.menuItemId === "rephrase-pa") { - chrome.sidePanel.open({ - tabId: tab.id! + setTimeout(async () => { + await browser.runtime.sendMessage({ + type: "explain", + from: "background", + text: info.selectionText }) - setTimeout(async () => { + }, isCopilotRunning ? 0 : 5000) + } else if (info.menuItemId === "custom-pg") { + chrome.sidePanel.open({ + tabId: tab.id! + }) - 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: "custom", + from: "background", + text: info.selectionText }) + }, isCopilotRunning ? 0 : 5000) + } + }) - 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! - }) + 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 + } + }) - 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() - } else if (info.menuItemId === "open-web-ui-pa") { - browser.tabs.create({ - url: browser.runtime.getURL("/options.html") - }) - } else if (info.menuItemId === "summarize-pa") { - if (!isCopilotRunning) { - browser.sidebarAction.toggle() - } - setTimeout(async () => { - await browser.runtime.sendMessage({ - from: "background", - type: "summary", - text: info.selectionText - }) - }, isCopilotRunning ? 0 : 5000) - } else if (info.menuItemId === "rephrase-pa") { - if (!isCopilotRunning) { - browser.sidebarAction.toggle() - } - setTimeout(async () => { - await browser.runtime.sendMessage({ - type: "rephrase", - from: "background", - text: info.selectionText - }) - }, isCopilotRunning ? 0 : 5000) - } else if (info.menuItemId === "translate-pg") { - if (!isCopilotRunning) { - browser.sidebarAction.toggle() - } - setTimeout(async () => { - await browser.runtime.sendMessage({ - type: "translate", - from: "background", - text: info.selectionText - }) - }, isCopilotRunning ? 0 : 5000) - } else if (info.menuItemId === "explain-pa") { - if (!isCopilotRunning) { - browser.sidebarAction.toggle() - } - setTimeout(async () => { - await browser.runtime.sendMessage({ - type: "explain", - from: "background", - text: info.selectionText - }) - }, isCopilotRunning ? 0 : 5000) - } else if (info.menuItemId === "custom-pg") { - if (!isCopilotRunning) { - browser.sidebarAction.toggle() - } - 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": - browser.sidebarAction.toggle() - break - default: - break - } - }) - } }, persistent: true }) diff --git a/src/hooks/useTTS.tsx b/src/hooks/useTTS.tsx index 6573ec4..9ac8607 100644 --- a/src/hooks/useTTS.tsx +++ b/src/hooks/useTTS.tsx @@ -32,7 +32,10 @@ export const useTTS = () => { if (isSSML) { utterance = markdownToSSML(utterance) } - if (import.meta.env.BROWSER === "chrome") { + if ( + import.meta.env.BROWSER === "chrome" || + import.meta.env.BROWSER === "edge" + ) { chrome.tts.speak(utterance, { voiceName: voice, onEvent(event) { @@ -68,7 +71,7 @@ export const useTTS = () => { let currentAudioData = nextAudioData || (await generateSpeech(apiKey, sentences[i], voiceId, modelId)) - + if (i < sentences.length - 1) { generateSpeech(apiKey, sentences[i + 1], voiceId, modelId) .then((nextAudioData) => { @@ -112,7 +115,7 @@ export const useTTS = () => { return } - if (import.meta.env.BROWSER === "chrome") { + if (import.meta.env.BROWSER === "chrome" || import.meta.env.BROWSER === "edge") { chrome.tts.stop() } else { window.speechSynthesis.cancel() diff --git a/src/libs/get-html.ts b/src/libs/get-html.ts index c1aa304..0c02071 100644 --- a/src/libs/get-html.ts +++ b/src/libs/get-html.ts @@ -24,7 +24,7 @@ const _getHtml = () => { export const getDataFromCurrentTab = async () => { const result = new Promise((resolve) => { - if (import.meta.env.BROWSER === "chrome") { + if (import.meta.env.BROWSER === "chrome" || import.meta.env.BROWSER === "edge") { chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => { const tab = tabs[0] diff --git a/src/libs/get-screenshot.ts b/src/libs/get-screenshot.ts index d3cbb33..bcaf67d 100644 --- a/src/libs/get-screenshot.ts +++ b/src/libs/get-screenshot.ts @@ -1,6 +1,6 @@ const captureVisibleTab = () => { const result = new Promise((resolve) => { - if (import.meta.env.BROWSER === "chrome") { + if (import.meta.env.BROWSER === "chrome" || import.meta.env.BROWSER === "edge") { chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => { const tab = tabs[0] chrome.tabs.captureVisibleTab(null, { format: "png" }, (dataUrl) => { diff --git a/src/libs/runtime.ts b/src/libs/runtime.ts index dc54c4a..63b1f4b 100644 --- a/src/libs/runtime.ts +++ b/src/libs/runtime.ts @@ -6,7 +6,7 @@ export const urlRewriteRuntime = async function ( ) { if (browser.runtime && browser.runtime.id) { const { isEnableRewriteUrl, rewriteUrl } = await getAdvancedOllamaSettings() - if (import.meta.env.BROWSER === "chrome") { + if (import.meta.env.BROWSER === "chrome" || import.meta.env.BROWSER === "edge") { const url = new URL(domain) const domains = [url.hostname] let origin = `${url.protocol}//${url.hostname}` diff --git a/src/parser/google-docs.ts b/src/parser/google-docs.ts index 0781396..20f8ef2 100644 --- a/src/parser/google-docs.ts +++ b/src/parser/google-docs.ts @@ -95,7 +95,7 @@ const getGoogleDocs = () => { export const parseGoogleDocs = async () => { const result = new Promise((resolve) => { - if (import.meta.env.BROWSER === "chrome") { + if (import.meta.env.BROWSER === "chrome" || import.meta.env.BROWSER === "edge") { chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => { const tab = tabs[0] diff --git a/src/services/tts.ts b/src/services/tts.ts index ace7b05..98b8a40 100644 --- a/src/services/tts.ts +++ b/src/services/tts.ts @@ -21,7 +21,7 @@ export const setTTSProvider = async (ttsProvider: string) => { } export const getBrowserTTSVoices = async () => { - if (import.meta.env.BROWSER === "chrome") { + if (import.meta.env.BROWSER === "chrome" || import.meta.env.BROWSER === "edge") { const tts = await chrome.tts.getVoices() return tts } else { diff --git a/src/utils/action.ts b/src/utils/action.ts index 5881aa4..fec2343 100644 --- a/src/utils/action.ts +++ b/src/utils/action.ts @@ -1,7 +1,7 @@ import { browser } from "wxt/browser" export const setTitle = ({ title }: { title: string }) => { - if (import.meta.env.BROWSER === "chrome") { + if (import.meta.env.BROWSER === "chrome" || import.meta.env.BROWSER === "edge") { chrome.action.setTitle({ title }) } else { browser.browserAction.setTitle({ title }) @@ -9,7 +9,7 @@ export const setTitle = ({ title }: { title: string }) => { } export const setBadgeBackgroundColor = ({ color }: { color: string }) => { - if (import.meta.env.BROWSER === "chrome") { + if (import.meta.env.BROWSER === "chrome" || import.meta.env.BROWSER === "edge") { chrome.action.setBadgeBackgroundColor({ color }) } else { browser.browserAction.setBadgeBackgroundColor({ color }) @@ -17,7 +17,7 @@ export const setBadgeBackgroundColor = ({ color }: { color: string }) => { } export const setBadgeText = ({ text }: { text: string }) => { - if (import.meta.env.BROWSER === "chrome") { + if (import.meta.env.BROWSER === "chrome" || import.meta.env.BROWSER === "edge") { chrome.action.setBadgeText({ text }) } else { browser.browserAction.setBadgeText({ text }) diff --git a/src/utils/ocr.ts b/src/utils/ocr.ts index e56a9ab..7b43b74 100644 --- a/src/utils/ocr.ts +++ b/src/utils/ocr.ts @@ -1,17 +1,18 @@ -import { createWorker } from 'tesseract.js'; +import { createWorker } from "tesseract.js" export async function processImageForOCR(imageData: string): Promise { - const worker = await createWorker('eng-fast', undefined, { - workerPath: "/ocr/worker.min.js", - workerBlobURL: false, - corePath: "/ocr/tesseract-core-simd.js", - errorHandler: e => console.error(e), - langPath: "/ocr/lang" - }); + const isOCROffline = import.meta.env.BROWSER === "edge" + const worker = await createWorker(!isOCROffline ? "eng-fast" : "eng", undefined, { + workerPath: "/ocr/worker.min.js", + workerBlobURL: false, + corePath: "/ocr/tesseract-core-simd.js", + errorHandler: (e) => console.error(e), + langPath: !isOCROffline ? "/ocr/lang" : undefined + }) - const result = await worker.recognize(imageData); + const result = await worker.recognize(imageData) - await worker.terminate(); + await worker.terminate() - return result.data.text; + return result.data.text }