Skip to content

Commit 68ec4cf

Browse files
committed
Replace session storage with in-memory session state management
- Refactor send-prompt-content-script.ts and add-apply-response-button.ts to replace all sessionStorage calls (setItem, getItem, removeItem) with a newly created exported module-level variable (e.g. let session_state) to securely store the session data within the content script's isolated memory. └── apps └── browser ├── README.md └── src ├── background │ ├── clear-chat-init-data.ts │ ├── main.ts │ └── message-handler.ts ├── content-scripts │ └── send-prompt-content-script │ ├── chatbots │ │ └── ai-studio.ts │ ├── send-prompt-content-script.ts │ └── utils │ ├── add-apply-response-button.ts │ └── session-state.ts └── manifest.json
1 parent 83c6cf2 commit 68ec4cf

3 files changed

Lines changed: 25 additions & 23 deletions

File tree

‎apps/browser/src/content-scripts/send-prompt-content-script/send-prompt-content-script.ts‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
hugging_chat,
2525
z_ai
2626
} from './chatbots'
27+
import { session_state, set_session_state } from './utils/session-state'
2728

2829
// In case url changes on load
2930
const current_url = window.location.href
@@ -176,8 +177,6 @@ const initialize_chat = async (params: { message: string; chat: Chat }) => {
176177
}
177178

178179
const main = async () => {
179-
const session_data_key = 'cwc-session-data'
180-
181180
const hash = window.location.hash
182181
const is_cwc_hash = hash.startsWith(hash_prefix_new)
183182
const batch_id = hash.substring(hash_prefix_new.length + 1)
@@ -235,32 +234,27 @@ const main = async () => {
235234
if (chatbot?.setup_observer) {
236235
const inject_button = stored_data.inject_apply_response_button ?? false
237236

238-
sessionStorage.setItem(
239-
session_data_key,
240-
JSON.stringify({
241-
client_id: stored_data.client_id,
242-
raw_instructions: stored_data.raw_instructions,
243-
inject_button
244-
})
245-
)
237+
set_session_state({
238+
client_id: stored_data.client_id,
239+
raw_instructions: stored_data.raw_instructions,
240+
inject_button
241+
})
246242
chatbot.setup_observer({
247243
client_id: stored_data.client_id,
248244
raw_instructions: stored_data.raw_instructions,
249245
inject_button
250246
})
251247
} else {
252-
sessionStorage.removeItem(session_data_key)
248+
set_session_state(null)
253249
}
254250
} else {
255-
const session_data_str = sessionStorage.getItem(session_data_key)
256-
if (session_data_str && chatbot) {
251+
if (session_state && chatbot) {
257252
try {
258-
const session_data = JSON.parse(session_data_str)
259253
if (chatbot.setup_observer) {
260254
chatbot.setup_observer({
261-
client_id: session_data.client_id,
262-
raw_instructions: session_data.raw_instructions,
263-
inject_button: session_data.inject_button ?? true
255+
client_id: session_state.client_id,
256+
raw_instructions: session_state.raw_instructions,
257+
inject_button: session_state.inject_button ?? true
264258
})
265259
}
266260
} catch (e) {

‎apps/browser/src/content-scripts/send-prompt-content-script/utils/add-apply-response-button.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
set_button_disabled_state
88
} from './apply-response-styles'
99
import { show_response_ready_notification } from './show-response-ready-notification'
10+
import { session_state } from './session-state'
1011

1112
export function add_apply_response_button(params: {
1213
client_id: string
@@ -40,12 +41,10 @@ export function add_apply_response_button(params: {
4041
let raw_instructions = params.raw_instructions
4142

4243
try {
43-
const session_data_str = sessionStorage.getItem('cwc-session-data')
44-
if (session_data_str) {
45-
const session_data = JSON.parse(session_data_str)
46-
if (session_data.client_id !== undefined) {
47-
client_id = session_data.client_id
48-
raw_instructions = session_data.raw_instructions
44+
if (session_state) {
45+
if (session_state.client_id !== undefined) {
46+
client_id = session_state.client_id
47+
raw_instructions = session_state.raw_instructions
4948
}
5049
}
5150
} catch (error) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export let session_state: {
2+
client_id: string
3+
raw_instructions?: string
4+
inject_button?: boolean
5+
} | null = null
6+
7+
export const set_session_state = (state: typeof session_state) => {
8+
session_state = state
9+
}

0 commit comments

Comments
 (0)