Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use electron latest in electron-dl #136

Open
integemjack opened this issue Jun 15, 2021 · 10 comments
Open

how to use electron latest in electron-dl #136

integemjack opened this issue Jun 15, 2021 · 10 comments

Comments

@integemjack
Copy link

No description provided.

@corwin-of-amber
Copy link

@integemjack can you provide a description of the error?

I am trying the following:

ipcMain.on('download-request', async (event, {url}) => {
  console.log('requesting to download', url);
  try {
    const win = BrowserWindow.getFocusedWindow();
    await download(win, url);
  }
  catch (e) { console.error('(download)', e); }
});

and I'm getting this popup:

Uncaught Exception:
TypeError: Cannot read properties of null (reading 'getOwnerBrowserWindow')
at Function.n.fromWebContents (node:electron/js2c/browser_init:33:1573)
at Session.listener (/Users/corwin/var/workspace/Teaching.Web.GR/node_modules/electron-dl/index.js:68:64)
at Session.emit (node:events:406:35)

The exception is not caught by my try .. catch because apparently it occurs in a tasklet.

@Kiyozz
Copy link

Kiyozz commented Jan 25, 2022

@integemjack can you provide a description of the error?

I am trying the following:

ipcMain.on('download-request', async (event, {url}) => {
  console.log('requesting to download', url);
  try {
    const win = BrowserWindow.getFocusedWindow();
    await download(win, url);
  }
  catch (e) { console.error('(download)', e); }
});

and I'm getting this popup:

Uncaught Exception:
TypeError: Cannot read properties of null (reading 'getOwnerBrowserWindow')
at Function.n.fromWebContents (node:electron/js2c/browser_init:33:1573)
at Session.listener (/Users/corwin/var/workspace/Teaching.Web.GR/node_modules/electron-dl/index.js:68:64)
at Session.emit (node:events:406:35)

The exception is not caught by my try .. catch because apparently it occurs in a tasklet.

const win is for sure null. That is why you get this error.

@corwin-of-amber
Copy link

corwin-of-amber commented Jan 25, 2022

Thanks for the reply. But that's exactly what the docs say:
https://github.com/sindresorhus/electron-dl#use-it-manually

Which also worked with a previous version of Electron, and this is why I attached my excerpt to this issue.
Why would BrowserWindow.getFocusedWindow return null?

I went ahead and printed it. It is not null.

@sKY02644
Copy link

Any solution to this please?

Facing the same issue

@Kiyozz
Copy link

Kiyozz commented Apr 8, 2022

I use latest electron-dl and Electron in my project. No issue so far.

Sample code

win is injected through function parameter, not with getFocusedWindow

import dl from 'electron-dl'  

void dl.download(this.win, mod.url, {
        filename: mod.name,
        onStarted: item => {
          modToUse = this.updateDownloadInStore(modToUse, {
            localFilePath: item.getSavePath(),
          })
          this.addDownloadInStore(modToUse, item)
          this.setPreviousTime(modToUse)
          this.startInterval(mod.id)
          resolve(modToUse)
        },
        onProgress: () => {
          this.sendIpcEvents.progressModDownload(this.modDownloadStore.get())
        },
        onCompleted: () => {
          modToUse = this.updateDownloadInStore(modToUse, {
            state: DownloadState.finish,
            downloadedSize: mod.size,
          })

          this.sendIpcEvents.finishModDownload(this.modDownloadStore.get())
          this.deletePreviousTime(modToUse)
        },
      })
    })
this.win: BrowserWindow
mod.url: string

@corwin-of-amber
Copy link

@Kiyozz thanks, but what is this.win in your case? Is this code part of a class definition (since you are using this)?

@Kiyozz
Copy link

Kiyozz commented Apr 8, 2022

Class method

startDownloadOfMod(mod: BaseModDownload): Promise<BaseModDownload> {
    // ...

    return new Promise<BaseModDownload>(resolve => {
      void dl.download(this.win, mod.url, {
        filename: mod.name,
        onStarted: item => {
          modToUse = this.updateDownloadInStore(modToUse, {
            localFilePath: item.getSavePath(),
          })
          this.addDownloadInStore(modToUse, item)
          this.setPreviousTime(modToUse)
          this.startInterval(mod.id)
          resolve(modToUse)
        },
      })
    })
  }

Class constructor

constructor(
    private http: HttpManager,
    private modDownloadStore: ModDownloadStore,
    private win: BrowserWindow,
    private sendIpcEvents: SendIpcEvents,
  ) {}

So in my main file, I create the window. I use a function I called initialize.

await initialize(win)

In initialize, I create the instance and I inject it.

const downloadManager = new DownloadManager(
    httpManager,
    modDownloadStore,
    win,
    sendIpcEvents,
  )

@corwin-of-amber
Copy link

Can anyone try this with their code base? (I kind of stopped using electron-dl in my code as a result of this issue; @OfekA-IAI, @sKY02644, perhaps?)

@CharlesBT
Copy link

You can use :
BrowserWindow.getAllWindows()[0]
or
BrowserWindow.fromId(1)

instead of BrowserWindow.getFocusedWindow() to retrieve the root window

@mykejnr
Copy link

mykejnr commented Feb 20, 2024

I encountered the same issue. It turned out to be the url passed to download function.

I was passing the url without the origin (https://hostname). I was passing in just the path (/path/to/file/to/download) without the origin.

I Later changed to pass in the full url (https://hostname:port/part/to/file/to/download.

This is what worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants