@@ -46,19 +46,19 @@ export class Offscreen {
46
46
/* eslint-disable @stylistic/no-multi-spaces */
47
47
/** @type {import('offscreen').ApiMap } */
48
48
this . _apiMap = createApiMap ( [
49
- [ 'clipboardGetTextOffscreen' , this . _getTextHandler . bind ( this ) ] ,
50
- [ 'clipboardGetImageOffscreen' , this . _getImageHandler . bind ( this ) ] ,
49
+ [ 'clipboardGetTextOffscreen' , this . _getTextHandler . bind ( this ) ] ,
50
+ [ 'clipboardGetImageOffscreen' , this . _getImageHandler . bind ( this ) ] ,
51
51
[ 'clipboardSetBrowserOffscreen' , this . _setClipboardBrowser . bind ( this ) ] ,
52
- [ 'databasePrepareOffscreen' , this . _prepareDatabaseHandler . bind ( this ) ] ,
53
- [ 'getDictionaryInfoOffscreen' , this . _getDictionaryInfoHandler . bind ( this ) ] ,
54
- [ 'databasePurgeOffscreen' , this . _purgeDatabaseHandler . bind ( this ) ] ,
55
- [ 'databaseGetMediaOffscreen' , this . _getMediaHandler . bind ( this ) ] ,
56
- [ 'databaseGetMediaObjectsOffscreen ' , this . _getMediaObjectsHandler . bind ( this ) ] ,
57
- [ 'translatorPrepareOffscreen' , this . _prepareTranslatorHandler . bind ( this ) ] ,
58
- [ 'findKanjiOffscreen' , this . _findKanjiHandler . bind ( this ) ] ,
59
- [ 'findTermsOffscreen' , this . _findTermsHandler . bind ( this ) ] ,
60
- [ 'getTermFrequenciesOffscreen' , this . _getTermFrequenciesHandler . bind ( this ) ] ,
61
- [ 'clearDatabaseCachesOffscreen' , this . _clearDatabaseCachesHandler . bind ( this ) ] ,
52
+ [ 'databasePrepareOffscreen' , this . _prepareDatabaseHandler . bind ( this ) ] ,
53
+ [ 'getDictionaryInfoOffscreen' , this . _getDictionaryInfoHandler . bind ( this ) ] ,
54
+ [ 'databasePurgeOffscreen' , this . _purgeDatabaseHandler . bind ( this ) ] ,
55
+ [ 'databaseGetMediaOffscreen' , this . _getMediaHandler . bind ( this ) ] ,
56
+ [ 'databaseDrawMediaOffscreen ' , this . _drawMediaHandler . bind ( this ) ] ,
57
+ [ 'translatorPrepareOffscreen' , this . _prepareTranslatorHandler . bind ( this ) ] ,
58
+ [ 'findKanjiOffscreen' , this . _findKanjiHandler . bind ( this ) ] ,
59
+ [ 'findTermsOffscreen' , this . _findTermsHandler . bind ( this ) ] ,
60
+ [ 'getTermFrequenciesOffscreen' , this . _getTermFrequenciesHandler . bind ( this ) ] ,
61
+ [ 'clearDatabaseCachesOffscreen' , this . _clearDatabaseCachesHandler . bind ( this ) ]
62
62
] ) ;
63
63
/* eslint-enable @stylistic/no-multi-spaces */
64
64
@@ -69,6 +69,18 @@ export class Offscreen {
69
69
/** */
70
70
prepare ( ) {
71
71
chrome . runtime . onMessage . addListener ( this . _onMessage . bind ( this ) ) ;
72
+
73
+ const registerPort = ( ) => {
74
+ const mc = new MessageChannel ( ) ;
75
+ mc . port1 . onmessage = ( e ) => {
76
+ this . _onSWMessage ( e . data ) ;
77
+ } ;
78
+ void navigator . serviceWorker . ready . then ( ( swr ) => {
79
+ swr . active ?. postMessage ( { action : 'registerOffscreenPort' } , [ mc . port2 ] ) ;
80
+ } ) ;
81
+ } ;
82
+ navigator . serviceWorker . addEventListener ( "controllerchange" , registerPort ) ;
83
+ registerPort ( ) ;
72
84
}
73
85
74
86
/** @type {import('offscreen').ApiHandler<'clipboardGetTextOffscreen'> } */
@@ -111,9 +123,9 @@ export class Offscreen {
111
123
return media . map ( ( m ) => ( { ...m , content : arrayBufferToBase64 ( m . content ) } ) ) ;
112
124
}
113
125
114
- /** @type {import('offscreen').ApiHandler<'databaseGetMediaObjectsOffscreen '> } */
115
- async _getMediaObjectsHandler ( { targets} ) {
116
- return await this . _dictionaryDatabase . getMediaObjects ( targets ) ;
126
+ /** @type {import('offscreen').ApiHandler<'databaseDrawMediaOffscreen '> } */
127
+ async _drawMediaHandler ( { targets} ) {
128
+ await this . _dictionaryDatabase . drawMedia ( targets ) ;
117
129
}
118
130
119
131
/** @type {import('offscreen').ApiHandler<'translatorPrepareOffscreen'> } */
@@ -136,11 +148,11 @@ export class Offscreen {
136
148
const enabledDictionaryMap = new Map ( options . enabledDictionaryMap ) ;
137
149
const excludeDictionaryDefinitions = (
138
150
options . excludeDictionaryDefinitions !== null ?
139
- new Set ( options . excludeDictionaryDefinitions ) :
140
- null
151
+ new Set ( options . excludeDictionaryDefinitions ) :
152
+ null
141
153
) ;
142
154
const textReplacements = options . textReplacements . map ( ( group ) => {
143
- if ( group === null ) { return null ; }
155
+ if ( group === null ) { return null ; }
144
156
return group . map ( ( opt ) => {
145
157
// https://stackoverflow.com/a/33642463
146
158
const match = opt . pattern . match ( / \/ ( .* ?) \/ ( [ a - z ] * ) ? $ / i) ;
@@ -172,4 +184,11 @@ export class Offscreen {
172
184
_onMessage ( { action, params} , _sender , callback ) {
173
185
return invokeApiMapHandler ( this . _apiMap , action , params , [ ] , callback ) ;
174
186
}
187
+
188
+ /** @param {{action: string, params: any} } obj */
189
+ _onSWMessage ( { action, params} ) {
190
+ if ( action === 'drawMedia' ) {
191
+ this . _dictionaryDatabase . drawMedia ( params ) ;
192
+ }
193
+ }
175
194
}
0 commit comments