gpt4 book ai didi

javascript - Electron JS中的capturePage

转载 作者:行者123 更新时间:2023-12-03 12:33:39 25 4
gpt4 key购买 nike

这是我的代码。我想截取浏览器窗口的屏幕截图。但是它并没有像预期的那样截屏。窗口不在屏幕上。我想在页面加载时尽快获取屏幕截图并将其转换为PNG。有人可以帮忙吗

const {BrowserWindow} = require('electron')

// Offscreen BrowserWindow
let offscreenWindow

// Exported readItem function
module.exports = (url, callback) => {

// Create offscreen window
offscreenWindow = new BrowserWindow({
width: 500,
height: 500,
show: false,
webPreferences: {
offscreen: true
}
})

// Load item url
offscreenWindow.loadURL(url)
console.log("readitem")

// Wait for content to finish loading
offscreenWindow.webContents.on('did-stop-loading', () => {

// Get page title
let title = offscreenWindow.getTitle()
console.log(title)

// Get screenshot (thumbnail)
offscreenWindow.capturePage( image => {

fs.writeFile('test.png', image.toPNG(), (err) => {
if (err) throw err
console.log('It\'s saved!')})})


// Get image as dataURL
//let screenshot = image.toDataURL()


// Execute callback with new item object
callback({ title, url })

// Clean up
offscreenWindow.close()
offscreenWindow = null

})}

最佳答案

我使用您创建了此文件。它的工作和测试。您需要使用contents.capturePage([rect])
offscreenWindow.webContents.capturePage().then(image=>{ //something done})然后使用fs.writeSync()将图像同步写入dis

screenshot.js

const { BrowserWindow } = require('electron')
const fs = require('fs')
const path = require('path')
// Offscreen BrowserWindow
let offscreenWindow
let nativeImage
// Exported readItem function
module.exports = (url, callback) => {

// Create offscreen window
offscreenWindow = new BrowserWindow({
width: 500,
height: 500,
show: false,
webPreferences: {
offscreen: true
}
})

// Load item url
offscreenWindow.loadURL(url)
console.log("readitem")

// Wait for content to finish loading
offscreenWindow.webContents.on('did-stop-loading', async() => {

// Get page title
let title = offscreenWindow.getTitle()
console.log(title)

// Get screenshot (thumbnail)
nativeImage = await offscreenWindow.webContents.capturePage().then(image => {
fs.writeFileSync('test.png', image.toPNG(), (err) => {
if (err) throw err
})
console.log('It\'s saved!')
return image.toDataURL()

})
let obj = { title:title,
url: url,
image: nativeImage }
callback(obj)
// Clean up
offscreenWindow.close()
offscreenWindow = null

})
}

main.js
const {app, BrowserWindow, ipcMain } = require('electron')
const path = require("path")
const fs = require("fs")
const screenshot = require("./screen")
let window

function createWindow () {
window = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
window.loadFile("./index.html")
window.webContents.openDevTools()
}

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})

ipcMain.on("start::screenshot", (event,arg) => {
console.log("Starting")
screenshot(arg, (reply) => {console.log("It's completed", reply)})
})

renderer.js
const { ipcRenderer } = require('electron')

function start() {
console.log("start")
let args = "https://google.com"
ipcRenderer.send('start::screenshot',args)
}

关于javascript - Electron JS中的capturePage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61241868/

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