Skip to content

Commit

Permalink
Add support for BrowserView (#128)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
Willyfrog and sindresorhus authored Feb 18, 2021
1 parent 49246b0 commit 6b5cd40
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BrowserWindow, DownloadItem} from 'electron';
import {BrowserView, BrowserWindow, DownloadItem} from 'electron';

declare namespace electronDl {
interface Progress {
Expand Down Expand Up @@ -121,7 +121,7 @@ declare const electronDl: {
```
*/
download(
window: BrowserWindow,
window: BrowserWindow | BrowserView,
url: string,
options?: electronDl.Options
): Promise<DownloadItem>;
Expand Down
40 changes: 34 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,39 @@ const getFilenameFromMime = (name, mime) => {
return `${name}.${extensions[0].ext}`;
};

const majorElectronVersion = () => {
const version = process.versions.electron.split('.');
return parseInt(version[0], 10);
};

const getWindowFromBrowserView = webContents => {
for (const currentWindow of BrowserWindow.getAllWindows()) {
for (const currentBrowserView of currentWindow.getBrowserViews()) {
if (currentBrowserView.webContents.id === webContents.id) {
return currentWindow;
}
}
}
};

const getWindowFromWebContents = webContents => {
let window_;
const webContentsType = webContents.getType();
switch (webContentsType) {
case 'webview':
window_ = BrowserWindow.fromWebContents(webContents.hostWebContents);
break;
case 'browserView':
window_ = getWindowFromBrowserView(webContents);
break;
default:
window_ = BrowserWindow.fromWebContents(webContents);
break;
}

return window_;
};

function registerListener(session, options, callback = () => {}) {
const downloadItems = new Set();
let receivedBytes = 0;
Expand All @@ -32,12 +65,7 @@ function registerListener(session, options, callback = () => {}) {
downloadItems.add(item);
totalBytes += item.getTotalBytes();

let hostWebContents = webContents;
if (webContents.getType() === 'webview') {
({hostWebContents} = webContents);
}

const window_ = BrowserWindow.fromWebContents(hostWebContents);
const window_ = majorElectronVersion() >= 12 ? BrowserWindow.fromWebContents(webContents) : getWindowFromWebContents(webContents);

const directory = options.directory || app.getPath('downloads');
let filePath;
Expand Down
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Saves the file to the users Downloads directory instead of prompting.
- Bounces the Downloads directory in the dock when done. *(macOS)*
- Handles multiple downloads.
- Support for `BrowserWindow` and `BrowserView`.
- Shows badge count *(macOS & Linux only)* and download progress. Example on macOS:

<img src="screenshot.png" width="82">
Expand Down Expand Up @@ -63,9 +64,9 @@ It can only be used in the [main](https://electronjs.org/docs/glossary/#main-pro

### window

Type: `BrowserWindow`
Type: `BrowserWindow | BrowserView`

Window to register the behavior on.
Window to register the behavior on. Alternatively, a `BrowserView` can be passed.

### url

Expand Down

0 comments on commit 6b5cd40

Please sign in to comment.