gpt4 book ai didi

electron - Uncaught Error : could not call remote function 'capturePage' on electron app

转载 作者:行者123 更新时间:2023-12-03 12:37:21 29 4
gpt4 key购买 nike

我正在用cheerio进行 Electron 应用。
我有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);
});
});
有人能帮我吗?

最佳答案

检查 Electron 版本;
在新版本上工作的代码'capturePage({x:x,y:y,width:width,height:height})。then(img => {})'喜欢'12 .x.x'
在旧版本上工作的代码'capturePage((img)=> {})'喜欢'2.x.x'
如果参数和函数要求不匹配,则会抛出“索引0处的参数处理错误,#转换失败”。

关于electron - Uncaught Error : could not call remote function 'capturePage' on electron app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66052359/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com