Skip to content

Commit 746eea9

Browse files
committed
Add Firefox Container support for autofilling prompts in isolated contexts
1 parent 225f613 commit 746eea9

6 files changed

Lines changed: 193 additions & 18 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ firefox_manifest.permissions = firefox_manifest.permissions.filter(
2222
(p) => p != 'alarms'
2323
)
2424

25+
firefox_manifest.permissions.push('contextualIdentities')
26+
firefox_manifest.optional_permissions = ['cookies'] // Needed by Firefox Containers
27+
2528
delete firefox_manifest.host_permissions
2629

2730
const firefox_dist_dir = path.join(__dirname, 'dist-firefox')

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ const process_next_chat = async () => {
8484
}
8585
})
8686

87+
const { 'selected-firefox-container': selected_firefox_container_id } =
88+
await browser.storage.local.get('selected-firefox-container')
89+
90+
const create_tab_options: any = {
91+
active: true
92+
}
93+
94+
if (selected_firefox_container_id) {
95+
create_tab_options.cookieStoreId = selected_firefox_container_id
96+
}
97+
8798
// OpenRouter is a special case—model handling via search params
8899
if (current_chat_message.url == 'https://openrouter.ai/chat') {
89100
// https://openrouter.ai/chat?models=openrouter/quasar-alpha
@@ -94,15 +105,11 @@ const process_next_chat = async () => {
94105
const open_router_url = `${
95106
current_chat_message.url
96107
}?${search_params.toString()}#cwc-${batch_id}`
97-
browser.tabs.create({
98-
url: open_router_url,
99-
active: true
100-
})
108+
create_tab_options.url = open_router_url
109+
browser.tabs.create(create_tab_options)
101110
} else {
102-
browser.tabs.create({
103-
url: `${current_chat_message.url}#cwc-${batch_id}`,
104-
active: true
105-
})
111+
create_tab_options.url = `${current_chat_message.url}#cwc-${batch_id}`
112+
browser.tabs.create(create_tab_options)
106113
}
107114

108115
// Set a timeout to automatically proceed if no confirmation is received

‎apps/browser/src/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "Code Web Chat Connector",
55
"short_name": "CWC",
66
"description": "Autofill your coding prompts in chatbots.",
7-
"version": "1.95.0",
7+
"version": "1.96.0",
88
"homepage_url": "https://github.com/robertpiosik/CodeWebChat",
99
"icons": {
1010
"16": "icons/icon-16.png",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.popup {
2+
display: flex;
3+
flex-direction: column;
4+
align-items: center;
5+
text-align: center;
6+
padding: 16px;
7+
gap: 12px;
8+
9+
&__title {
10+
font-size: 16px;
11+
margin: 0;
12+
}
13+
14+
&__firefox {
15+
border-top: 1px solid #eee;
16+
padding-top: 12px;
17+
width: 100%;
18+
display: flex;
19+
flex-direction: column;
20+
align-items: center;
21+
gap: 8px;
22+
}
23+
24+
&__enable-button {
25+
background: #0060df;
26+
color: white;
27+
padding: 8px 16px;
28+
border-radius: 4px;
29+
font-size: 14px;
30+
}
31+
32+
&__select-label {
33+
font-size: 14px;
34+
align-self: flex-start;
35+
}
36+
37+
&__container-select {
38+
width: 100%;
39+
padding: 4px;
40+
}
41+
}
Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
1+
import React from 'react'
2+
import styles from './Popup.module.scss'
3+
import { use_firefox_containers } from './use-firefox-containers'
4+
15
export const Popup: React.FC = () => {
6+
const {
7+
is_firefox,
8+
has_permission,
9+
containers,
10+
selected_container_id,
11+
request_permissions,
12+
handle_container_change
13+
} = use_firefox_containers()
14+
215
return (
3-
<div
4-
style={{
5-
display: 'flex',
6-
flexDirection: 'column',
7-
alignItems: 'center',
8-
textAlign: 'center'
9-
}}
10-
>
11-
<p style={{ fontSize: '16px' }}>Code Web Chat Connector is alive</p>
16+
<div className={styles.popup}>
17+
<p className={styles['popup__title']}>Code Web Chat Connector is alive</p>
18+
19+
{is_firefox && (
20+
<div className={styles['popup__firefox']}>
21+
{!has_permission ? (
22+
<button
23+
onClick={request_permissions}
24+
className={styles['popup__enable-button']}
25+
>
26+
Enable Firefox Containers
27+
</button>
28+
) : (
29+
<>
30+
<label className={styles['popup__select-label']}>
31+
Autofill prompts in container:
32+
</label>
33+
<select
34+
value={selected_container_id}
35+
onChange={handle_container_change}
36+
className={styles['popup__container-select']}
37+
>
38+
<option value="">Default (no container)</option>
39+
{containers.map((c) => (
40+
<option key={c.cookieStoreId} value={c.cookieStoreId}>
41+
{c.name}
42+
</option>
43+
))}
44+
</select>
45+
</>
46+
)}
47+
</div>
48+
)}
1249
</div>
1350
)
1451
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { useEffect, useState } from 'react'
2+
import browser from 'webextension-polyfill'
3+
4+
export type FirefoxContainer = {
5+
cookieStoreId: string
6+
name: string
7+
color: string
8+
icon: string
9+
}
10+
11+
export const use_firefox_containers = () => {
12+
const [containers, set_containers] = useState<FirefoxContainer[]>([])
13+
const [selected_container_id, set_selected_container_id] =
14+
useState<string>('')
15+
const [has_permission, set_has_permission] = useState<boolean>(false)
16+
const [is_firefox, set_is_firefox] = useState<boolean>(false)
17+
18+
useEffect(() => {
19+
const is_firefox_detected = navigator.userAgent.includes('Firefox')
20+
set_is_firefox(is_firefox_detected)
21+
22+
const init = async () => {
23+
try {
24+
if (is_firefox_detected) {
25+
const permissions = await browser.permissions.contains({
26+
permissions: ['cookies']
27+
})
28+
set_has_permission(permissions)
29+
if (permissions) {
30+
await load_containers()
31+
}
32+
}
33+
} catch (error) {
34+
console.error('Error initializing Firefox containers:', error)
35+
}
36+
}
37+
init()
38+
}, [])
39+
40+
const load_containers = async () => {
41+
try {
42+
const identities = await browser.contextualIdentities.query({})
43+
set_containers(identities)
44+
45+
const { 'selected-firefox-container': selected_id } =
46+
await browser.storage.local.get('selected-firefox-container')
47+
set_selected_container_id((selected_id as string) || '')
48+
} catch (error) {
49+
console.error('Error loading containers:', error)
50+
}
51+
}
52+
53+
const request_permissions = async () => {
54+
try {
55+
const granted = await browser.permissions.request({
56+
permissions: ['cookies']
57+
})
58+
if (granted) {
59+
set_has_permission(true)
60+
await load_containers()
61+
}
62+
} catch (error) {
63+
console.error('Error requesting permissions:', error)
64+
}
65+
}
66+
67+
const handle_container_change = async (
68+
e: React.ChangeEvent<HTMLSelectElement>
69+
) => {
70+
const value = e.target.value
71+
set_selected_container_id(value)
72+
if (value) {
73+
await browser.storage.local.set({ 'selected-firefox-container': value })
74+
} else {
75+
await browser.storage.local.remove('selected-firefox-container')
76+
}
77+
}
78+
79+
return {
80+
is_firefox,
81+
has_permission,
82+
containers,
83+
selected_container_id,
84+
request_permissions,
85+
handle_container_change
86+
}
87+
}

0 commit comments

Comments
 (0)