Skip to content

Commit 5ae7f44

Browse files
committed
feature: improve Findbar binding
1 parent 05fada0 commit 5ae7f44

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/main.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { createRequire } from 'module'
33
import fs from 'fs'
44
import path from 'path'
55

6+
const FINDBAR_PROP = '__findbar__';
7+
68
interface Matches {
79
active: number
810
total: number
@@ -16,7 +18,7 @@ interface LastState {
1618
}
1719

1820
interface FindableWebContents extends WebContents {
19-
_findbar?: Findbar
21+
[FINDBAR_PROP]?: Findbar
2022
}
2123

2224
interface FindInPageResult {
@@ -71,7 +73,7 @@ class Findbar {
7173

7274
if (!this.findableContents) { throw new Error('There are no searchable web contents.'); }
7375

74-
this.findableContents._findbar = this;
76+
Findbar.setRef(this.findableContents, this);
7577
this.findableContents.prependOnceListener('destroyed', () => { this.detach(); });
7678
this.findableContents.prependListener('found-in-page', (_e, result: FindInPageResult) => {
7779
this.sendMatchesCount(result.activeMatchOrdinal, result.matches);
@@ -91,12 +93,12 @@ class Findbar {
9193
const options = Findbar.mergeStandardOptions(this.customOptions, this.parent);
9294
this.isMovableFlag = options.movable ?? false;
9395
this.window = new BrowserWindow(options) as FindableBrowserWindow;
94-
this.window.webContents._findbar = this;
9596

9697
this.registerListeners();
9798

9899
this.windowHandler?.(this.window);
99100
this.window.loadFile(Findbar.assetPaths.html);
101+
Findbar.setRef(this.window.webContents, this);
100102
}
101103

102104
/**
@@ -113,9 +115,9 @@ class Findbar {
113115
*/
114116
detach(): void {
115117
this.close();
116-
this.findableContents._findbar = void 0;
118+
Findbar.setRef(this.findableContents, undefined);
117119
if (this.window) {
118-
this.window.webContents._findbar = void 0;
120+
Findbar.setRef(this.window.webContents, undefined);
119121
}
120122
}
121123

@@ -445,13 +447,17 @@ class Findbar {
445447
};
446448
}
447449

450+
private static setRef(obj: Object, instance: Findbar | undefined): void {
451+
Object.defineProperty(obj, FINDBAR_PROP, { value: instance, configurable: true, writable: false });
452+
}
453+
448454
/**
449455
* Get the findbar instance for a given BrowserWindow or WebContents.
450456
*/
451457
static from(windowOrWebContents: BaseWindow | BrowserWindow | WebContents, customWebContents?: WebContents): Findbar {
452458
const webContents = isFindable(windowOrWebContents) ? windowOrWebContents : customWebContents ?? Findbar.retrieveWebContents(windowOrWebContents)
453459
if (!webContents) { throw new Error('[Findbar] There are no searchable web contents.'); }
454-
return (webContents as FindableWebContents)._findbar || new Findbar(windowOrWebContents, customWebContents);
460+
return (webContents as FindableWebContents).__findbar__ || new Findbar(windowOrWebContents, customWebContents);
455461
}
456462

457463
/**
@@ -460,7 +466,7 @@ class Findbar {
460466
static fromIfExists(windowOrWebContents: BaseWindow | BrowserWindow | WebContents): Findbar | undefined {
461467
const webContents = isFindable(windowOrWebContents) ? windowOrWebContents : Findbar.retrieveWebContents(windowOrWebContents);
462468
if (!webContents) { throw new Error('[Findbar] There are no searchable web contents.'); }
463-
return (webContents as FindableWebContents)._findbar;
469+
return (webContents as FindableWebContents).__findbar__;
464470
}
465471
}
466472

0 commit comments

Comments
 (0)