Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/electron-chrome-extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ Example:
}
```

##### `extensions.windowUpdated(window)`

- `window` Electron.BrowserWindow

Update the details of a window from the main process.

##### `extensions.tabUpdated(tab)`

- `tab` Electron.WebContents

Update the details of a tab from the main process.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell me about how you plan to use these methods? Would it be possible to instead update automatically from within the module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using the tabUpdated method here.

For example, I have a custom tab discarding system which would need manually updating.

{
  assignTabDetails: (tabDetails, tabWebContents) => {
    const tab = tabManager.getTabByWebContents(tabWebContents);
    if (!tab) return;

    tabDetails.title = tab.title;
    tabDetails.url = tab.url;
    tabDetails.favIconUrl = tab.faviconURL ?? undefined;
    tabDetails.discarded = tab.asleep;
    tabDetails.autoDiscardable = false;
  }
}


#### Instance Events

##### Event: 'browser-action-popup-created'
Expand Down
6 changes: 6 additions & 0 deletions packages/electron-chrome-extensions/src/browser/api/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ export class TabsAPI {
}

this.ctx.store.tabDetailsCache.set(tab.id, details)

// Update the window's tab cache
if (win) {
this.ctx.store.updateWindowDetails(win)
}

return details
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class WindowsAPI {
handle('windows.remove', this.remove.bind(this))

this.ctx.store.on('window-added', this.observeWindow.bind(this))
this.ctx.store.on('update-window-details', this.updateWindowDetails.bind(this))
}

private observeWindow(window: Electron.BrowserWindow) {
Expand Down Expand Up @@ -62,7 +63,7 @@ export class WindowsAPI {
})
.map((tab) => this.ctx.store.tabDetailsCache.get(tab.id) as chrome.tabs.Tab)
.filter(Boolean),
incognito: this.ctx.session.isPersistent(),
incognito: !this.ctx.session.isPersistent(),
type: 'normal', // TODO
state: getWindowState(win),
alwaysOnTop: win.isAlwaysOnTop(),
Expand All @@ -81,6 +82,11 @@ export class WindowsAPI {
return details
}

public updateWindowDetails(win: Electron.BaseWindow) {
const details = this.createWindowDetails(win)
return details
}

private getWindowFromId(id: number) {
if (id === WindowsAPI.WINDOW_ID_CURRENT) {
return this.ctx.store.getCurrentWindow()
Expand Down
13 changes: 13 additions & 0 deletions packages/electron-chrome-extensions/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,19 @@ export class ElectronChromeExtensions extends EventEmitter {
}
}

windowUpdated(window: Electron.BaseWindow) {
return this.api.windows.updateWindowDetails(window)
}

tabUpdated(tab: Electron.WebContents) {
this.checkWebContentsArgument(tab)
if (this.ctx.store.tabs.has(tab)) {
this.api.tabs.onUpdated(tab.id)
return true
}
return false
}

/**
* Add webContents to be tracked as an extension host which will receive
* extension events when a chrome-extension:// resource is loaded.
Expand Down
4 changes: 4 additions & 0 deletions packages/electron-chrome-extensions/src/browser/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,8 @@ export class ExtensionStore extends EventEmitter {
const result: unknown = await this.impl.requestPermissions(extension, permissions)
return typeof result === 'boolean' ? result : false
}

updateWindowDetails(win: Electron.BaseWindow) {
this.emit('update-window-details', win)
}
}