Skip to content

Commit 66c854a

Browse files
committed
Implement Firefox compatibility for retrieving active tab HTML content and remove unnecessary comments from background scripts
1 parent f688f2b commit 66c854a

6 files changed

Lines changed: 25 additions & 56 deletions

File tree

‎packages/browser/create-firefox-build.js‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
11
const fs = require('fs')
22
const path = require('path')
33

4-
// Read the original manifest.json
54
const manifest_path = path.join(__dirname, 'src', 'manifest.json')
65
const manifest = JSON.parse(fs.readFileSync(manifest_path, 'utf8'))
76

8-
// Change manifest version
97
const firefox_manifest = { ...manifest }
108
delete firefox_manifest.manifest_version
119
firefox_manifest.manifest_version = 2
1210

13-
// Replace service_worker with background.scripts
1411
if (firefox_manifest.background && firefox_manifest.background.service_worker) {
1512
firefox_manifest.background = {
1613
scripts: [firefox_manifest.background.service_worker],
1714
persistent: true
1815
}
1916
}
2017

21-
// Replace action with browser action
2218
firefox_manifest.browser_action = firefox_manifest.action
2319
delete firefox_manifest.action
2420

25-
// Filter not relevant permissions
2621
firefox_manifest.permissions = firefox_manifest.permissions.filter(
2722
(p) => p != 'alarms'
2823
)
2924

3025
delete firefox_manifest.host_permissions
3126

32-
// Create dist-firefox directory if it doesn't exist
3327
const firefox_dist_dir = path.join(__dirname, 'dist-firefox')
3428
if (!fs.existsSync(firefox_dist_dir)) {
3529
fs.mkdirSync(firefox_dist_dir)
3630
}
3731

38-
// Copy the contents of the dist directory to dist-firefox
3932
const dist_dir = path.join(__dirname, 'dist')
4033
fs.cpSync(dist_dir, firefox_dist_dir, { recursive: true })
4134

