如何解决未捕获的错误:无法在电子应用程序上调用远程函数“capturePage”
我正在用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,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',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);
});
});
有人可以帮我吗?
解决方法
查看电子版;
在新版本上工作的代码'capturePage({x:x,y:y,width:width,height:height}).then(img=>{})'像'12.x.x'
在旧版本上工作的代码 'capturePage((img)=>{})' 喜欢 '2.x.x'
如果参数和函数要求不匹配,它会抛出“在索引 0 处处理参数时出错,从# 转换失败”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。