简体   繁体   English

单击电子自动更新

[英]electron auto-update on click

I'm trying to make my electron app auto-updatable, and after searching on google I found nice guide that works. 我正在尝试使我的电子应用程序可自动更新,在Google上搜索后,我发现了不错的指南。 The only problem is that I want to create a button for when update is downloaded so that the user can decide when he wants to update and restart the app. 唯一的问题是我想为下载更新的时间创建一个按钮,以便用户可以决定何时更新和重新启动应用程序。 So far I was able to put this code together 到目前为止,我已经能够将这段代码放在一起

renderer.js renderer.js

const electron = require('electron')
const ipcRenderer = electron.ipcRenderer

let lastMsgId = 0

window.quitAndInstall = function () {
  electron.remote.autoUpdater.quitAndInstall()
}

ipcRenderer.on('console', (event, consoleMsg) => {
  console.log(consoleMsg)
})

ipcRenderer.on('message', (event, data) => {
  showMessage(data.msg, data.hide, data.replaceAll)
})

function showMessage(message, hide = true, replaceAll = false) {
  const messagesContainer = document.querySelector('.messages-container')
  const msgId = lastMsgId++ + 1
  const msgTemplate = `<div id="${msgId}" class="alert alert-info alert-info-message animated fadeIn">${message}</div>`

  if (replaceAll) {
    messagesContainer.innerHTML = msgTemplate
  } else {
    messagesContainer.insertAdjacentHTML('afterbegin', msgTemplate)
  }

  if (hide) {
    setTimeout(() => {
      const msgEl = document.getElementById(msgId)
      msgEl.classList.remove('fadeIn')
      msgEl.classList.add('fadeOut')
    }, 4000)
  }
}

and this is my index.js where messages are storred 这是我存储邮件的index.js

const electron = require('electron');
const {autoUpdater} = require('electron-updater');
const log = require('electron-log');
const appVersion = require('./package.json').version

// configure logging
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');

const app = electron.app;
const BrowserWindow = electron.BrowserWindow;

let mainWindow;

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 1020,
    height: 800,
  });
  mainWindow.loadURL('file://' +__dirname + '/public/index.html');

  // Open the DevTools.
  //mainWindow.webContents.openDevTools();

  mainWindow.on('closed', function() {
    mainWindow = null;
  });
}

app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function() {
    app.quit();
});

app.on('activate', function() {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow();
  }
});

//-------------------------------------------------------------------
// Auto updates
//-------------------------------------------------------------------
const sendStatusToWindow = (text) => {
  log.info(text);
  if (mainWindow) {
    mainWindow.webContents.send('console', `App version: ${appVersion}`)
    mainWindow.webContents.send('message', { msg: `App version: ${appVersion}` })
  }
};

autoUpdater.on('error', (ev, err) => {
  mainWindow.webContents.send('message', { msg: `Error: ${err}` })
})

autoUpdater.once('checking-for-update', (ev, err) => {
  mainWindow.webContents.send('message', { msg: 'Checking for updates' })
})

autoUpdater.once('update-available', (ev, err) => {
  mainWindow.webContents.send('message', { msg: 'Update available. Downloading ⌛️', hide: false })
})

autoUpdater.once('update-not-available', (ev, err) => {
  mainWindow.webContents.send('message', { msg: 'Update not available' })
})

autoUpdater.once('update-downloaded', (ev, err) => {
  const msg = 'Update downloaded - <button onclick="quitAndInstall()">Restart</button>'
  mainWindow.webContents.send('message', { msg, hide: false, replaceAll: true })
})

autoUpdater.checkForUpdates()

As you can see I added a button to call for function but it doesnt work. 如您所见,我添加了一个按钮来调用函数,但是它不起作用。 When I press the button nothing happens. 当我按下按钮时,什么也没发生。 If I remove button and just say auto.updater.quitAndInstall() it works. 如果我删除按钮,只是说auto.updater.quitAndInstall(),它就可以工作。 It auto close window and install new version. 它会自动关闭窗口并安装新版本。 What am I missing? 我想念什么?

I think it's that electron.remote.autoUpdater.quitAndInstall() doesn't work when run in the renderer. 我认为这是在渲染器中运行时electron.remote.autoUpdater.quitAndInstall()不起作用的原因。

In the docs it doesn't say anything against running it in the renderer process but I think sending a message back to the main process to run the quitAndInstall function would work. 在文档中,它并没有说反对在渲染器进程中运行它,但是我认为向主进程发送一条消息以运行quitAndInstall函数是可行的。

Inside of the quitAndInstall function put this instead: quitAndInstall函数内部,将其改为:

ipcRenderer.send('asynchronous-message', 'quitAndInstall');

Then in the main process put: 然后在主过程中输入:

electron.ipcMain.on('asynchronous-message', function (evt, message) {
    if (message == 'quitAndInstall') {
        autoUpdater.quitAndInstall();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM