Я делаю электронное приложение с cheerio. У меня есть main.js, который является точкой входа и создает mainWindow и childWindow. Я хотел бы захватить страницу из главного окна, но эта ошибка вылезает.
capturePage не работает ... поэтому я не могу сделать снимок экрана. мой indexjs предназначен для mainWindow.
Uncaught Error: Could not call remote function 'capturePage'. Check that the function signature is correct. Underlying error: Error processing argument at index 0, conversion failure from #<Object>
at callFunction (C:\Users\projects\electron\poc_v4\evm_app\node_modules\electron\dist\resources\electron.asar\browser\rpc-server.js:258:17)
at C:\Users\projects\electron\poc_v4\evm_app\node_modules\electron\dist\resources\electron.asar\browser\rpc-server.js:409:10
at EventEmitter.ipcMain.on.args (C:\Users\projects\electron\poc_v4\evm_app\node_modules\electron\dist\resources\electron.asar\browser\rpc-server.js:273:21)
at EventEmitter.emit (events.js:182:13)
at WebContents.<anonymous> (C:\Users\projects\electron\poc_v4\evm_app\node_modules\electron\dist\resources\electron.asar\browser\api\web-contents.js:368:21)
at WebContents.emit (events.js:182:13)
мой код похож на этот main.js
const url = require('url')
const path = require('path')
const { app, BrowserWindow, ipcMain } = require('electron')
let mainWindow = null
let childWindow = null
const mainUrl = url.format({
protocol: 'file',
slashes: true,
pathname: path.join(__dirname, 'app/index.html')
})
app.on('ready', function () {
mainWindow = new BrowserWindow({
center: true,
minWidth: 1024,
minHeight: 768,
show: false,
webPreferences: {
enableRemoteModule:true,
}
})
mainWindow.webContents.openDevTools()
mainWindow.loadURL(mainUrl)
mainWindow.webContents.on('dom-ready', function () {
console.log('user-agent:', mainWindow.webContents.getUserAgent());
mainWindow.webContents.openDevTools()
mainWindow.maximize()
mainWindow.show()
})
mainWindow.on('closed', function () {
mainWindow = null
app.quit()
})
childWindow = new BrowserWindow({
parent: mainWindow,
center: true,
minWidth: 800,
minHeight: 600,
show: false,
webPreferences: {
nodeIntegration: false, // https://electronjs.org/docs/tutorial/security#2-d%C3%A9sactiver-lint%C3%A9gration-de-nodejs-dans-tous-les-renderers-affichant-des-contenus-distants
preload: path.join(__dirname, 'app/js/preload.js')
}
})
childWindow.webContents.openDevTools()
childWindow.webContents.on('dom-ready', function () {
console.log('childWindow DOM-READY => send back html')
childWindow.send('sendbackhtml')
})
}) // app.on('ready'
// Quit when all windows are closed.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') { app.exit() }
})
ipcMain.on('scrapeurl', (event, url) => {
childWindow.loadURL(url, { userAgent: 'My Super Browser v2.0 Youpi Tralala !' })
})
ipcMain.on('hereishtml', (event, html) => {
mainWindow.send('extracthtml', html)
})
index.js
const cheerio = require('cheerio')
const {ipcRenderer } = require('electron')
const fs = require('fs');
const electron = require('electron')
const BrowserWindow = electron.remote.BrowserWindow;
let win = BrowserWindow.getFocusedWindow();
const webview = document.querySelector('webview')
const remote = electron.remote;
const webContents = remote.getCurrentWebContents();
const btn_go_back = document.getElementById('go_backBtn')
const btn_go_forward = document.getElementById('go_forwardBtn')
const scrapBtn = document.getElementById('scrapBtn')
const screenshotBtn = document.getElementById('screenshotBtn');
const searchBtn = document.getElementById('searchBtn');
console.log(document);
webview.addEventListener('did-stop-loading', () => {
let currentURL = webview.getURL()
let titlePage = webview.getTitle()
console.log('currentURL is : ' + currentURL)
console.log('titlePage is : ' + titlePage)
})
screenshotBtn.addEventListener('click', (e) => {
webContents.capturePage({
x:250,
y:0,
width:1500,
height:800,
}).then((img)=> {
dialog.showSaveDialog({
title: "Select the File Path to save",
// Default path to assets folder
defaultPath: path.join(__dirname,
"../assets/image.png"),
// defaultPath: path.join(__dirname,
// '../assets/image.jpeg'),
buttonLabel: "Save",
// Restricting the user to only Image Files.
filters: [
{
name: "Image Files",
extensions: ["png", "jpeg", "jpg"],
},
],
properties: [],
})
.then((file) => {
// Stating whether dialog operation was
// cancelled or not.
console.log(file.canceled);
if (!file.canceled) {
console.log(file.filePath.toString());
// Creating and Writing to the image.png file
// Can save the File as a jpeg file as well,
// by simply using img.toJPEG(100);
fs.writeFile(file.filePath.toString(),
img.toPNG(), "base64", function (err) {
if (err) throw err;
console.log("Saved!");
});
}
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});
});
Кто-нибудь может мне помочь?