Skip to content

Commit

Permalink
feat: electron backwards compat
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaddock committed Dec 13, 2024
1 parent c33f830 commit 1dd24cf
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 29 deletions.
17 changes: 11 additions & 6 deletions packages/electron-chrome-extensions/spec/crx-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ export const createCrxSession = () => {
}

export const addCrxPreload = (session: Electron.Session) => {
// TODO(mv3): remove any
(session as any).registerPreloadScript({
id: 'crx-test-preload',
type: 'frame',
filePath: path.join(__dirname, 'fixtures', 'crx-test-preload.js')
})
const preloadPath = path.join(__dirname, 'fixtures', 'crx-test-preload.js')
if ('registerPreloadScript' in session) {
// TODO(mv3): remove any
;(session as any).registerPreloadScript({
id: 'crx-test-preload',
type: 'frame',
filePath: preloadPath,
})
} else {
session.setPreloads([...session.getPreloads(), preloadPath])
}
}

export const createCrxRemoteWindow = () => {
Expand Down
11 changes: 8 additions & 3 deletions packages/electron-chrome-extensions/src/browser-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,14 @@ export const injectBrowserAction = () => {
contextBridge.exposeInMainWorld('browserAction', __browserAction__)

// Must execute script in main world to modify custom component registry.
;(contextBridge as any).executeInMainWorld({
func: mainWorldScript,
})
if ('executeInMainWorld' in contextBridge) {
;(contextBridge as any).executeInMainWorld({
func: mainWorldScript,
})
} else {
// TODO(mv3): remove webFrame usage
webFrame.executeJavaScript(`(${mainWorldScript}());`)
}
} else {
// When contextIsolation is disabled, contextBridge will throw an error.
// If that's the case, we're in the main world so we can just execute our
Expand Down
27 changes: 16 additions & 11 deletions packages/electron-chrome-extensions/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,23 @@ export class ElectronChromeExtensions extends EventEmitter {
private async prependPreload() {
const { session } = this.ctx

// TODO(mv3): remove 'any'
const preloadPath = path.join(this.modulePath, 'dist/preload.js')
;(session as any).registerPreloadScript({
id: 'crx-mv2-preload',
type: 'frame',
filePath: preloadPath,
})
;(session as any).registerPreloadScript({
id: 'crx-mv3-preload',
type: 'service-worker',
filePath: preloadPath,
})

if ('registerPreloadScript' in session) {
// TODO(mv3): remove 'any'
;(session as any).registerPreloadScript({
id: 'crx-mv2-preload',
type: 'frame',
filePath: preloadPath,
})
;(session as any).registerPreloadScript({
id: 'crx-mv3-preload',
type: 'service-worker',
filePath: preloadPath,
})
} else {
session.setPreloads([...session.getPreloads(), preloadPath])
}

let preloadExists = false
try {
Expand Down
1 change: 1 addition & 0 deletions packages/electron-chrome-extensions/src/preload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { injectExtensionAPIs } from './renderer'

// Only load within extension page context
// TODO(mv3): remove any
if ((process as any).type === 'service-worker' || location.href.startsWith('chrome-extension://')) {
injectExtensionAPIs()
}
11 changes: 8 additions & 3 deletions packages/electron-chrome-extensions/src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,14 @@ export const injectExtensionAPIs = () => {
contextBridge.exposeInMainWorld('electron', electronContext)

// Mutate global 'chrome' object with additional APIs in the main world.
;(contextBridge as any).executeInMainWorld({
func: mainWorldScript,
})
if ('executeInMainWorld' in contextBridge) {
;(contextBridge as any).executeInMainWorld({
func: mainWorldScript,
})
} else {
// TODO(mv3): remove webFrame usage
webFrame.executeJavaScript(`(${mainWorldScript}());`)
}
} catch (error) {
console.error(`injectExtensionAPIs error (${location.href})`)
console.error(error)
Expand Down
12 changes: 11 additions & 1 deletion packages/electron-chrome-web-store/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,17 @@ export function installChromeWebStore(opts: ElectronChromeWebStoreOptions = {})

// Add preload script to session
const preloadPath = path.join(modulePath, 'dist/renderer/web-store-preload.js')
session.setPreloads([...session.getPreloads(), preloadPath])

if ('registerPreloadScript' in session) {
;(session as any).registerPreloadScript({
id: 'electron-chrome-web-store',
type: 'frame',
filePath: preloadPath,
})
} else {
// TODO(mv3): remove
session.setPreloads([...session.getPreloads(), preloadPath])
}

addIpcListeners(webStoreState)

Expand Down
15 changes: 10 additions & 5 deletions packages/shell/browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,16 @@ class Browser {
this.initSession()
setupMenu(this)

this.session.registerPreloadScript({
id: 'shell-preload',
type: 'frame',
filePath: PATHS.PRELOAD,
})
if ('registerPreloadScript' in this.session) {
this.session.registerPreloadScript({
id: 'shell-preload',
type: 'frame',
filePath: PATHS.PRELOAD,
})
} else {
// TODO(mv3): remove
this.session.setPreloads([PATHS.PRELOAD])
}

this.extensions = new ElectronChromeExtensions({
license: 'internal-license-do-not-use',
Expand Down

0 comments on commit 1dd24cf

Please sign in to comment.