Merge pull request #63 from n4ze3m/firefox

Firefox support
This commit is contained in:
Muhammed Nazeem 2024-05-12 11:59:05 +05:30 committed by GitHub
commit bd84b90e5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 330 additions and 161 deletions

3
.gitignore vendored
View File

@ -42,4 +42,7 @@ keys.json
# typescript # typescript
.tsbuildinfo .tsbuildinfo
# WXT
.wxt .wxt
# WebStorm
.idea

BIN
bun.lockb

Binary file not shown.

View File

@ -5,12 +5,12 @@
"description": "Use your locally running AI models to assist you in your web browsing.", "description": "Use your locally running AI models to assist you in your web browsing.",
"author": "n4ze3m", "author": "n4ze3m",
"scripts": { "scripts": {
"dev": "wxt", "dev": "cross-env TARGET=chrome wxt",
"dev:firefox": "wxt -b firefox", "dev:firefox": "cross-env TARGET=firefox wxt -b firefox",
"build": "wxt build", "build": "cross-env TARGET=chrome wxt build",
"build:firefox": "wxt build -b firefox", "build:firefox": "cross-env TARGET=chrome cross-env TARGET=firefox wxt build -b firefox",
"zip": "wxt zip", "zip": "cross-env TARGET=chrome wxt zip",
"zip:firefox": "wxt zip -b firefox", "zip:firefox": "cross-env TARGET=firefox wxt zip -b firefox",
"compile": "tsc --noEmit", "compile": "tsc --noEmit",
"postinstall": "wxt prepare" "postinstall": "wxt prepare"
}, },
@ -66,6 +66,7 @@
"@types/react-syntax-highlighter": "^15.5.11", "@types/react-syntax-highlighter": "^15.5.11",
"@types/turndown": "^5.0.4", "@types/turndown": "^5.0.4",
"autoprefixer": "^10.4.17", "autoprefixer": "^10.4.17",
"cross-env": "^7.0.3",
"postcss": "^8.4.33", "postcss": "^8.4.33",
"prettier": "3.2.4", "prettier": "3.2.4",
"tailwindcss": "^3.4.1", "tailwindcss": "^3.4.1",

View File

@ -23,7 +23,7 @@ Click the button below to deploy the code to Railway.
```bash ```bash
git clone https://github.com/n4ze3m/page-share-app.git git clone https://github.com/n4ze3m/page-share-app.git
cd page-assist-app cd page-share-app
``` ```
2. Run the server 2. Run the server

View File

