electron: How to set the proxy of the child window - javascript

I try to loadurl https://www.google.com and always get an error electron: Failed to load URL: https://www.google.com/ with error: ERR_PROXY_CONNECTION_FAILED . This problem has been bothering me for a week and I hope I can get your help
newWin = new BrowserWindow({
width: 400,
height: 600,
frame: true,
maximizable: true,
webPreferences: {
devTools: true,
webviewTag: true,
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false,
preload: link
},
});
newWin.index = arg.index;
newWin.type = arg.type;
//I have set ip and port here
newWin.webContents.session.setProxy({
proxyRules: "http://129.226.148.192:3128",
}).then(() => {
newWin.loadURL(`${arg.href}`);
}).catch((err) => console.error(err));
// I have configured the proxy account and password when I am here (login)
newWin.on('login', (event, webContents, details, authInfo, callback) => {
event.preventDefault();
if (authInfo.isProxy) {
callback('sub_1kni3wjb4g9i6pkhmbsz5qzd', 'stat900')
}
});
If anyone knows how to answer it would be greatly appreciated.

Related

Can Electron read DOM from BrowserView?

is it possible to read DOM of website with given url in BrowserView?
I have code that looks like this one, and Electron is throwing an error:
"An object could not be cloned"
// Create the browser window.
const mainWindow = new BrowserWindow({
height: 800,
width: 1200,
webPreferences: {
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
},
});
const view = new BrowserView({
webPreferences: {
webSecurity: false,
allowRunningInsecureContent: true,
},
});
mainWindow.setBrowserView(view);
view.setBounds({ x: 200, y: 0, width: 900, height: 800 });
view.webContents.loadURL("https://electronjs.org");
view.setAutoResize({ width: true, height: true });
view.webContents
.executeJavaScript("document.querySelector('body')")
.then((result) => {
console.log("html", result);
})
.catch((err) => console.log(err));
// and load the index.html of the app.
mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);
// Open the DevTools.
mainWindow.webContents.openDevTools();
};```

I use Electron 'webContents.print' a HTML file, but no callback, and printer doesn't work

const printWindow = new BrowserWindow({
width: 800,
height: 600,
show: false,
webPreferences: {
nodeIntegration: true,
webSecurity: false,
devTools: false,
},
dpi: 96,
});
const options = {
silent: true,
deviceName: SelectedPrinter,
}
const callback = (success, errorType) => {
if (!success) console.log(errorType)
}
printWindow.loadURL(tmpPath);
printWindow.webContents.on('did-finish-load', () => {
printWindow.webContents.print(options, callback)
})
That's my code upon, I create a document as HTML file, and load in a new BrowerWindow.
On some computer, there OS are all windows 7, doen't have callback.
It can executing the function 'webContent.print', and no callback response.
I saw printer has received an HTML file, but pages is N/A, and size is empty.
I don't know what is going on those devices, is that an OS problem or a hardware problem?

Minimize and close buttons not working in Electron app

I'm making an app using Electron framework and I tried using buttons to close and minimize the window. I've tried multiple things for it to do this but without success.
This is what I have for the moment:
HTML:
<body>
<!-- Titlebar -->
<button id="minimize-button">-</button>
<button id="close-button">x</button>
<!-- Script -->
<script src="./js/minimize-close.js"></script>
</body>
JS (minimize-close.js):
const { ipcRenderer } = require('electron');
document.getElementById("minimize-button").addEventListener('click', () => {
ipcRenderer.send('minimize-window');
});
document.getElementById("close-button").addEventListener('click', () => {
ipcRenderer.send('close-window');
});
JS (index.js):
const { app, BrowserWindow, ipcMain } = require('electron');
function createWindow(){
const window = new BrowserWindow({
width: 960, height: 580,
resizable: false, maximizable: false,
frame: false, autoHideMenuBar: true,
icon: './icon.ico',
webPreferences: {
nodeIntegration: true,
devTools: false
}
});
window.loadFile('./src/index.html');
}
// Minimize and close window
ipcMain.on('minimize-window', () => {
window.minimize();
});
ipcMain.on('close-window', () => {
window.close();
});
app.whenReady().then(() => {
createWindow();
app.on('activate', function(){
if(BrowserWindow.getAllWindows().length === 0){
createWindow();
}
});
});
app.on('window-all-closed', function(){
if(process.platform !== 'darwin'){
app.quit();
}
});
You need to set contextIsolation: false.
Also I have read around that window must be global. If not at some point it will be collected by the garbage collector because function which created it has already finished.
let window;
function createWindow(){
window = new BrowserWindow({
width: 960, height: 580,
resizable: false, maximizable: false,
frame: false, autoHideMenuBar: true,
icon: './icon.ico',
webPreferences: {
nodeIntegration: true,
// devTools: false, // commented for debugging purposes
contextIsolation: false
}
});
window.loadFile('./src/index.html');
window.webContents.openDevTools(); // Open dev tools to see if any error arised. In this case I saw 'require is not defined' before setting contextIsolation. Remove it when going into production.
}
Option B: more "secure"
If security is a concern and you want nodeIntegration and contextIsolation to retain their default secure values then preload.js scheme is needed.
This should be the case if remote content is loaded by your App.
HTML
<body>
<!-- Titlebar -->
<button id="minimize-button">-</button>
<button id="close-button">x</button>
</body>
preload.js
const { ipcRenderer } = require('electron');
window.addEventListener('DOMContentLoaded', () => {
document.getElementById("minimize-button").addEventListener('click', () => {
ipcRenderer.send('minimize-window');
});
document.getElementById("close-button").addEventListener('click', () => {
ipcRenderer.send('close-window');
});
})
index.js
const path = require('path');
function createWindow(){
window = new BrowserWindow({
width: 960, height: 580,
resizable: false, maximizable: false,
frame: false, autoHideMenuBar: true,
icon: './icon.ico',
webPreferences: {
// devTools: false, // commented for debugging purposes
preload: path.join(__dirname, 'preload.js')
}
});
window.loadFile('./src/index.html');
window.webContents.openDevTools(); // Open dev tools to see if any error arised. In this case I saw 'require is not defined' before setting contextIsolation. Remove it when going into production.
}

How to detect system idle in Electron app and do something

I developed app and I want to make it if the user do nothing in 30 minutes close the app.
So I search it on google to solve this problem and I found that there is 'powerMonitor' API in electron but the problem is my app is old version (Electron 3.1.14) so it doesn't support various methods.
Electron powerMonitor API Docs
I think I should use powerMonitor.querySystemIdleTime() method and I tried to test 'pwoerMonitor' API so I paste code but
console.log(powerMonitor.querySystemIdleTime()) it returns this error message.
How can I detect system idle and do something in electron? Should I use another package?
import { app, dialog, Menu, Notification, Tray, ipcMain, powerMonitor } from 'electron'
import windowManager from 'electron-window-manager'
import schedule from 'node-schedule'
let mainWindow = null
let tray = null
if (process.env.NODE_ENV !== 'development') {
global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}
app.setAppUserModelId('Geomec Cloud Manager')
windowManager.init({
appBase: process.env.NODE_ENV === 'production'
? `file://${__dirname}`
: 'http://localhost:9080'
})
if (isSecondInstance) {
app.quit()
} else {
app.on('second-instance', () => {
if (mainWindow) {
mainWindow.object.show()
mainWindow.object.focus()
}
})
/* Main Window */
app.on('ready', () => {
mainWindow = windowManager.createNew('main-window', '', '/index.html', null, {
title: 'Geomec Cloud Manager',
height: 500,
width: 500,
useContentSize: true,
frame: false,
maximizable: false,
minimizable: false,
resizable: false
}).create()
console.log(powerMonitor.querySystemIdleTime()) // I thought it returns idle time
if (!process.argv.includes('--systray')) {
mainWindow.object.once('ready-to-show', () => {
mainWindow.object.show()
})
}
tray = new Tray(__static + '/tray-icon.ico')
const trayMenu = Menu.buildFromTemplate([{
label: '프로그램 정보',
click () {
const window = windowManager.createNew('about-window', '', '/index.html#about', null, {
height: 380,
width: 550,
useContentSize: true,
frame: false,
maximizable: false,
minimizable: false,
resizable: false,
modal: true,
parent: windowManager.get('main-window').object
}).create()
window.object.once('ready-to-show', () => {
window.object.show()
})
}
},
{
/* Tray 내부 프로그램 종료 함수 */
label: '프로그램 종료',
click () {
const notification = {
icon: __static + '/' + 'app-icon.png',
title: '네트워크 드라이브 연결이 해제되었습니다',
body: '프로그램을 종료합니다'
}
new Notification(notification).show()
mainWindow.object.webContents.send('terminate-child-processes')
ipcMain.on('child-processes-terminated', () => {
setTimeout(function () {
app.quit()
}, 5000)
})
}
}
])
tray.setToolTip('Geomec Cloud Manager')
tray.setContextMenu(trayMenu)
tray.on('click', () => {
mainWindow.object.show()
})
})
}
According to the docs the call is: powerMonitor.querySystemIdleTime(callback) and as the error message says, you are not giving the required argument.
Try changing:
console.log(powerMonitor.querySystemIdleTime())
to:
powerMonitor.querySystemIdleTime((idleSecs)=>{
console.log(`The system has been idle for ${idleSecs} seconds.`)
})
Based on your use case, it is actually powerMonitor.querySystemIdleState(idleThreshold, callback) you will want to be using, with idleThreshold set to 30 * 60.

Is there anyway to get videojs-record to work on phone?

Hi so as you can see i have a problem with videojs-recorder, problem is i can't get access to my cam on phone. I use code down below and i am testing it with remote dev tools on chrome. Error is:
VIDEOJS: ERROR: TypeError: Cannot read property 'getUserMedia' of undefined
This is my code:
let player;
const options = {
controls: true,
width: 300,
height: 300,
plugins: {
record: {
audio: true,
screen: true,
image: false,
maxLength: 3600,
debug: true,
}
}
};
player = videojs("myVideo", options , function(){
// print version information at startup
videojs.log('Using video.js', videojs.VERSION,
'with videojs-record', videojs.getPluginVersion('record'),
'and recordrtc', RecordRTC.version);
});
// error handling
player.on('deviceError', function() {
console.log('device error:', player.deviceErrorCode);
});
player.on('error', function(element, error) {
console.error(error);
});
player.on('startRecord', function() {
console.log('started recording!');
});
player.on('finishRecord', function() {
console.log('finished recording: ', player.recordedData);
});
Edit:
Currently i am using 2.1.2 and i am unable to do update.

Categories