Skip to content

Commit

Permalink
fix: 临时修复 electron 21 崩溃问题
Browse files Browse the repository at this point in the history
  • Loading branch information
nashaofu committed Oct 25, 2022
1 parent 9e8c448 commit 2dee78a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 84 deletions.
1 change: 0 additions & 1 deletion packages/electron-screenshots/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"dependencies": {
"debug": "^4.3.3",
"fs-extra": "^10.1.0",
"node-screenshots": "^0.0.11",
"react-screenshots": "^0.5.10"
},
"peerDependencies": {
Expand Down
56 changes: 21 additions & 35 deletions packages/electron-screenshots/src/screenshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,46 +224,32 @@ export default class Screenshots extends Events {
private async capture (display: Display): Promise<string> {
this.logger('SCREENSHOTS:capture')

try {
const { Screenshots: NodeScreenshots } = await import('node-screenshots')
const capturer = NodeScreenshots.fromDisplay(display.id)
this.logger('SCREENSHOTS:capture NodeScreenshots.fromDisplay(%d) %o', display.id, capturer)
if (!capturer) {
throw new Error(`NodeScreenshots.fromDisplay(${display.id}) get null`)
const sources = await desktopCapturer.getSources({
types: ['screen'],
thumbnailSize: {
width: display.width,
height: display.height
}
})

const image = await capturer.capture()
return `data:image/png;base64,${image.toString('base64')}`
} catch (err) {
this.logger('SCREENSHOTS:capture NodeScreenshots capture() error %o', err)

const sources = await desktopCapturer.getSources({
types: ['screen'],
thumbnailSize: {
width: display.width,
height: display.height
}
let source
// Linux系统上,screen.getDisplayNearestPoint 返回的 Display 对象的 id
// 和这里 source 对象上的 display_id(Linux上,这个值是空字符串) 或 id 的中间部分,都不一致
// 但是,如果只有一个显示器的话,其实不用判断,直接返回就行
if (sources.length === 1) {
source = sources[0]
} else {
source = sources.find(source => {
return source.display_id === display.id.toString() || source.id.startsWith(`screen:${display.id}:`)
})
}

let source
// Linux系统上,screen.getDisplayNearestPoint 返回的 Display 对象的 id
// 和这里 source 对象上的 display_id(Linux上,这个值是空字符串) 或 id 的中间部分,都不一致
// 但是,如果只有一个显示器的话,其实不用判断,直接返回就行
if (sources.length === 1) {
source = sources[0]
} else {
source = sources.find(source => {
return source.display_id === display.id.toString() || source.id.startsWith(`screen:${display.id}:`)
})
}

if (!source) {
this.logger("SCREENSHOTS:capture Can't find screen source. sources: %o, display: %o", sources, display)
throw new Error("Can't find screen source")
}

return source.thumbnail.toDataURL()
if (!source) {
this.logger("SCREENSHOTS:capture Can't find screen source. sources: %o, display: %o", sources, display)
throw new Error("Can't find screen source")
}

return source.thumbnail.toDataURL()
}

/**
Expand Down
48 changes: 0 additions & 48 deletions packages/electron-screenshots/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1388,54 +1388,6 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==

node-screenshots-darwin-arm64@0.0.11:
version "0.0.11"
resolved "https://registry.npmmirror.com/node-screenshots-darwin-arm64/-/node-screenshots-darwin-arm64-0.0.11.tgz#33e9669c00b9d9863647132d11fae0afcc5cbdd3"
integrity sha512-koLc7YiSSmeseXM7sLzD4jssbtF9xVlW0AWfuvcgWFl1myIpWfwD++hd+5apMy0gjHtJtnJCdHg07yMh4dQ9CA==

node-screenshots-darwin-x64@0.0.11:
version "0.0.11"
resolved "https://registry.npmmirror.com/node-screenshots-darwin-x64/-/node-screenshots-darwin-x64-0.0.11.tgz#36e95daa1bbeff8959f58647d4e5b66d6fb0ece7"
integrity sha512-SeEf4LfpAy8NSG95w2sUeF+8UZNmregEYO9UWuPB0qJvwSQqdYghmfgFPwcUXYmzlz04LIeRblHQMBa7RWL33g==

node-screenshots-linux-x64-gnu@0.0.11:
version "0.0.11"
resolved "https://registry.npmmirror.com/node-screenshots-linux-x64-gnu/-/node-screenshots-linux-x64-gnu-0.0.11.tgz#8d6b49c499fa93e23c8006badcbdd0c9eeddef64"
integrity sha512-rihMiZQis78XMw8T5DShW122c0Ah/HR28V/7YN30jnVLOzY0EC3Ek2U3yVY4NxzierO/zdyVl1LJpYF/gxzclw==

node-screenshots-linux-x64-musl@0.0.11:
version "0.0.11"
resolved "https://registry.npmmirror.com/node-screenshots-linux-x64-musl/-/node-screenshots-linux-x64-musl-0.0.11.tgz#0d1fcdf596ed2dc6e1cdfd059fd9536bb74e7011"
integrity sha512-9+1PdH4hTqu7TUkcoDxdS/b5zUlq49q0P7dhb5QFI0SmxicAKTPlSS0QaAEkx/NRD4wM8T6kn8bobqcTv2DkmQ==

node-screenshots-win32-arm64-msvc@0.0.11:
version "0.0.11"
resolved "https://registry.npmmirror.com/node-screenshots-win32-arm64-msvc/-/node-screenshots-win32-arm64-msvc-0.0.11.tgz#4f6c528b1ccbef50209ccc19cf89d340b89cc491"
integrity sha512-w4dzuUohQZvMdUwMEDeug9E0aunz/XuyeRhLByZNDtbE8vc0R6mQCEHZdwy55NfLqdfnc0F3wKa38dRU7V2UBw==

node-screenshots-win32-ia32-msvc@0.0.11:
version "0.0.11"
resolved "https://registry.npmmirror.com/node-screenshots-win32-ia32-msvc/-/node-screenshots-win32-ia32-msvc-0.0.11.tgz#2bd94c03300d2b34865d275c4ab48f71b0a910a7"
integrity sha512-vEHBxG/a38nzvpqJjlVqK8In/QhBUYfBjmUsrvNCOOX6+y8zlELEsrc1nAxyIHtgr3HO6LjCms/yktFlxQ6N9g==

node-screenshots-win32-x64-msvc@0.0.11:
version "0.0.11"
resolved "https://registry.npmmirror.com/node-screenshots-win32-x64-msvc/-/node-screenshots-win32-x64-msvc-0.0.11.tgz#da2435a7b515f8adbccfc0323095394b74fa4102"
integrity sha512-qX9o0xda5+5M6lZJHGvseJnJjdI4g/I9dMMB1A1yOxrGiMcEiu5f2qMiHOQGOx9B3wiFcubIuTCIqq8EuitHOw==

node-screenshots@^0.0.11:
version "0.0.11"
resolved "https://registry.npmmirror.com/node-screenshots/-/node-screenshots-0.0.11.tgz#ba638ed763b0512a129b1bcecf81f217a586e94a"
integrity sha512-u8wAvfc5YWZ6sh1QPkWUjciaf8NXAtnoO1WMLgBXY2IGzdsJexdHSEFkEUZNbwfzeBM3oes6/BLtT6Qf3kZT+Q==
optionalDependencies:
node-screenshots-darwin-arm64 "0.0.11"
node-screenshots-darwin-x64 "0.0.11"
node-screenshots-linux-x64-gnu "0.0.11"
node-screenshots-linux-x64-musl "0.0.11"
node-screenshots-win32-arm64-msvc "0.0.11"
node-screenshots-win32-ia32-msvc "0.0.11"
node-screenshots-win32-x64-msvc "0.0.11"

normalize-url@^4.1.0:
version "4.5.1"
resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-4.5.1.tgz#0dd90cf1288ee1d1313b87081c9a5932ee48518a"
Expand Down

0 comments on commit 2dee78a

Please sign in to comment.