42-
// Write the modified manifest to manifest.json in dist-firefox
4335
const firefox_manifest_path = path.join(
4436
__dirname,
4537
'dist-firefox',

‎packages/browser/src/background/clear-chat-init-data.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import browser from 'webextension-polyfill'
22

3-
/**
4-
* Clear leftover chat initialization data
5-
*/
63
export const clear_chat_init_data = async () => {
74
try {
85
const items = await browser.storage.local.get()

‎packages/browser/src/background/config.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Central configuration file for shared constants
2-
31
export const CONFIG = {
42
// WebSocket configuration
53
RECONNECT_DELAY: 5000 // 5 seconds

‎packages/browser/src/background/keep-alive.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import browser from 'webextension-polyfill'
22

3-
/**
4-
* Initialize the keepalive mechanism for Chromium browsers
5-
*/
63
export function setup_keep_alive() {
7-
// Keep alive with alarms in chromium browsers
84
if (!browser.browserAction) {
95
const create_keep_alive_alarm = async () => {
106
try {

‎packages/browser/src/background/message-handler.ts‎

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ const process_next_chat = async () => {
6363

6464
const current_queue_item = chat_queue[0]
6565

66-
// Clear any existing timeout
6766
if (current_queue_item.timeout_id) {
6867
clearTimeout(current_queue_item.timeout_id)
6968
current_queue_item.timeout_id = undefined
@@ -72,11 +71,9 @@ const process_next_chat = async () => {
7271
if (
7372
current_queue_item.current_index >= current_queue_item.message.chats.length
7473
) {
75-
// Current queue item is complete, remove it and process the next one
7674
chat_queue.shift()
7775

7876
if (chat_queue.length > 0) {
79-
// Start processing the next queue item
8077
chat_queue[0].current_index = 0
8178
await process_next_chat()
8279
} else {
@@ -88,10 +85,8 @@ const process_next_chat = async () => {
8885
const current_chat =
8986
current_queue_item.message.chats[current_queue_item.current_index]
9087

91-
// Generate a unique 3-character alphanumeric batch ID
9288
const batch_id = await generate_alphanumeric_id('chat-init')
9389

94-
// Store only the relevant information - the text and the current chat configuration
9590
await browser.storage.local.set({
9691
[`chat-init:${batch_id}`]: {
9792
text: current_queue_item.message.text,
@@ -118,14 +113,12 @@ const process_next_chat = async () => {
118113
active: true
119114
})
120115
} else {
121-
// Open the tab with the current chat URL
122116
browser.tabs.create({
123117
url: `${current_chat.url}#cwc-${batch_id}`,
124118
active: true
125119
})
126120
}
127121

128-
// Increment the current index for the next chat
129122
current_queue_item.current_index++
130123

131124
// Set a timeout to automatically proceed if no confirmation is received
@@ -150,14 +143,12 @@ const handle_initialize_chats_message = async (
150143
message: InitializeChatsMessage
151144
) => {
152145
if (message.chats && message.chats.length > 0) {
153-
// Add the new request to the queue
154146
chat_queue.push({
155147
message,
156148
remaining_chats: message.chats.length,
157149
current_index: 0
158150
})
159151

160-
// Start processing if not already doing so
161152
await start_processing()
162153
}
163154
}
@@ -191,9 +182,7 @@ const handle_initialize_chat_message = async (
191182
}
192183

193184
const handle_chat_initialized = async () => {
194-
// Process the next chat in the queue if one exists
195185
if (chat_queue.length > 0) {
196-
// Clear the timeout since we received confirmation
197186
if (chat_queue[0].timeout_id) {
198187
clearTimeout(chat_queue[0].timeout_id)
199188
chat_queue[0].timeout_id = undefined
@@ -209,19 +198,37 @@ const handle_get_tab_data = async (
209198
) => {
210199
try {
211200
const tabs = await browser.tabs.query({ active: true, currentWindow: true })
212-
const url = tabs[0]?.url
201+
const tab = tabs[0]
213202
const favicon_url = tabs[0]?.favIconUrl
214203

215-
if (!url || !url.startsWith('http')) {
204+
if (!tab?.url || !tab.url.startsWith('http')) {
216205
throw new Error('URL is not valid')
217206
}
218207

219208
let html = ''
220-
const response = await fetch(url)
221-
if (!response.ok) {
222-
throw new Error(`HTTP error! Status: ${response.status}`)
209+
210+
// (Firefox uses browserAction, Chrome uses action)
211+
const is_firefox = typeof browser.browserAction !== 'undefined'
212+
213+
if (is_firefox && tab.id) {
214+
// Firefox: Use the older tabs.executeScript API (MV2 style)
215+
const results = await browser.tabs.executeScript(tab.id, {
216+
code: 'document.documentElement.outerHTML'
217+
})
218+
219+
if (!results || results.length === 0 || !results[0]) {
220+
throw new Error('Failed to get page content')
221+
}
222+
223+
html = results[0] as string
224+
} else {
225+
// Chrome: Use fetch
226+
const response = await fetch(tab.url)
227+
if (!response.ok) {
228+
throw new Error(`HTTP error! Status: ${response.status}`)
229+
}
230+
html = await response.text()
223231
}
224-
html = await response.text()
225232

226233
let favicon_base64: string | undefined
227234
if (favicon_url) {
@@ -264,7 +271,7 @@ export const setup_message_listeners = () => {
264271
return true
265272
}
266273
}
267-
return false // For messages that don't need a response
274+
return false
268275
}
269276
)
270277
}

‎packages/browser/src/background/websocket.ts‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ import browser from 'webextension-polyfill'
66
import type { Website } from '@ui/components/browser/SavedWebsites'
77
import localforage from 'localforage'
88

9-
// Store WebSocket instance and connection state
109
let websocket: WebSocket | null = null
1110
let is_reconnecting = false
1211

13-
/**
14-
* Check if the server is healthy before attempting connection
15-
*/
1612
export const check_server_health = async (): Promise<boolean> => {
1713
try {
1814
const response = await fetch(`http://localhost:${DEFAULT_PORT}/health`)
@@ -22,19 +18,14 @@ export const check_server_health = async (): Promise<boolean> => {
2218
}
2319
}
2420

25-
/**
26-
* Connect to WebSocket server with reconnection logic
27-
*/
2821
export const connect_websocket = async (): Promise<void> => {
29-
// Prevent concurrent reconnection attempts
3022
if (is_reconnecting || websocket?.readyState === WebSocket.OPEN) {
3123
return
3224
}
3325

3426
is_reconnecting = true
3527

3628
try {
37-
// Check server health before attempting WebSocket connection
3829
const is_healthy = await check_server_health()
3930
if (!is_healthy) {
4031
console.debug('Server health check failed, retrying in 5 seconds...')
@@ -45,7 +36,6 @@ export const connect_websocket = async (): Promise<void> => {
4536
return
4637
}
4738

48-
// Get manifest data for version
4939
const manifest = browser.runtime.getManifest()
5040
const version = manifest.version
5141

@@ -57,7 +47,6 @@ export const connect_websocket = async (): Promise<void> => {
5747
console.log('Connected with the VS Code!')
5848
is_reconnecting = false
5949

60-
// Send any saved websites immediately after connection is established
6150
send_current_saved_websites()
6251
}
6352

@@ -84,9 +73,6 @@ export const connect_websocket = async (): Promise<void> => {
8473
}
8574
}
8675

87-
/**
88-
* Send saved websites to the WebSocket server
89-
*/
9076
export const send_saved_websites = (websites: Website[]): boolean => {
9177
if (websocket?.readyState == WebSocket.OPEN) {
9278
const websites_to_send = websites.map((site) => ({
@@ -108,9 +94,6 @@ export const send_saved_websites = (websites: Website[]): boolean => {
10894
return false
10995
}
11096

111-
/**
112-
* Send a generic message to the WebSocket server
113-
*/
11497
export const send_message_to_server = (message: any): boolean => {
11598
if (websocket?.readyState == WebSocket.OPEN) {
11699
console.debug('Sending message to server:', message)
@@ -121,12 +104,8 @@ export const send_message_to_server = (message: any): boolean => {
121104
return false
122105
}
123106

124-
/**
125-
* Retrieve and send current saved websites
126-
*/
127107
const send_current_saved_websites = async (): Promise<void> => {
128108
try {
129-
// Create a localforage instance with the same config as in use-websites-store.ts
130109
const websites_store = localforage.createInstance({
131110
name: 'gemini-coder-connector',
132111
storeName: 'websites'

0 commit comments

Comments
 (0)