@ -60,7 +60,7 @@ export const ModelsBody = () => {
form.reset() form.reset()
chrome.runtime.sendMessage({ browser.runtime.sendMessage({
type: "pull_model", type: "pull_model",
modelName modelName
}) })

View File

@ -11,7 +11,7 @@ export const AboutApp = () => {
const { data, status } = useQuery({ const { data, status } = useQuery({
queryKey: ["fetchOllamURL"], queryKey: ["fetchOllamURL"],
queryFn: async () => { queryFn: async () => {
const chromeVersion = chrome.runtime.getManifest().version const chromeVersion = browser.runtime.getManifest().version
try { try {
const url = await getOllamaURL() const url = await getOllamaURL()
const req = await fetch(`${cleanUrl(url)}/api/version`) const req = await fetch(`${cleanUrl(url)}/api/version`)

View File

@ -1,13 +1,13 @@
import { getOllamaURL, isOllamaRunning } from "../services/ollama" import { getOllamaURL, isOllamaRunning } from "../services/ollama"
import { Storage } from "@plasmohq/storage" import { browser } from "wxt/browser"
import { setBadgeBackgroundColor, setBadgeText, setTitle } from "@/utils/action"
const progressHuman = (completed: number, total: number) => { const progressHuman = (completed: number, total: number) => {
return ((completed / total) * 100).toFixed(0) + "%" return ((completed / total) * 100).toFixed(0) + "%"
} }
const clearBadge = () => { const clearBadge = () => {
chrome.action.setBadgeText({ text: "" }) setBadgeText({ text: "" })
chrome.action.setTitle({ title: "" }) setTitle({ title: "" })
} }
const streamDownload = async (url: string, model: string) => { const streamDownload = async (url: string, model: string) => {
url += "/api/pull" url += "/api/pull"
@ -42,16 +42,16 @@ const streamDownload = async (url: string, model: string) => {
completed?: number completed?: number
} }
if (json.total && json.completed) { if (json.total && json.completed) {
chrome.action.setBadgeText({ setBadgeText({
text: progressHuman(json.completed, json.total) text: progressHuman(json.completed, json.total)
}) })
chrome.action.setBadgeBackgroundColor({ color: "#0000FF" }) setBadgeBackgroundColor({ color: "#0000FF" })
} else { } else {
chrome.action.setBadgeText({ text: "🏋️‍♂️" }) setBadgeText({ text: "🏋️‍♂️" })
chrome.action.setBadgeBackgroundColor({ color: "#FFFFFF" }) setBadgeBackgroundColor({ color: "#FFFFFF" })
} }
chrome.action.setTitle({ title: json.status }) setTitle({ title: json.status })
if (json.status === "success") { if (json.status === "success") {
isSuccess = true isSuccess = true
@ -62,13 +62,13 @@ const streamDownload = async (url: string, model: string) => {
} }
if (isSuccess) { if (isSuccess) {
chrome.action.setBadgeText({ text: "✅" }) setBadgeText({ text: "✅" })
chrome.action.setBadgeBackgroundColor({ color: "#00FF00" }) setBadgeBackgroundColor({ color: "#00FF00" })
chrome.action.setTitle({ title: "Model pulled successfully" }) setTitle({ title: "Model pulled successfully" })
} else { } else {
chrome.action.setBadgeText({ text: "❌" }) setBadgeText({ text: "❌" })
chrome.action.setBadgeBackgroundColor({ color: "#FF0000" }) setBadgeBackgroundColor({ color: "#FF0000" })
chrome.action.setTitle({ title: "Model pull failed" }) setTitle({ title: "Model pull failed" })
} }
setTimeout(() => { setTimeout(() => {
@ -77,29 +77,18 @@ const streamDownload = async (url: string, model: string) => {
} }
export default defineBackground({ export default defineBackground({
main() { main() {
const storage = new Storage() browser.runtime.onMessage.addListener(async (message) => {
chrome.runtime.onMessage.addListener(async (message) => {
if (message.type === "sidepanel") { if (message.type === "sidepanel") {
chrome.tabs.query( browser.sidebarAction.open()
{ active: true, currentWindow: true },
async (tabs) => {
const tab = tabs[0]
chrome.sidePanel.open({
// tabId: tab.id!,
windowId: tab.windowId!
})
}
)
} else if (message.type === "pull_model") { } else if (message.type === "pull_model") {
const ollamaURL = await getOllamaURL() const ollamaURL = await getOllamaURL()
const isRunning = await isOllamaRunning() const isRunning = await isOllamaRunning()
if (!isRunning) { if (!isRunning) {
chrome.action.setBadgeText({ text: "E" }) setBadgeText({ text: "E" })
chrome.action.setBadgeBackgroundColor({ color: "#FF0000" }) setBadgeBackgroundColor({ color: "#FF0000" })
chrome.action.setTitle({ title: "Ollama is not running" }) setTitle({ title: "Ollama is not running" })
setTimeout(() => { setTimeout(() => {
clearBadge() clearBadge()
}, 5000) }, 5000)
@ -109,35 +98,24 @@ export default defineBackground({
} }
}) })
if (import.meta.env.BROWSER === "chrome") {
chrome.action.onClicked.addListener((tab) => { chrome.action.onClicked.addListener((tab) => {
chrome.tabs.create({ url: chrome.runtime.getURL("options.html") }) browser.tabs.create({ url: browser.runtime.getURL("/options.html") })
}) })
} else {
chrome.commands.onCommand.addListener((command) => { browser.browserAction.onClicked.addListener((tab) => {
switch (command) { console.log("browser.browserAction.onClicked.addListener")
case "execute_side_panel": browser.tabs.create({ url: browser.runtime.getURL("/options.html") })
chrome.tabs.query(
{ active: true, currentWindow: true },
async (tabs) => {
const tab = tabs[0]
chrome.sidePanel.open({
windowId: tab.windowId!
}) })
} }
)
break
default:
break
}
})
chrome.contextMenus.create({ browser.contextMenus.create({
id: "open-side-panel-pa", id: "open-side-panel-pa",
title: browser.i18n.getMessage("openSidePanelToChat"), title: browser.i18n.getMessage("openSidePanelToChat"),
contexts: ["all"] contexts: ["all"]
}) })
if (import.meta.env.BROWSER === "chrome") {
chrome.contextMenus.onClicked.addListener((info, tab) => { browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "open-side-panel-pa") { if (info.menuItemId === "open-side-panel-pa") {
chrome.tabs.query( chrome.tabs.query(
{ active: true, currentWindow: true }, { active: true, currentWindow: true },
@ -150,6 +128,43 @@ export default defineBackground({
) )
} }
}) })
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()
}
})
browser.commands.onCommand.addListener((command) => {
switch (command) {
case "execute_side_panel":
browser.sidebarAction.toggle()
break
default:
break
}
})
}
}, },
persistent: true persistent: true
}) })

View File

@ -9,7 +9,7 @@ export default defineContentScript({
`[Page Assist Extension] Pulling ${modelName} model. For more details, check the extension icon.` `[Page Assist Extension] Pulling ${modelName} model. For more details, check the extension icon.`
) )
await chrome.runtime.sendMessage({ await browser.runtime.sendMessage({
type: "pull_model", type: "pull_model",
modelName modelName
}) })

View File

@ -4,6 +4,7 @@
<title>Page Assist - A Web UI for Local AI Models</title> <title>Page Assist - A Web UI for Local AI Models</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="manifest.type" content="browser_action" /> <meta name="manifest.type" content="browser_action" />
<meta name="manifest.open_at_install" content="false" />
<link href="~/assets/tailwind.css" rel="stylesheet" /> <link href="~/assets/tailwind.css" rel="stylesheet" />
<meta charset="utf-8" /> <meta charset="utf-8" />
</head> </head>

View File

@ -2,7 +2,6 @@ import { useEffect, useState } from "react"
import { notification } from "antd" import { notification } from "antd"
import { getVoice, isSSMLEnabled } from "@/services/tts" import { getVoice, isSSMLEnabled } from "@/services/tts"
import { markdownToSSML } from "@/utils/markdown-to-ssml" import { markdownToSSML } from "@/utils/markdown-to-ssml"
type VoiceOptions = { type VoiceOptions = {
utterance: string utterance: string
} }
@ -17,6 +16,7 @@ export const useTTS = () => {
if (isSSML) { if (isSSML) {
utterance = markdownToSSML(utterance) utterance = markdownToSSML(utterance)
} }
if (import.meta.env.BROWSER === "chrome") {
chrome.tts.speak(utterance, { chrome.tts.speak(utterance, {
voiceName: voice, voiceName: voice,
onEvent(event) { onEvent(event) {
@ -27,6 +27,17 @@ export const useTTS = () => {
} }
} }
}) })
} else {
// browser tts
window.speechSynthesis.speak(new SpeechSynthesisUtterance(utterance))
window.speechSynthesis.onvoiceschanged = () => {
const voices = window.speechSynthesis.getVoices()
const voice = voices.find((v) => v.name === voice)
const utter = new SpeechSynthesisUtterance(utterance)
utter.voice = voice
window.speechSynthesis.speak(utter)
}
}
} catch (error) { } catch (error) {
notification.error({ notification.error({
message: "Error", message: "Error",
@ -36,7 +47,11 @@ export const useTTS = () => {
} }
const cancel = () => { const cancel = () => {
if (import.meta.env.BROWSER === "chrome") {
chrome.tts.stop() chrome.tts.stop()
} else {
window.speechSynthesis.cancel()
}
setIsSpeaking(false) setIsSpeaking(false)
} }

View File

@ -4,7 +4,7 @@ import {
isTweet, isTweet,
isTwitterTimeline, isTwitterTimeline,
parseTweet, parseTweet,
parseTwitterTimeline, parseTwitterTimeline
} from "@/parser/twitter" } from "@/parser/twitter"
import { isGoogleDocs, parseGoogleDocs } from "@/parser/google-docs" import { isGoogleDocs, parseGoogleDocs } from "@/parser/google-docs"
import { cleanUnwantedUnicode } from "@/utils/clean" import { cleanUnwantedUnicode } from "@/utils/clean"
@ -24,6 +24,7 @@ const _getHtml = () => {
export const getDataFromCurrentTab = async () => { export const getDataFromCurrentTab = async () => {
const result = new Promise((resolve) => { const result = new Promise((resolve) => {
if (import.meta.env.BROWSER === "chrome") {
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => { chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
const tab = tabs[0] const tab = tabs[0]
@ -36,6 +37,22 @@ export const getDataFromCurrentTab = async () => {
resolve(data[0].result) resolve(data[0].result)
} }
}) })
} else {
browser.tabs
.query({ active: true, currentWindow: true })
.then(async (tabs) => {
const tab = tabs[0]
const data = await browser.scripting.executeScript({
target: { tabId: tab.id },
func: _getHtml
})
if (data.length > 0) {
resolve(data[0].result)
}
})
}
}) as Promise<{ }) as Promise<{
url: string url: string
content: string content: string

View File

@ -1,5 +1,6 @@
export const chromeRunTime = async function (domain: string) { export const chromeRunTime = async function (domain: string) {
if (typeof chrome !== "undefined" && chrome.runtime && chrome.runtime.id) { if (browser.runtime && browser.runtime.id) {
if (import.meta.env.BROWSER === "chrome") {
const url = new URL(domain) const url = new URL(domain)
const domains = [url.hostname] const domains = [url.hostname]
const rules = [ const rules = [
@ -22,10 +23,29 @@ export const chromeRunTime = async function (domain: string) {
} }
] ]
await chrome.declarativeNetRequest.updateDynamicRules({ await browser.declarativeNetRequest.updateDynamicRules({
removeRuleIds: rules.map((r) => r.id), removeRuleIds: rules.map((r) => r.id),
// @ts-ignore // @ts-ignore
addRules: rules addRules: rules
}) })
} }
if (import.meta.env.BROWSER === "firefox") {
const url = new URL(domain)
const domains = [`*://${url.hostname}/*`]
browser.webRequest.onBeforeSendHeaders.addListener(
(details) => {
for (let i = 0; i < details.requestHeaders.length; i++) {
if (details.requestHeaders[i].name === "Origin") {
details.requestHeaders[i].value =
`${url.protocol}//${url.hostname}`
}
}
return { requestHeaders: details.requestHeaders }
},
{ urls: domains },
["blocking", "requestHeaders"]
)
}
}
} }

View File

@ -1,4 +1,3 @@
export const isGoogleDocs = (url: string) => { export const isGoogleDocs = (url: string) => {
const GOOGLE_DOCS_REGEX = /docs\.google\.com\/document/g const GOOGLE_DOCS_REGEX = /docs\.google\.com\/document/g
return GOOGLE_DOCS_REGEX.test(url) return GOOGLE_DOCS_REGEX.test(url)
@ -96,6 +95,7 @@ const getGoogleDocs = () => {
export const parseGoogleDocs = async () => { export const parseGoogleDocs = async () => {
const result = new Promise((resolve) => { const result = new Promise((resolve) => {
if (import.meta.env.BROWSER === "chrome") {
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => { chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
const tab = tabs[0] const tab = tabs[0]
@ -109,6 +109,22 @@ export const parseGoogleDocs = async () => {
resolve(data[0].result) resolve(data[0].result)
} }
}) })
} else {
browser.tabs
.query({ active: true, currentWindow: true })
.then(async (tabs) => {
const tab = tabs[0]
const data = await browser.scripting.executeScript({
target: { tabId: tab.id },
func: getGoogleDocs
})
if (data.length > 0) {
resolve(data[0].result)
}
})
}
}) as Promise<{ }) as Promise<{
content?: string content?: string
}> }>

20
src/services/app.ts Normal file
View File

@ -0,0 +1,20 @@
import { Storage } from "@plasmohq/storage"
const storage = new Storage()
export const isUrlRewriteEnabled = async () => {
const enabled = await storage.get("urlRewriteEnabled")
return enabled === "true"
}
export const setUrlRewriteEnabled = async (enabled: boolean) => {
await storage.set("urlRewriteEnabled", enabled ? "true" : "false")
}
export const getRewriteUrl = async () => {
const rewriteUrl = await storage.get("rewriteUrl")
return rewriteUrl
}
export const setRewriteUrl = async (url: string) => {
await storage.set("rewriteUrl", url)
}

View File

@ -21,8 +21,16 @@ export const setTTSProvider = async (ttsProvider: string) => {
} }
export const getBrowserTTSVoices = async () => { export const getBrowserTTSVoices = async () => {
if (import.meta.env.BROWSER === "chrome") {
const tts = await chrome.tts.getVoices() const tts = await chrome.tts.getVoices()
return tts return tts
} else {
const tts = await speechSynthesis.getVoices()
return tts.map((voice) => ({
voiceName: voice.name,
lang: voice.lang
}))
}
} }
export const getVoice = async () => { export const getVoice = async () => {

25
src/utils/action.ts Normal file
View File

@ -0,0 +1,25 @@
import { browser } from "wxt/browser"
export const setTitle = ({ title }: { title: string }) => {
if (import.meta.env.BROWSER === "chrome") {
chrome.action.setTitle({ title })
} else {
browser.browserAction.setTitle({ title })
}
}
export const setBadgeBackgroundColor = ({ color }: { color: string }) => {
if (import.meta.env.BROWSER === "chrome") {
chrome.action.setBadgeBackgroundColor({ color })
} else {
browser.browserAction.setBadgeBackgroundColor({ color })
}
}
export const setBadgeText = ({ text }: { text: string }) => {
if (import.meta.env.BROWSER === "chrome") {
chrome.action.setBadgeText({ text })
} else {
browser.browserAction.setBadgeText({ text })
}
}

View File

@ -2,35 +2,70 @@ import { defineConfig } from "wxt"
import react from "@vitejs/plugin-react" import react from "@vitejs/plugin-react"
import topLevelAwait from "vite-plugin-top-level-await" import topLevelAwait from "vite-plugin-top-level-await"
const chromeMV3Permissions = [
"storage",
"sidePanel",
"activeTab",
"scripting",
"declarativeNetRequest",
"action",
"unlimitedStorage",
"contextMenus",
"tts"
]
const firefoxMV2Permissions = [
"storage",
"activeTab",
"scripting",
"unlimitedStorage",
"contextMenus",
"webRequest",
"webRequestBlocking",
"http://*/*",
"https://*/*",
"file://*/*"
]
// See https://wxt.dev/api/config.html // See https://wxt.dev/api/config.html
export default defineConfig({ export default defineConfig({
vite: () => ({ vite: () => ({
plugins: [react(), plugins: [
react(),
topLevelAwait({ topLevelAwait({
promiseExportName: '__tla', promiseExportName: "__tla",
promiseImportName: i => `__tla_${i}`, promiseImportName: (i) => `__tla_${i}`
}), })
], ],
build: { build: {
rollupOptions: { rollupOptions: {
external: [ external: ["langchain", "@langchain/community"]
"langchain",
"@langchain/community",
]
} }
} }
}), }),
entrypointsDir: "entries", entrypointsDir: "entries",
srcDir: "src", srcDir: "src",
outDir: "build", outDir: "build",
manifest: { manifest: {
version: "1.1.6", version: "1.1.7",
name: '__MSG_extName__', name: "__MSG_extName__",
description: '__MSG_extDescription__', description: "__MSG_extDescription__",
default_locale: 'en', default_locale: "en",
action: {}, action: {},
author: "n4ze3m", author: "n4ze3m",
host_permissions: ["http://*/*", "https://*/*", "file://*/*"], browser_specific_settings:
process.env.TARGET === "firefox"
? {
gecko: {
id: "page-assist@n4ze3m"
}
}
: undefined,
host_permissions:
process.env.TARGET !== "firefox"
? ["http://*/*", "https://*/*", "file://*/*"]
: undefined,
commands: { commands: {
_execute_action: { _execute_action: {
suggested_key: { suggested_key: {
@ -44,16 +79,9 @@ export default defineConfig({
} }
} }
}, },
permissions: [ permissions:
"storage", process.env.TARGET === "firefox"
"sidePanel", ? firefoxMV2Permissions
"activeTab", : chromeMV3Permissions
"scripting",
"declarativeNetRequest",
"action",
"unlimitedStorage",
"contextMenus",
"tts"
]
} }
}) })