asticode / go-astilectron

Build cross platform GUI apps with GO and HTML/JS/CSS (powered by Electron)
MIT License
4.9k stars 344 forks source link

unable to get electron objects, such as session, browserWindow #324

Closed dmitry84 closed 2 years ago

dmitry84 commented 3 years ago

Hi Probably I'm doing something in the wrong way, but this is what I want to achieve:

I want to get a session, cookie, and some other objects available from the electron for the client JS

In order to do so I've added const electron = require('electron');

into index.js of my app. When I log the object, it only shows the following properties/methods:

clipboard: (...)
contextBridge: (...)
crashReporter: (...)
desktopCapturer: (...)
ipcRenderer: (...)
nativeImage: (...)
shell: (...)
webFrame: (...)
deprecate: (...)
get clipboard: ()=>{const t=e();return t.__esModule&&t.default?t.default:t}
get contextBridge: ()=>{const t=e();return t.__esModule&&t.default?t.default:t}
get crashReporter: ()=>{const t=e();return t.__esModule&&t.default?t.default:t}
get desktopCapturer: ()=>{const t=e();return t.__esModule&&t.default?t.default:t}
get ipcRenderer: ()=>{const t=e();return t.__esModule&&t.default?t.default:t}
get nativeImage: ()=>{const t=e();return t.__esModule&&t.default?t.default:t}
get shell: ()=>{const t=e();return t.__esModule&&t.default?t.default:t}
get webFrame: ()=>{const t=e();return t.__esModule&&t.default?t.default:t}
get deprecate: ()=>{const t=e();return t.__esModule&&t.default?t.default:t}
__proto__: Object

tried to do this too const { session } = require('session') with no luck - object is undefined

in the same timeconst path = require('path') works.

in general, it would be nice to get some instructions on how to get electron modules available for client js

I see that you have a lot of objects imported in the compiled code: output/darwin-amd64/mytest.app/Contents/MacOS/vendor/astilectron/index.js but I can not understand how to make it work (get access to them) on my js.

p.s. The main reason why I need this - it looks like cookies are not always refreshed when I close the app, so when I reopen it again I see that it uses an old one, so to prevent this I want to call cookies.flushStore(). That is why I want to get the cookie from electron

asticode commented 3 years ago

You need to use the remote module to access session. However this module is deprecated and this won't work once astilectron uses version 12+ of Electron.

Therefore you have 2 solutions for your problem:

1) you still want to use the remote module (but you will have to manage Electron version manually). For that you'll need to set this attribute to true and then in your JS call something like const { session } = require('electron').remote to get the session (and then use session.defaultSession.cookies to get cookies) 2) we add a FlushCookiesStore() method to the GO Session struct. For that you'll need to create a PR, for which I'll guide you.

dmitry84 commented 3 years ago

Thank you very much for the answer. I'll try option 1 to make sure that I can get the result I wanted, I'm not familiar with the GO. If that works I'll see if I manage to do (2)