【问题标题】:uncaught error : could not call remote function 'capturePage' on electron app未捕获的错误:无法在电子应用程序上调用远程函数“capturePage”
【发布时间】:2021-02-04 19:16:59
【问题描述】:

我正在用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); 
  }); 
});

有人可以帮我吗?

【问题讨论】:

    标签: electron cheerio


    【解决方案1】:

    查看电子版;

    在新版本上工作的代码 'capturePage({x:x,y:y,width:width,height:height}).then(img=>{})' 喜欢 '12.x.x'

    在旧版本上工作的代码“capturePage((img)=>{})”喜欢“2.x.x”

    如果参数和函数要求不匹配,它会抛出“索引 0 处的错误处理参数,# 的转换失败”。

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 2018-08-09
      • 2015-03-18
      • 2018-03-15
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      相关资源
      最近更新 更多