electron-dl
Advanced tools
Comparing version 3.0.3 to 3.1.0
@@ -1,2 +0,2 @@ | ||
import {BrowserWindow, DownloadItem} from 'electron'; | ||
import {BrowserView, BrowserWindow, DownloadItem} from 'electron'; | ||
@@ -124,3 +124,3 @@ declare namespace electronDl { | ||
download( | ||
window: BrowserWindow, | ||
window: BrowserWindow | BrowserView, | ||
url: string, | ||
@@ -127,0 +127,0 @@ options?: electronDl.Options |
40
index.js
@@ -18,2 +18,35 @@ 'use strict'; | ||
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 = () => {}) { | ||
@@ -36,9 +69,4 @@ const downloadItems = new Set(); | ||
let hostWebContents = webContents; | ||
if (webContents.getType() === 'webview') { | ||
({hostWebContents} = webContents); | ||
} | ||
const window_ = majorElectronVersion() >= 12 ? BrowserWindow.fromWebContents(webContents) : getWindowFromWebContents(webContents); | ||
const window_ = BrowserWindow.fromWebContents(hostWebContents); | ||
const directory = options.directory || app.getPath('downloads'); | ||
@@ -45,0 +73,0 @@ let filePath; |
{ | ||
"name": "electron-dl", | ||
"version": "3.0.3", | ||
"version": "3.1.0", | ||
"description": "Simplified file downloads for your Electron app", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -11,2 +11,3 @@ # electron-dl | ||
- Handles multiple downloads. | ||
- Support for `BrowserWindow` and `BrowserView`. | ||
- Shows badge count *(macOS & Linux only)* and download progress. Example on macOS: | ||
@@ -67,5 +68,5 @@ | ||
Type: `BrowserWindow` | ||
Type: `BrowserWindow | BrowserView` | ||
Window to register the behavior on. | ||
Window to register the behavior on. Alternatively, a `BrowserView` can be passed. | ||
@@ -72,0 +73,0 @@ ### url |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14980
255
185