How to Auto update using onclick in electronjs - electron

I am developing small application in electronJS and implemented auto update function working fine with log file. but I need with custom window option see attached image could you please update my code and attached my code below. What I am missing refer more references but could not solve this issue.
enter image description here
main.js
`
main.js
// Modules to control application life and create native browser window
const {app, BrowserWindow,ipcMain, dialog } = require('electron')
const path = require('path')
const { autoUpdater }=require('electron-updater')
const log = require('electron-log');
log.transports.file.resolvePath = () => path.join('F:/electronjs/test/electron-quick-start', '/logs/m.log');
log.info("Application version" + app.getVersion())
log.info('Hello, log');
log.warn('loading');
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// 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.whenReady().then(() => {
createWindow()
// updateInterval = setInterval(() => autoUpdater.checkForUpdates());
autoUpdater.checkForUpdatesAndNotify();
app.on('activate', function () {
// On macOS 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 (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
autoUpdater.on('update-available',() => {
log.info('update-available')
})
autoUpdater.on('checking-for-update',() => {
log.info('checking-for-update')
})
autoUpdater.on('download-progress',(progressTrack) => {
log.info('download-progress')
log.info(progressTrack)
})
autoUpdater.on('update-downloaded',() => {
log.info('update-downloaded')
})
Package.json
`
{
"name": "electron-quick-start",
"version": "8.0.9",
"description": "A minimal Electron application",
"main": "main.js",
"scripts": {
"start": "electron .",
"pack": "electron-builder",
"package": "electron-builder -p always"
},
"build": {
"publish": [
{
"provider": "github",
"owner": "vansiva",
"repo": "electron"
}
]
},
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"electron": "^21.2.3",
"electron-builder": "^23.6.0"
},
"dependencies": {
"electron-log": "^4.4.8",
"electron-updater": "^5.3.0"
}
}`
I need auto update with custom popup box

Related

White screen and no error in Angular electron app not loading index.html after building with electron builder

I have an angular electron app that works perfectly fine ( dev environment ) before bundling it with electron-builder. After bundling, install and opening it, a got a white screen without error !!
Here's the package.json file of my project
{
"name": "groceryee",
"productName": "Groceryee",
"authors": "seifou eddine",
"description": "groceryee",
"version": "9.3.1",
"scripts": {
"ng": "ng",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"start": "concurrently \"ng serve\" \"npm run electron\"",
"electron": "electron ./src/electron.dev",
"build-installer": "electron-builder"
},
"build": {
"appId": "com.groceryee.webconcepter",
"copyright": "Copyright © 2021 groceryee",
"asar":false,
"win": {
"target": [
"nsis"
],
"requestedExecutionLevel": "requireAdministrator"
},
"nsis": {
"oneClick": false,
"allowToChangeInstallationDirectory": true,
"uninstallDisplayName": "groceryee",
"license": "license.txt"
},
"extraMetadata": {
"main": "src/electron.prod.js"
}
}
}
Here's my electron.prod.js file of my project
const { app, BrowserWindow } = require('electron');
const path = require('path');
const url = require('url');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
const createWindow = () => {
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'favicon.ico'),
});
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
win.webContents.openDevTools();
// Emitted when the window is closed.
win.on('closed', () => {
// 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.
win = null;
});
}
// 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);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On macOS 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 (win === null) {
createWindow();
}
});
I'm really confused! i believe that all my code is correct, possible that I forgot something
Any ideas, please !!
Try to delete "requestedExecutionLevel": "requireAdministrator" option in package.json
Or change requestedExecutionLevel value to "asInvoker"
It works for me

Using create-react-app with electron-builder

I have create a simple app using create-react-app and made the following changes to package.json
{
"main": "public/electron.js",
"homepage": "./",
"scripts": {
"start": "node scripts/start.js",
"electron-dev": "concurrently \"BROWSER=none yarn start\" \"wait-on http://localhost:3000 && electron .\"",
"preelectron-pack": "yarn build",
"electron-pack": "electron-builder build -m",
"build": "node scripts/build.js",
"prettify": "prettier --write \"src/**/*.js\"",
"precommit": "yarn prettify",
"test": "node scripts/test.js --env=jsdom"
}
}
The electron.js file in the public folder
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const url = require('url')
const isDev = require('electron-is-dev')
let mainWindow
function createWindow() {
mainWindow = new BrowserWindow({
width: 1364,
height: 768,
webPreferences: {
webSecurity: false
}
})
mainWindow.setMinimumSize(1364, 768)
mainWindow.loadURL(
isDev
? 'http://localhost:3000'
: url.format({
pathname: path.join(__dirname, '../build/index.html'),
protocol: 'file:',
slashes: true
})
)
mainWindow.webContents.openDevTools()
mainWindow.on('closed', () => (mainWindow = null))
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
On running the script electron-pack it gives me this error:
not allowed to load local resource file:///index.html
What could be the possible issue?
react-scripts version: 1.1.5
electron-builder version: 20.28.2
I solved it.
The issue was with BrowserRouter of react-router. I had to change it to HashRouter and now all files and routes load properly.

registering custom protocol at installation process in electron app

Hi I am new to electron and was wondering how i can register a custom protocol for the app at the time of installation process of the app.
I am using electron-builder for building the app. Here is the build build code
"build": {
"appId": "com.test.testapp",
"productName": "testapp",
"asar": true,
"protocols": [{
"name": "testapp",
"schemes": [ "testapp" ]
}],
"nsis": {
"oneClick": false,
"perMachine": true,
"allowToChangeInstallationDirectory": true,
"runAfterFinish": false,
"createDesktopShortcut": true
},
"squirrelWindows": {
"msi": true
},
"directories": {
"output": "distribution"
}
I know that by adding the below line registers the custom protocol
app.setAsDefaultProtocolClient("testapp");
but it only does if i run the app at least the first time.
Which i don't want there is no guarantee that the user will launch the app after installation.
So is there a way that i can register the custom protocol in the installation process using electron-builder
I'm still new to Electron and electron-builder but solved this problem for NSIS-target already. First of all I should note that app.setAsDefaultProtocolClient used to handle custom protocols inside the application as far as I do understand. You need to register this custom protocol using electron-builder itself.
Secondly I need to choose between NSIS and squirrelWindows. NSIS is the preferable as long I understand because Squirrel is less supported and has some problems. Again I'm not an expert but I read something about it. So squirrelWindows section is redundant. You don't specify win.target and it is "nsis" by default.
There is a problem with the custom protocol registration for NSIS target. You can read more here: Protocol (scheme) for windows but there is a workaround. You need to create a file named installer.nsh in your build folder with such a content:
!macro customInstall
DetailPrint "Register evehq-ng URI Handler"
DeleteRegKey HKCR "evehq-ng"
WriteRegStr HKCR "evehq-ng" "" "URL:evehq-ng"
WriteRegStr HKCR "evehq-ng" "EveHQ NG SSO authentication Protocol" ""
WriteRegStr HKCR "evehq-ng\DefaultIcon" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}"
WriteRegStr HKCR "evehq-ng\shell" "" ""
WriteRegStr HKCR "evehq-ng\shell\Open" "" ""
WriteRegStr HKCR "evehq-ng\shell\Open\command" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME} %1"
!macroend
Replace evehq-ng with your protocol string and EveHQ NG SSO authentication Protocol with your protocol description.
After that you have to set nsis.perMachine to true.
I haven't yet solved this problem for Linux but work in this direction. You can see my code in my proof of concepts project here: EveHQ-NG proof of concepts application.
If you will solve this problem for Linux and MacOS write me a message somehow here or on GitHub.
Hope it helps.
Since you are using electron-builder you can do the following:
"build": {
... other top level electron-builder keys ...
"protocols": [
{
"name": "Custom Protocol Name",
"schemes": [
"customProtocol"
]
}
]
}
In case you wanted to handle customProtocol://some-link with your application.
More details:
https://www.electron.build/configuration/configuration
(Search for protocols; as of now, the documentaion is a little mis-formatted. It's supposed to be the same top-level key as the fileAssociations key above it.)
custom protocol is still opening only after opening the electron application. And I also added installer.nsh inside build folder. I don't know what's the problem.
package.json
{
"name": "electron-project",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "electron .",
"pack": "build --dir",
"dist": "build"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^3.0.8",
"electron-builder": "^20.38.5"
},
"dependencies": {
"axios": "^0.18.0"
},
"build": {
"win": {
"target": "nsis"
},
"nsis": {
"oneClick" : false ,
"allowToChangeInstallationDirectory": true,
"include" : "dist/installer.nsh" ,
"perMachine" : true
},
"protocols": [{
"name": "electron-deep-linking",
"schemes": [
"test"
]
}],
"mac": {
"category": "public.app-category.Reference"
}
}
}
main.js
const { app, BrowserWindow } = require('electron')
const path = require('path')
const url = require('url')
let mainWindow
function createWindow() {
mainWindow = new BrowserWindow({ width: 800, height: 600 })
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.setAsDefaultProtocolClient('test')
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
Electron providing a very simple way to registering custom protocol URL in client machine(Windows, Mac)
so this way we can register and remove our own custom protocol in machine
const {app} = require('electron')
app.on('ready', onReady)
function onReady() {
.... // write other code
if(!app.isDefaultProtocolClient('quickstart')) {
app.setAsDefaultProtocolClient("quickstart", 'C:\\Users\\karthi\\electron-quick-start\\electron-quick-start-win32-x64\\electron-quick-start.exe');
}
}
this code will register the custom protocol in machine, then you can open your app using browser like quickstart://params
for removing the custom protocol in machine
app.removeAsDefaultProtocolClient("quickstart", 'C:\\Users\\karthi\\electron-quick-start\\electron-quick-start-win32-x64\\electron-quick-start.exe');
here i used electron quick start app and i used electron-packager npm for building my app
For more information custom protocol electron-protocol

How to add an icon to electron application

I've got my electron build files for a win .exe and installer but the icons aren't mine. In my main.js file, I have code to attach the icon but I can only make it work inside of the createWindow function. Outside the function, I get an error message. The .exe will run and I get my icon, though it's I get an error in doing so; the installer won't work at all. I've tried going through several tutorials but none of them solve this problem.
main.js
const {app, BrowserWindow, Tray} = require('electron')
const path = require('path')
const url = require('url')
let win
function createWindow () {
const appIcon = new Tray('icon/app.png')
win = new BrowserWindow({ width: 1920, height: 1080, icon: 'icon/app.ico' })
console.log(appIcon, win)
win.loadURL(url.format({
pathname: path.join(__dirname, 'app/app.html'),
protocol: 'file:',
slashes: true
}))
win.on('closed', () => {
win = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (win === null) {
createWindow()
}
})
package.json
{
"name": "myapp",
"version": "1.0.0",
"description": "MyApp",
"private": true,
"main": "main.js",
"build": {
"appID": "myapp",
"productName": "MyApp",
"icon": "icon/app.ico"
},
"scripts": {
"start": "electron ." ,
"package": "",
},
"author": "Me",
"license": "ISC",
"devDependencies": {
"electron": "^1.6.1"
}
}
I'm not sure what to do from here.
Simple solution
const nativeImage = require('electron').nativeImage;
var image = nativeImage.createFromPath(__dirname + '/public/img/logo.png');
// where public folder on the root dir
image.setTemplateImage(true);
// Create the browser window.
win = new BrowserWindow({
width: 1179,
height: 754,
backgroundColor: '#ffffff',
transparent: false,
icon: image
})
inside the main.js
mainWindow = new BrowserWindow({width: 800, height: 600,icon: __dirname + '/icon.ico'});
and on the installer, if you are using electron-builder
"devDependencies": {
"electron": "^1.4.15",
"electron-builder": "^12.3.1"
},
make a folder on the root and named build inside that folder add your icon.ico
sometimes you need to restart the electron or build the app 2 times
Following worked for me. To display the app icon in the taskbar, you can update the icon on the fly in main.js (if using typescript then main.ts)
win.setIcon(path.join(__dirname, '/src/assets/logo-small.png'));
Worth to mention __dirname points to same directory as package.json

How to require native node module from electron? Getting error with ref and ffi module

when i require "ref" module in my js code and run via node, I get the desired output.
But when i run the same js code via electron by providing necessary changes in package.json, it says "Could not locate the binding file.."
Here is my package.json file
{
"name": "firstapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "electron .",
"package": "electron-packager . myapp --platform=win32 --arch=ia32 --version=1.0.0 --overwrite"
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^1.4.8",
"electron-prebuilt": "^1.4.8",
"electron-rebuild": "^1.4.0",
"ffi": "^2.2.0",
"node-gyp": "^3.4.0",
"reach": "^1.0.0",
"ref": "^1.3.3"
},
"dependencies": {
"ffi": "^2.2.0",
"ref": "^1.3.3"
}
}
And here is my index.js file
const electron = require('electron');
const ref = require('ref');
const app = electron.app
const BrowserWindow = electron.BrowserWindow
let mainWindow
const createWindow = () => {
mainWindow = new BrowserWindow({ name: "ishwar", width: 800, height: 600, visible: true, toolbar: false });
mainWindow.loadURL(__dirname + '/index.html');
mainWindow.webContents.openDevTools()
mainWindow.on('closed', () => {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
app.quit()
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
When i start the project "npm start" without requiring "ref" I get no error. But when i do with ref, it throws error.
P.S. The main requirement is for "ffi" module, and I have done necessary changes needed to run "ffi". "ffi" intern requires "ref", such that when i run code which includes ref via node, it works perfectly..
Somehow i managed to find the solution for this one. After I installed these package using npm command, I was getting the binding error. As a solution, I needed to run "electron-rebuild" command for that new package externally. Download electron-rebuild module with npm and then run "electorn-rebuild -f -w ffi" and "electron-rebuild -f -w ref". That's all, its working now.

Resources