Skip to content

Commit

Permalink
chore: update methods
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaddock committed Dec 4, 2024
1 parent 7d3a1a2 commit a87ccec
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/electron-chrome-extensions/src/browser-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ export const injectBrowserAction = () => {
contextBridge.exposeInMainWorld('browserAction', __browserAction__)

// Must execute script in main world to modify custom component registry.
;(contextBridge as any).evaluateInMainWorld({
func: mainWorldScript
;(contextBridge as any).executeInMainWorld({
func: mainWorldScript,
})
} else {
// When contextIsolation is disabled, contextBridge will throw an error.
Expand Down
29 changes: 18 additions & 11 deletions packages/electron-chrome-extensions/src/browser/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const getSessionFromEvent = (event: any): Electron.Session => {
// TODO(mv3): add types
const getHostFromEvent = (event: any) => {
if (event.type === 'service-worker') {
const serviceWorker = event.session.serviceWorkers.fromVersionID(event.versionId)
const serviceWorker = event.session.serviceWorkers.getWorkerFromVersionID(event.versionId)
return serviceWorker && !serviceWorker.isDestroyed() ? serviceWorker : null
} else {
return event.sender
Expand Down Expand Up @@ -72,7 +72,9 @@ class RoutingDelegate {
const maybeListenForWorkerEvents = ({ runningStatus, versionId }: any) => {
if (runningStatus !== 'starting') return

const serviceWorker = (observer.session as any).serviceWorkers.fromVersionID(versionId)
const serviceWorker = (observer.session as any).serviceWorkers.getWorkerFromVersionID(
versionId,
)
if (
serviceWorker?.scope?.startsWith('chrome-extension://') &&
!this.workers.has(serviceWorker)
Expand Down Expand Up @@ -209,15 +211,18 @@ export class ExtensionRouter {
}
})

session.serviceWorkers.on('running-status-changed' as any, ({ runningStatus, versionId }: any) => {
if (runningStatus !== 'starting') return
session.serviceWorkers.on(
'running-status-changed' as any,
({ runningStatus, versionId }: any) => {
if (runningStatus !== 'starting') return

const serviceWorker = (session as any).serviceWorkers.fromVersionID(versionId)
if (serviceWorker) {
debug(`storing reference to background service worker [url:'${serviceWorker.scope}']`)
this.extensionWorkers.add(serviceWorker)
}
})
const serviceWorker = (session as any).serviceWorkers.getWorkerFromVersionID(versionId)
if (serviceWorker) {
debug(`storing reference to background service worker [url:'${serviceWorker.scope}']`)
this.extensionWorkers.add(serviceWorker)
}
},
)
}

private filterListeners(predicate: (listener: EventListener) => boolean) {
Expand Down Expand Up @@ -380,7 +385,9 @@ export class ExtensionRouter {

if (host.constructor.name === 'ServiceWorkerMain') {
if (host.isDestroyed()) {
console.error(`Service Worker is destroyed.\nUnable to send '${eventName}' to extension host for ${extensionId}`)
console.error(
`Service Worker is destroyed.\nUnable to send '${eventName}' to extension host for ${extensionId}`,
)
return
}
host.startWorker().then(send)
Expand Down
18 changes: 11 additions & 7 deletions packages/electron-chrome-extensions/src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export const injectExtensionAPIs = () => {
if (manifest.manifest_version === 3) {
// TODO(mv3): might need to use offscreen document to serialize
console.warn(
'action.setIcon with imageData is not yet supported by electron-chrome-extensions'
'action.setIcon with imageData is not yet supported by electron-chrome-extensions',
)
details.imageData = undefined
} else if (details.imageData instanceof ImageData) {
Expand All @@ -169,7 +169,7 @@ export const injectExtensionAPIs = () => {
obj[pair[0]] = imageData2base64(pair[1])
return obj
},
{}
{},
)
}
}
Expand Down Expand Up @@ -314,11 +314,11 @@ export const injectExtensionAPIs = () => {
...base,
isAllowedFileSchemeAccess: invokeExtension('extension.isAllowedFileSchemeAccess', {
noop: true,
defaultResponse: false
defaultResponse: false,
}),
isAllowedIncognitoAccess: invokeExtension('extension.isAllowedIncognitoAccess', {
noop: true,
defaultResponse: false
defaultResponse: false,
}),
// TODO: Add native implementation
getViews: () => [],
Expand Down Expand Up @@ -415,7 +415,11 @@ export const injectExtensionAPIs = () => {
const api = {
...base,
create: invokeExtension('tabs.create'),
executeScript: async function (arg1: unknown, arg2: unknown, arg3: unknown): Promise<any> {
executeScript: async function (
arg1: unknown,
arg2: unknown,
arg3: unknown,
): Promise<any> {
// Electron's implementation of chrome.tabs.executeScript is in
// C++, but it doesn't support implicit execution in the active
// tab. To handle this, we need to get the active tab ID and
Expand Down Expand Up @@ -541,8 +545,8 @@ export const injectExtensionAPIs = () => {
contextBridge.exposeInMainWorld('electron', electronContext)

// Mutate global 'chrome' object with additional APIs in the main world.
;(contextBridge as any).evaluateInMainWorld({
func: mainWorldScript
;(contextBridge as any).executeInMainWorld({
func: mainWorldScript,
})
} catch (error) {
console.error(`injectExtensionAPIs error (${location.href})`)
Expand Down

0 comments on commit a87ccec

Please sign in to comment.