Skip to content

Commit 3623bcf

Browse files
committed
feat: Re-add context menu items and initialize context menu on startup
This commit reintroduces the context menu items that were removed in a previous commit. It also ensures that the context menu is initialized when the background script starts. - **Implementation:** - Re-adds context menu items for "Summarize", "Explain", "Rephrase", "Translate", and "Custom" actions. - Moves the context menu creation logic into the `initialize` function. - Calls the `initialize` function at the end of the background script to ensure context menus are created on startup.
1 parent ba6e19c commit 3623bcf

File tree

2 files changed

+65
-86
lines changed

2 files changed

+65
-86
lines changed

src/entries-firefox/background.ts

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,46 @@ export default defineBackground({
4040
const data = await getInitialConfig()
4141
contextMenuClick = data.contextMenuClick
4242
actionIconClick = data.actionIconClick
43-
browser.contextMenus.remove(
44-
data.contextMenuClick === "sidePanel"
45-
? contextMenuId["webui"]
46-
: contextMenuId["sidePanel"]
47-
)
4843
browser.contextMenus.create({
4944
id: contextMenuId[contextMenuClick],
5045
title: contextMenuTitle[contextMenuClick],
5146
contexts: ["page", "selection"]
5247
})
53-
48+
browser.contextMenus.create({
49+
id: "summarize-pa",
50+
title: browser.i18n.getMessage("contextSummarize"),
51+
contexts: ["selection"]
52+
})
53+
54+
browser.contextMenus.create({
55+
id: "explain-pa",
56+
title: browser.i18n.getMessage("contextExplain"),
57+
contexts: ["selection"]
58+
})
59+
60+
browser.contextMenus.create({
61+
id: "rephrase-pa",
62+
title: browser.i18n.getMessage("contextRephrase"),
63+
contexts: ["selection"]
64+
})
65+
66+
browser.contextMenus.create({
67+
id: "translate-pg",
68+
title: browser.i18n.getMessage("contextTranslate"),
69+
contexts: ["selection"]
70+
})
71+
72+
browser.contextMenus.create({
73+
id: "custom-pg",
74+
title: browser.i18n.getMessage("contextCustom"),
75+
contexts: ["selection"]
76+
})
77+
5478
} catch (error) {
5579
console.error("Error in initLogic:", error)
5680
}
5781
}
5882

59-
initialize()
6083

6184
browser.runtime.onMessage.addListener(async (message) => {
6285
if (message.type === "sidepanel") {
@@ -106,42 +129,6 @@ export default defineBackground({
106129
sidePanel: "open-side-panel-pa"
107130
}
108131

109-
browser.contextMenus.create({
110-
id: contextMenuId["sidePanel"],
111-
title: contextMenuTitle["sidePanel"],
112-
contexts: ["page", "selection"]
113-
})
114-
115-
browser.contextMenus.create({
116-
id: "summarize-pa",
117-
title: browser.i18n.getMessage("contextSummarize"),
118-
contexts: ["selection"]
119-
})
120-
121-
browser.contextMenus.create({
122-
id: "explain-pa",
123-
title: browser.i18n.getMessage("contextExplain"),
124-
contexts: ["selection"]
125-
})
126-
127-
browser.contextMenus.create({
128-
id: "rephrase-pa",
129-
title: browser.i18n.getMessage("contextRephrase"),
130-
contexts: ["selection"]
131-
})
132-
133-
browser.contextMenus.create({
134-
id: "translate-pg",
135-
title: browser.i18n.getMessage("contextTranslate"),
136-
contexts: ["selection"]
137-
})
138-
139-
browser.contextMenus.create({
140-
id: "custom-pg",
141-
title: browser.i18n.getMessage("contextCustom"),
142-
contexts: ["selection"]
143-
})
144-
145132

146133
browser.contextMenus.onClicked.addListener((info, tab) => {
147134
if (info.menuItemId === "open-side-panel-pa") {
@@ -217,6 +204,9 @@ export default defineBackground({
217204
break
218205
}
219206
})
207+
208+
initialize()
209+
220210
},
221211
persistent: true
222212
})

src/entries/background.ts

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,46 @@ export default defineBackground({
4343
const data = await getInitialConfig()
4444
contextMenuClick = data.contextMenuClick
4545
actionIconClick = data.actionIconClick
46-
browser.contextMenus.remove(
47-
data.contextMenuClick === "sidePanel"
48-
? contextMenuId["webui"]
49-
: contextMenuId["sidePanel"]
50-
)
46+
5147
browser.contextMenus.create({
5248
id: contextMenuId[contextMenuClick],
5349
title: contextMenuTitle[contextMenuClick],
5450
contexts: ["page", "selection"]
5551
})
56-
52+
browser.contextMenus.create({
53+
id: "summarize-pa",
54+
title: browser.i18n.getMessage("contextSummarize"),
55+
contexts: ["selection"]
56+
})
57+
58+
browser.contextMenus.create({
59+
id: "explain-pa",
60+
title: browser.i18n.getMessage("contextExplain"),
61+
contexts: ["selection"]
62+
})
63+
64+
browser.contextMenus.create({
65+
id: "rephrase-pa",
66+
title: browser.i18n.getMessage("contextRephrase"),
67+
contexts: ["selection"]
68+
})
69+
70+
browser.contextMenus.create({
71+
id: "translate-pg",
72+
title: browser.i18n.getMessage("contextTranslate"),
73+
contexts: ["selection"]
74+
})
75+
76+
browser.contextMenus.create({
77+
id: "custom-pg",
78+
title: browser.i18n.getMessage("contextCustom"),
79+
contexts: ["selection"]
80+
})
5781
} catch (error) {
5882
console.error("Error in initLogic:", error)
5983
}
6084
}
6185

62-
initialize()
6386

6487
browser.runtime.onMessage.addListener(async (message) => {
6588
if (message.type === "sidepanel") {
@@ -106,42 +129,6 @@ export default defineBackground({
106129
sidePanel: browser.i18n.getMessage("openSidePanelToChat")
107130
}
108131

109-
browser.contextMenus.create({
110-
id: contextMenuId["sidePanel"],
111-
title: contextMenuTitle["sidePanel"],
112-
contexts: ["page", "selection"]
113-
})
114-
115-
browser.contextMenus.create({
116-
id: "summarize-pa",
117-
title: browser.i18n.getMessage("contextSummarize"),
118-
contexts: ["selection"]
119-
})
120-
121-
browser.contextMenus.create({
122-
id: "explain-pa",
123-
title: browser.i18n.getMessage("contextExplain"),
124-
contexts: ["selection"]
125-
})
126-
127-
browser.contextMenus.create({
128-
id: "rephrase-pa",
129-
title: browser.i18n.getMessage("contextRephrase"),
130-
contexts: ["selection"]
131-
})
132-
133-
browser.contextMenus.create({
134-
id: "translate-pg",
135-
title: browser.i18n.getMessage("contextTranslate"),
136-
contexts: ["selection"]
137-
})
138-
139-
browser.contextMenus.create({
140-
id: "custom-pg",
141-
title: browser.i18n.getMessage("contextCustom"),
142-
contexts: ["selection"]
143-
})
144-
145132
browser.contextMenus.onClicked.addListener(async (info, tab) => {
146133
if (info.menuItemId === "open-side-panel-pa") {
147134
chrome.sidePanel.open({
@@ -245,6 +232,8 @@ export default defineBackground({
245232
break
246233
}
247234
})
235+
236+
initialize()
248237
},
249238
persistent: true
250239
})

0 commit comments

Comments
 (0)