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

Electron 4.0.2 remote.getCurrentWindow() returns null inside webview preload script #16558

Closed
dannyseo517 opened this issue Jan 25, 2019 · 16 comments
Labels
4-2-x bug 🪲 bug/regression ↩️ A new version of Electron broke something platform/linux

Comments

@dannyseo517
Copy link

dannyseo517 commented Jan 25, 2019

  • Operating System (Platform and Version):
    Ubuntu 16.04

Expected Behavior
require('electron').remote.getCurrentWindow() returns BrowserWindow object

Actual behavior
require('electron').remote.getCurrentWindow() returns null

To Reproduce
https://github.com/kaniseo/electron-getCurrentWindow-bug
Instructions on reproduction in the readme file.

@welcome
Copy link

welcome bot commented Jan 25, 2019

👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

To help make it easier for us to investigate your issue, please follow the contributing guidelines.

@kenzaal
Copy link

kenzaal commented Jan 30, 2019

Hello !
can I work on this issue ?

@kenzaal
Copy link

kenzaal commented Feb 2, 2019

Hello !

The code of the file webview-preload.js:

const { remote } = require('electron');
document.addEventListener('DOMContentLoaded', () => {
    console.log(remote.getCurrentWindow());
    document.getElementById('container').innerHTML = remote.getCurrentWindow()
});

has been changed like below:


const { remote } = require('electron');
document.addEventListener('DOMContentLoaded', () => {
    console.log(remote);
    document.getElementById('container').innerHTML = remote
});

And this gives the expected result.

I would like to know if this resolves the issue asked previously?

@dannyseo517
Copy link
Author

Hey Kenzaal.
Unfortunately, it does not. The problem is not the remote returning null, but the getCurrentWindow() is what's returning null.

@VladimirPal
Copy link

I've faced the same issue!

@YasmineBOU
Copy link

YasmineBOU commented Feb 15, 2019

Hi!

We're students of the University of Paris 8 in France.
In one of our subjects, we have to contribute in an open source project.
We're actually working with kenzaal on this bug.
We have encountered difficulties while executing electron from the source code. Indeed after executing these commands (especially the last one):
npm install
npm start

the output given is like below:


> electron@4.0.2 start /home/user/codeSource/version-4.0.2/electron-4.0.2
> node ./script/start.js

events.js:173
      throw er; // Unhandled 'error' event
      ^

Error: spawn /home/user/codeSource/version-4.0.2/out/Debug/electron ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:19)
    at onErrorNT (internal/child_process.js:421:16)
    at processTicksAndRejections (internal/process/next_tick.js:76:17)
    at process.runNextTicks [as _tickCallback] (internal/process/next_tick.js:51:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:804:11)
    at executeUserCode (internal/bootstrap/node.js:526:15)
    at startMainThreadExecution (internal/bootstrap/node.js:439:3)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:252:12)
    at onErrorNT (internal/child_process.js:421:16)
    [... lines matching original stack trace ...]
    at startMainThreadExecution (internal/bootstrap/node.js:439:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! electron@4.0.2 start: `node ./script/start.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the electron@4.0.2 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/user/.npm/_logs/2019-02-15T15_04_26_147Z-debug.log

We have tried some of the solutions found from certain Github pages that deal with that kind of problems, unfortunately, it doesn't work for us.

We would like some help in order to start to deal with this bug as it should be.

Thank you in advance.

@kenzaal
Copy link

kenzaal commented Feb 23, 2019

Hi,
We just wanted to inform you that we are no longer working on this bug.

@harryi3t
Copy link

harryi3t commented Apr 10, 2019

Any update on this? This is a major blocker to upgrade electron for us.

PS: happening for mac as well on the latest version of electron 4.1.4

@grozdanovgg
Copy link

I have the same issue - remote.getCurrentWindow() always return null, after update to Electron 1.8.x to 4.x.x. Have not found a solution yet.

@harryi3t
Copy link

@grozdanovgg We have found a workaround.

  1. Some how make sure each browser window knows its ID. For this we are passing the ID in the query params while opening the window itself.
  2. Instead of using remote.getCurrentWindow() use remote.BrowserWindow.fromId(currentWindowId)

@grozdanovgg
Copy link

@harryi3t, thanks for the suggested workaround. I actually used another one -
In our app we only have single BrowserWindow, so I can reach it like this:
Instead of using remote.getCurrentWindow(), use remote.BrowserWindow.getAllWindows()[0].

Still, this approach works in that specific case, it is not general solution.

The bug with remote.getCurrentWindow() still remains...

@miniak
Copy link
Contributor

miniak commented Oct 29, 2019

I cannot reproduce it with Electron 4.2.12.

@miniak miniak closed this as completed Oct 29, 2019
@ipavlyk
Copy link

ipavlyk commented Feb 2, 2020

@grozdanovgg We have found a workaround.

  1. Some how make sure each browser window knows its ID. For this we are passing the ID in the query params while opening the window itself.
  2. Instead of using remote.getCurrentWindow() use remote.BrowserWindow.fromId(currentWindowId)

remote.BrowserWindow.fromId(currentWindowId) returns null:

Steps to reproduce:

  • inherit BrowserWindow class with some child class
  • create an instance using that child
  • try remote.BrowserWindow.fromId(currentWindowId)

@nana-yaw
Copy link

I am facing this issue on Electron 8.3.4 and none of the suggested workaround solved this issue. I need help please.

@linonetwo
Copy link

linonetwo commented Jul 28, 2020

This is still an issue in "electron": "9.1.0",

Maybe reopen this?

Screen Shot 2020-07-28 at 10 04 31 PM

@NeurAlch
Copy link

NeurAlch commented Oct 3, 2020

In case it helps anyone, I needed to do this: https://stackoverflow.com/questions/37884130/electron-remote-is-undefined/63612780#63612780

const w = new BrowserWindow({ webPreferences: { enableRemoteModule: true } });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4-2-x bug 🪲 bug/regression ↩️ A new version of Electron broke something platform/linux
Projects
None yet
Development

No branches or pull requests