Skip to content

Commit 0f2487e

Browse files
committed
Merge branch 'patch-1' of https://github.com/dancesWithBugs/nut.js into dancesWithBugs-patch-1
2 parents dcaed48 + d8fb90e commit 0f2487e

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/screen.class.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ export class Screen {
8484
params?: LocationParameters,
8585
): Promise<Region> {
8686
const minMatch = (params && params.confidence) || this.config.confidence;
87-
const searchRegion =
88-
(params && params.searchRegion) || await this.vision.screenSize();
87+
const screenSize = await this.vision.screenSize();
88+
const searchRegion = (params && params.searchRegion) || screenSize;
8989
const searchMultipleScales = (params && params.searchMultipleScales)
9090

9191
const fullPathToNeedle = normalize(join(this.config.resourceDirectory, templateImageFilename));
@@ -102,6 +102,18 @@ export class Screen {
102102

103103
return new Promise<Region>(async (resolve, reject) => {
104104
try {
105+
if ( searchRegion.left < 0 || searchRegion.top < 0 || searchRegion.width < 0 || searchRegion.height < 0 ) {
106+
throw new Error(`Negative values in search region ${searchRegion}`)
107+
}
108+
if ( isNaN(searchRegion.left) || isNaN(searchRegion.top) || isNaN(searchRegion.width) || isNaN(searchRegion.height) ) {
109+
throw new Error(`NaN values in search region ${searchRegion}`)
110+
}
111+
if ( searchRegion.width < 2 || searchRegion.height < 2 ) {
112+
throw new Error(`Search region ${searchRegion} is not large enough. Must be at least two pixels in both width and height.`)
113+
}
114+
if ( searchRegion.left + searchRegion.width > screenSize.width || searchRegion.top + searchRegion.height > screenSize.height ) {
115+
throw new Error(`Search region ${searchRegion} extends beyond screen boundaries (${screenSize.width}x${screenSize.height})`)
116+
}
105117
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
106118
if (matchResult.confidence >= minMatch) {
107119
const possibleHooks = this.findHooks.get(templateImageFilename) || [];

0 commit comments

Comments
 (0)