我知道电子正在努力使他们的api调用更加有效,但是我想我在此期间找到了解决方案,但是,它并不是很有效。
在我的创建窗口功能中,我有以下内容...
const mainSession = mainWindow.webContents.session
const getCookies = util.promisify(mainSession.cookies.get)
const cookies = await getCookies({})
console.log(cookies)
我相信它会在我根据此错误得到的诺言解决之前破坏对象。
(node:21863) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Object has been destroyed
我需要同步cookie的原因是因为我正在为我的documentation应用构建登录方法,以生成长期访问令牌(以验证同步)。该方法必须具有足够的动态性,以适用于任何学校的登录页面,在该页面上,该方法实质上将“继续尝试”制作长期访问令牌,直到成功为止。
我想您可以想象,使这种同步确实可以简化流程。
任何帮助或指针将不胜感激!
编辑:添加了所有相关代码
const electron = require('electron')
const util = require('util')
const _ = require('lodash')
const request = require('request-promise')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const url = require('url')
let mainWindow
async function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
mainWindow.loadURL('https://suu.instructure.com')
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
try {
const mainSession = mainWindow.webContents.session
const getCookies = util.promisify(mainSession.cookies.get)
const cookies = await getCookies({})
console.log(cookies)
} catch(err) {
console.log(err)
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)