@@ -16,7 +16,7 @@ import {
1616 gptApiModelKeys ,
1717} from '../config/index.mjs'
1818import { isSafari } from '../utils/is-safari'
19- import { isFirefox } from '../utils/is-firefox '
19+ import { config as menuConfig } from '../content-script/menu-tools '
2020
2121const KEY_ACCESS_TOKEN = 'accessToken'
2222const cache = new ExpiryMap ( 10 * 1000 )
@@ -107,41 +107,58 @@ Browser.runtime.onMessage.addListener(async (message) => {
107107 }
108108} )
109109
110- Browser . contextMenus . removeAll ( ) . then ( ( ) => {
111- const menuId = 'ChatGPTBox-Menu'
112- Browser . contextMenus . create ( {
113- id : menuId ,
114- title : 'ChatGPTBox' ,
115- contexts : [ 'all' ] ,
116- } )
110+ function refreshMenu ( ) {
111+ Browser . contextMenus . removeAll ( ) . then ( ( ) => {
112+ const menuId = 'ChatGPTBox-Menu'
113+ Browser . contextMenus . create ( {
114+ id : menuId ,
115+ title : 'ChatGPTBox' ,
116+ contexts : [ 'all' ] ,
117+ } )
117118
118- Browser . contextMenus . create ( {
119- id : menuId + 'new' ,
120- parentId : menuId ,
121- title : 'New Chat' ,
122- contexts : [ isFirefox ( ) ? 'all' : 'selection' ] ,
123- } )
124- for ( const index in defaultConfig . selectionTools ) {
125- const key = defaultConfig . selectionTools [ index ]
126- const desc = defaultConfig . selectionToolsDesc [ index ]
119+ for ( const [ k , v ] of Object . entries ( menuConfig ) ) {
120+ Browser . contextMenus . create ( {
121+ id : menuId + k ,
122+ parentId : menuId ,
123+ title : v . label ,
124+ contexts : [ 'all' ] ,
125+ } )
126+ }
127127 Browser . contextMenus . create ( {
128- id : menuId + key ,
128+ id : menuId + 'separator1' ,
129129 parentId : menuId ,
130- title : desc ,
131130 contexts : [ 'selection' ] ,
131+ type : 'separator' ,
132132 } )
133- }
134-
135- Browser . contextMenus . onClicked . addListener ( ( info , tab ) => {
136- const itemId = info . menuItemId === menuId ? 'new' : info . menuItemId . replace ( menuId , '' )
137- const message = {
138- itemId : itemId ,
139- selectionText : info . selectionText ,
133+ for ( const index in defaultConfig . selectionTools ) {
134+ const key = defaultConfig . selectionTools [ index ]
135+ const desc = defaultConfig . selectionToolsDesc [ index ]
136+ Browser . contextMenus . create ( {
137+ id : menuId + key ,
138+ parentId : menuId ,
139+ title : desc ,
140+ contexts : [ 'selection' ] ,
141+ } )
140142 }
141- console . debug ( 'menu clicked' , message )
142- Browser . tabs . sendMessage ( tab . id , {
143- type : 'MENU' ,
144- data : message ,
143+
144+ Browser . contextMenus . onClicked . addListener ( ( info , tab ) => {
145+ const message = {
146+ itemId : info . menuItemId . replace ( menuId , '' ) ,
147+ selectionText : info . selectionText ,
148+ }
149+ console . debug ( 'menu clicked' , message )
150+ Browser . tabs . sendMessage ( tab . id , {
151+ type : 'CREATE_MENU' ,
152+ data : message ,
153+ } )
145154 } )
146155 } )
156+ }
157+
158+ Browser . runtime . onMessage . addListener ( async ( message ) => {
159+ if ( message . type === 'REFRESH_MENU' ) {
160+ refreshMenu ( )
161+ }
147162} )
163+
164+ refreshMenu ( )
0 commit comments