Skip to content

扩展包

常用扩展包

下面介绍常用的electron 扩展包

壁纸设置

使用 wallpaper 扩展包操作,如果和 electron 结合使用,就不能使用高版本的 wallpaper,因为 electron 不支持 ESM,但高版本的 wallpaper 使用 ESM

pnpm add wallpaper@v5.0.1

下面是请求远程图片,并设置为壁纸的主进程代码

import { IpcMainEvent, ipcMain } from 'electron'
import { createWriteStream } from 'fs'
import fetch from 'node-fetch'
import { pipeline } from 'node:stream'
import { promisify } from 'node:util'
import { resolve } from 'path'
import wallpaper from 'wallpaper'

ipcMain.on('setWallpaper', async (event: IpcMainEvent, url: string) => {
  const localFile = resolve(__dirname, '../../wallpaper/' + url.split('/').pop())
  const streamPipeline = promisify(pipeline)
  const response = await fetch(url)
  if (!response.ok) throw new Error(`unexpected response ${response.statusText}`)
  await streamPipeline(response.body!, createWriteStream(localFile))

  wallpaper.set(localFile, { screen: 'all', scale: 'auto' })
})