So far I build a simple Electron application. My problem is the input.focus() is not working on displaying an alert box. I tried to solve the problem, and I came up with a solution: when I minimize and maximize the window, the input.focus() is working well. So when I try to show an alert box, the input.focus() isn't working, except minimize and maximizing. I try to open the code in Chrome, and all functionalities are working very well, so the problem is in the Electron renderer.
Before minimizing and maximizing the window
After minimizing and maximizing the window
My Electron renderer
const path = require("path");
const { app, BrowserWindow } = require("electron");
const createWindow = () => {
const win = new BrowserWindow({
width: 780,
height: 600,
minWidth: 780,
minHeight: 600,
icon: path.join(__dirname, "assets/favicon.ico"),
webPreferences: {},
});
win.maximize();
// win.removeMenu();
win.loadFile("index.html");
};
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0)
createWindow();
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin")
app.quit();
});
The Input focus will lose if the app window not focused.
When you minimize and maximize the app window the input focus come back.
And When you send alert box the alert open in another window so the main app window blur and its input focus blur.
Related
I have a list. Click one item and then pop-up window which is named open. It's focused when it first opens, but it's out of focus the second time.
enter image description here
when I click a button, the following code execute. But, winObj.focus() not working in electron. It's only focus the main window. How do I fix it?
chatList.js (build in react)
const onClickBtn = useCallback(
(chat) => {
const winObj = window.open(
`/chatting/${chat.url}`,
chat.url,
"",
true
);
winObj.focus();
},
[]
);
electron.js (same as public/main.js)
function createWindow() {
const win = new BrowserWindow({
width: 400,
height: 700,
webPreferences: {
nodeIntegration: true,
nativeWindowOpen: true,
preload: path.join(__dirname, "/../build/preload.js"),
},
});
const pubUrl = "https://---";
win.loadURL(pubUrl);
//when i click the button above, the event come into here.
win.webContents.setWindowOpenHandler((event) => {
return {
action: "allow",
overrideBrowserWindowOptions: {
...win.webContents.browserWindowOptions,
parent: win,
},
};
});
}
This is an existing bug with Electron.
win.focus() will not actually focus the BrowserWindow. You'll want to send an ipc message down to the main process, grab your BrowserWindow and call win.focus() from there.
I implemented a touch bar button to my program which loads a file. My question is: How can I click to a specific button on the website after that page has loaded when I press the touch bar button? Here is my main.js file:
const { app, BrowserWindow, TouchBar } = require('electron')
const { TouchBarButton } = TouchBar
const path = require('path')
let window
function createWindow() {
const win = new BrowserWindow({
width: 1366,
height: 758,
minWidth: 1300,
minHeight: 700,
icon: __dirname + 'logoSquare.icns',
webPreferences: {
//preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile('login.html')
win.maximize()
return win
}
app.whenReady().then(() => {
window = createWindow()
const button = new TouchBarButton({
label: `🛫 Add Flight`,
accessibilityLabel: 'Add Flight',
backgroundColor: '#a20021',
click: () => {
window.loadFile('./flights.html')
},
});
const touchBar = new TouchBar({
items: [
button,
],
})
window.setTouchBar(touchBar);
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
I have just started learning electron.js and couldn't find a solution yet.
I found an alternate way to solve this issue. I sent a query and got that information with JavaScript. This is the main.js file that is responsible for sending the query:
const button = new TouchBarButton({
label: `🛫 Add Flight`,
accessibilityLabel: 'Add Flight',
backgroundColor: '#3a2e39',
click: () => {
//window.loadFile('./flights.html?addflight=1')
window.loadURL('file://'+__dirname+'/flights.html?addflight=1')
},
})
And this is the code that is responsible for receiving data in the flights.html file with JavaScript:
function addFlightRedir() {
if (location.search == '?addflight=1') {
addNewFlight(); // This is the code that I wanted to execute
}
}
addFlightRedir();
And since it is reusable with standard JS, now I can implement it to the other areas of my program as well (independent from the Touch Bar buttons).
I am trying to add thumbnail toolbar with specified buttons in a taskbar layout of an application from it's doc. But it's showing some problem.
The main.js:
// Modules to control application life and create native browser window
const {app, BrowserWindow, ipcMain, dialog} = require('electron')
const path = require('path')
// 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 mainWindow;
const isSecondInstance = app.makeSingleInstance((commandLine, workingDirectory) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
})
if (isSecondInstance) {
app.quit();
}
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
show: false,
width: 500,
height: 200,
minWidth: 500,
minHeight: 200,
transparent: true,
frame: false,
})
mainWindow.setResizable(false)
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
mainWindow.once('ready-to-show', () => {
mainWindow.show()
mainWindow.focus()
})
// Emitted when the window is closed.
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
})
}
// 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', function () {
// On OS X 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', 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()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
ipcMain.on('open-information-dialog', (event) => {
if (mainWindow) {
const options = {
type: 'info',
title: 'I am abled',
buttons: ['Ok'],
message: 'This is the way.'
}
dialog.showMessageBox(mainWindow, options, function () {})
}
})
ipcMain.on('close', (event) => {
app.quit()
})
ipcMain.on('minimize', (event) => {
mainWindow.minimize()
})
ipcMain.on('progress', (event, per) => {
mainWindow.setProgressBar(per)
})
mainWindow.setThumbarButtons([
{
tooltip: 'button1',
icon: path.join(__dirname, 'button1.png'),
click () { console.log('button1 clicked') }
}, {
tooltip: 'button2',
icon: path.join(__dirname, 'button2.png'),
flags: ['enabled', 'dismissonclick'],
click () { console.log('button2 clicked.') }
}
])
The error:
All of my mainWindow reference working fine but when I am trying to set thumbar buttons with setThumbarButtons() it's getting the problem. I just try this one and I don't get any error but it's not showing any buttons on taskber window,
The Code:
app.on('ready', function(){
console.log(mainWindow)
mainWindow.setThumbarButtons([
{
tooltip: 'button1',
icon: path.join(__dirname, 'start.png'),
click () { console.log('button1 clicked') }
}
])
})
I can't make any sense with this problem.
The problem is that when you run mainWindow.setThumbarButtons your window hasn't been created. Therefor mainWindow is undefined. Which is the error you are running into, and what it says in the error screenshot you posted.
Currently you are creating and setting your window in the createWindow function.
If you move your setThumbarButtons code into the createWindow function it should work.
Something like the following:
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
show: false,
width: 500,
height: 200,
minWidth: 500,
minHeight: 200,
transparent: true,
frame: false,
})
mainWindow.setResizable(false)
// and load the index.html of the app.
mainWindow.loadFile('index.html')
mainWindow.setThumbarButtons([
{
tooltip: 'button1',
icon: path.join(__dirname, 'start.png'),
click () { console.log('button1 clicked') }
}
])
// Open the DevTools.
// mainWindow.webContents.openDevTools()
mainWindow.once('ready-to-show', () => {
mainWindow.show()
mainWindow.focus()
})
// Emitted when the window is closed.
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
})
}
That way you are creating the mainWindow first and defining it before calling that function. Otherwise mainWindow will be undefined which is what you were experiencing.
I am trying to create my own version of http://meetfranz.com/ using Electron.
The app should allow up multiple URLs such as https://messenger.com/ and https://gmail.com/ to be visible and have a tabbed interface.
I've tried both generating Webview and BrowserWindow.
WebView can't seem to load Messenger completely (does not load login)
BrowserWindow pops out of the main window…
I tried earlier on also with iFrames which was a no-go.
Any ideas on best way to accomplish a tabbed minimal browser interface that allows cookies/https?
index.html
<html>
<head>
<style>
webview {
display: inline-flex;
width: 800px;
height: 600px;
}
</style>
</head>
<body>
<form name="form">
<input name="input" placeholder="https://messenger.com/" value="https://messenger.com">
<button type="submit">submit</button>
</form>
<div class="tabs">
</div>
<div class="webviews">
</div>
<!--<webview src="https://messenger.com/" autosize="on" nodeintegration="on" disablewebsecurity="on" webpreferences="allowRunningInsecureContent"></webview>-->
<script type="text/javascript">
require('./app.js')
</script>
</body>
</html>
main.js
const {app, BrowserWindow, BrowserView} = require('electron')
app.on('ready', createWindow)
function createWindow(){
let win = new BrowserWindow({
width: 1000,
height: 600,
height: 600,
"node-integration": "iframe", // and this line
"web-preferences": {
"web-security": false,
"nodeIntegration": false,
}
})
win.on('closed', () => {
win = null
})
win.webContents.openDevTools()
// Load a remote URL
//win.loadURL('https://github.com')
// Or load a local HTML file
win.loadURL(`file://${__dirname}/index.html`)
/*win.webContents.session.webRequest.onHeadersReceived({}, (d, c) => {
if(d.responseHeaders['x-frame-options'] || d.responseHeaders['X-Frame-Options']){
delete d.responseHeaders['x-frame-options']
delete d.responseHeaders['X-Frame-Options']
}
c({cancel: false, responseHeaders: d.responseHeaders})
})*/
}
//app.commandLine.appendSwitch('disable-web-security')
app.js
const {app, BrowserWindow, BrowserView} = require('electron').remote
let tabs = document.querySelector(".tabs")
let webviews = document.querySelector(".webviews")
document.getElementsByTagName("form")[0].onsubmit = function(event){
//createWebview(form.input.value)
createBrowserWindow(form.input.value)
return false;
}
function createWebview(url){
let webview = new WebView()
webview.setAttribute("autosize","on")
webview.setAttribute("nodeintegration","on")
webview.setAttribute("disablewebsecurity","on")
webview.setAttribute("webpreferences","allowRunningInsecureContent")
webview.src = url
webviews.appendChild(webview)
let tab = document.createElement("div")
tab.textContent = url
tab.target = webview
tabs.appendChild(tab)
}
function createBrowserWindow(url){
let win = new BrowserWindow({
width: 800,
height: 600,
y: 100,
x: 100,
webPreferences: {
webSecurity: false,
nodeIntegration: false
}
})
win.setMenu(null)
win.on('closed', () => {
win = null
})
view = new BrowserView({
webPreferences: {
nodeIntegration: false
}
})
win.webContents.openDevTools()
// Load a remote URL
win.loadURL(url)
}
<webview> clearly is the way to if you want to have a single window. It's also a lot better than an <iframe>, because it's securely isolated from your app and runs in a separate process.
See the docs: https://electron.atom.io/docs/api/webview-tag/
If messenger.com doesn't load properly, this should be the problem you should be addressing (e.g. inspect console messages, network log). Follow your instincts, your first choice was the right one, now it's about making it work.
In Electron I want to trigger a javascript function in a browser window when that browser window is shown.
The code I have at the moment is as follows:
main.js (Main Process)
myWin = new BrowserWindow({ width: 1200, height: 400, show: false })
.... some time later and under certain circumstances ;-) ....
myWin.show()
usbUpload.js (Browser Window)
function validateFlights() {
...blar...
}
this.addEventListener('onshow', () => {
validateFlights()
})
ValidateFlights() is the function in the browser window that I want to execute when the browser window is shown.
Any ideas?
You can directly call javascript in the renderer process from main process using executeJavaScript. Combined with 'show' event of BrowserWindow you can do the following:
myWin.on('show', () => {
myWin.webContents.executeJavaScript('validateFlights()')
})
Example:
main.js
const { app, BrowserWindow } = require('electron')
const path = require('path')
app.once('ready', () => {
let win = new BrowserWindow({show: false})
win.once('show', () => {
win.webContents.executeJavaScript('validateFlights()')
})
win.loadURL(path.resolve(__dirname, 'index.html'))
win.show()
})
index.html
<html>
<head>
<script type="text/javascript">
function validateFlights() {
console.log('validated')
}
</script>
</head>
<body></body>
</html>
In your main process you probably want to do something with win.show() and the focus event if your window is already open in the background. Something like so:
let win = new BrowserWindow({width: 800, height: 600})
win.on('onFocus', () => {
win.show()
})