File tree Expand file tree Collapse file tree 7 files changed +65
-29
lines changed
electron-chrome-extensions
electron-chrome-web-store/src/browser Expand file tree Collapse file tree 7 files changed +65
-29
lines changed Original file line number Diff line number Diff line change @@ -14,12 +14,17 @@ export const createCrxSession = () => {
1414}
1515
1616export const addCrxPreload = ( session : Electron . Session ) => {
17- // TODO(mv3): remove any
18- ( session as any ) . registerPreloadScript ( {
19- id : 'crx-test-preload' ,
20- type : 'frame' ,
21- filePath : path . join ( __dirname , 'fixtures' , 'crx-test-preload.js' )
22- } )
17+ const preloadPath = path . join ( __dirname , 'fixtures' , 'crx-test-preload.js' )
18+ if ( 'registerPreloadScript' in session ) {
19+ // TODO(mv3): remove any
20+ ; ( session as any ) . registerPreloadScript ( {
21+ id : 'crx-test-preload' ,
22+ type : 'frame' ,
23+ filePath : preloadPath ,
24+ } )
25+ } else {
26+ session . setPreloads ( [ ...session . getPreloads ( ) , preloadPath ] )
27+ }
2328}
2429
2530export const createCrxRemoteWindow = ( ) => {
Original file line number Diff line number Diff line change @@ -405,9 +405,14 @@ export const injectBrowserAction = () => {
405405 contextBridge . exposeInMainWorld ( 'browserAction' , __browserAction__ )
406406
407407 // Must execute script in main world to modify custom component registry.
408- ; ( contextBridge as any ) . executeInMainWorld ( {
409- func : mainWorldScript ,
410- } )
408+ if ( 'executeInMainWorld' in contextBridge ) {
409+ ; ( contextBridge as any ) . executeInMainWorld ( {
410+ func : mainWorldScript ,
411+ } )
412+ } else {
413+ // TODO(mv3): remove webFrame usage
414+ webFrame . executeJavaScript ( `(${ mainWorldScript } ());` )
415+ }
411416 } else {
412417 // When contextIsolation is disabled, contextBridge will throw an error.
413418 // If that's the case, we're in the main world so we can just execute our
Original file line number Diff line number Diff line change @@ -104,18 +104,23 @@ export class ElectronChromeExtensions extends EventEmitter {
104104 private async prependPreload ( ) {
105105 const { session } = this . ctx
106106
107- // TODO(mv3): remove 'any'
108107 const preloadPath = path . join ( this . modulePath , 'dist/preload.js' )
109- ; ( session as any ) . registerPreloadScript ( {
110- id : 'crx-mv2-preload' ,
111- type : 'frame' ,
112- filePath : preloadPath ,
113- } )
114- ; ( session as any ) . registerPreloadScript ( {
115- id : 'crx-mv3-preload' ,
116- type : 'service-worker' ,
117- filePath : preloadPath ,
118- } )
108+
109+ if ( 'registerPreloadScript' in session ) {
110+ // TODO(mv3): remove 'any'
111+ ; ( session as any ) . registerPreloadScript ( {
112+ id : 'crx-mv2-preload' ,
113+ type : 'frame' ,
114+ filePath : preloadPath ,
115+ } )
116+ ; ( session as any ) . registerPreloadScript ( {
117+ id : 'crx-mv3-preload' ,
118+ type : 'service-worker' ,
119+ filePath : preloadPath ,
120+ } )
121+ } else {
122+ session . setPreloads ( [ ...session . getPreloads ( ) , preloadPath ] )
123+ }
119124
120125 let preloadExists = false
121126 try {
Original file line number Diff line number Diff line change 11import { injectExtensionAPIs } from './renderer'
22
33// Only load within extension page context
4+ // TODO(mv3): remove any
45if ( ( process as any ) . type === 'service-worker' || location . href . startsWith ( 'chrome-extension://' ) ) {
56 injectExtensionAPIs ( )
67}
Original file line number Diff line number Diff line change @@ -545,9 +545,14 @@ export const injectExtensionAPIs = () => {
545545 contextBridge . exposeInMainWorld ( 'electron' , electronContext )
546546
547547 // Mutate global 'chrome' object with additional APIs in the main world.
548- ; ( contextBridge as any ) . executeInMainWorld ( {
549- func : mainWorldScript ,
550- } )
548+ if ( 'executeInMainWorld' in contextBridge ) {
549+ ; ( contextBridge as any ) . executeInMainWorld ( {
550+ func : mainWorldScript ,
551+ } )
552+ } else {
553+ // TODO(mv3): remove webFrame usage
554+ webFrame . executeJavaScript ( `(${ mainWorldScript } ());` )
555+ }
551556 } catch ( error ) {
552557 console . error ( `injectExtensionAPIs error (${ location . href } )` )
553558 console . error ( error )
Original file line number Diff line number Diff line change @@ -525,7 +525,17 @@ export function installChromeWebStore(opts: ElectronChromeWebStoreOptions = {})
525525
526526 // Add preload script to session
527527 const preloadPath = path . join ( modulePath , 'dist/renderer/web-store-preload.js' )
528- session . setPreloads ( [ ...session . getPreloads ( ) , preloadPath ] )
528+
529+ if ( 'registerPreloadScript' in session ) {
530+ ; ( session as any ) . registerPreloadScript ( {
531+ id : 'electron-chrome-web-store' ,
532+ type : 'frame' ,
533+ filePath : preloadPath ,
534+ } )
535+ } else {
536+ // TODO(mv3): remove
537+ session . setPreloads ( [ ...session . getPreloads ( ) , preloadPath ] )
538+ }
529539
530540 addIpcListeners ( webStoreState )
531541
Original file line number Diff line number Diff line change @@ -126,11 +126,16 @@ class Browser {
126126 this . initSession ( )
127127 setupMenu ( this )
128128
129- this . session . registerPreloadScript ( {
130- id : 'shell-preload' ,
131- type : 'frame' ,
132- filePath : PATHS . PRELOAD ,
133- } )
129+ if ( 'registerPreloadScript' in this . session ) {
130+ this . session . registerPreloadScript ( {
131+ id : 'shell-preload' ,
132+ type : 'frame' ,
133+ filePath : PATHS . PRELOAD ,
134+ } )
135+ } else {
136+ // TODO(mv3): remove
137+ this . session . setPreloads ( [ PATHS . PRELOAD ] )
138+ }
134139
135140 this . extensions = new ElectronChromeExtensions ( {
136141 license : 'internal-license-do-not-use' ,
You can’t perform that action at this time.
0 commit comments