|
53 | 53 | clientEmitter.on('Runtime.consoleAPICalled', this.#onConsoleAPI.bind(this)); |
54 | 54 | clientEmitter.on(CDPSessionEvent.Disconnected, () => { |
55 | 55 | this[disposeSymbol](); |
56 | | -@@ -351,6 +358,74 @@ |
| 56 | +@@ -351,6 +358,119 @@ |
57 | 57 | return await this.#evaluate(false, pageFunction, ...args); |
58 | 58 | } |
59 | 59 |
|
|
65 | 65 | + this.#puppeteerUtil = undefined |
66 | 66 | + } |
67 | 67 | + // rebrowser-patches: get context id if it's missing |
68 | | -+ async acquireContextId() { |
| 68 | ++ async acquireContextId(tryCount = 1): Promise<any> { |
69 | 69 | + if (this.#id > 0) { |
70 | 70 | + return |
71 | 71 | + } |
72 | 72 | + |
73 | | -+ const fixMode = process.env['REBROWSER_PATCHES_RUNTIME_FIX_MODE'] || 'alwaysIsolated' |
74 | | -+ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] id = ${this.#id}, name = ${this.#name}, fixMode = ${fixMode}`) |
| 73 | ++ const fixMode = process.env['REBROWSER_PATCHES_RUNTIME_FIX_MODE'] || 'addBinding' |
| 74 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] id = ${this.#id}, name = ${this.#name}, fixMode = ${fixMode}, tryCount = ${tryCount}`) |
75 | 75 | + |
76 | 76 | + let contextId: any |
77 | | -+ if (fixMode === 'alwaysIsolated') { |
| 77 | ++ if (fixMode === 'addBinding') { |
| 78 | ++ if (this.#id === -2) { |
| 79 | ++ // isolated world |
| 80 | ++ const sendRes = await this.#client |
| 81 | ++ .send('Page.createIsolatedWorld', { |
| 82 | ++ frameId: this._frameId, |
| 83 | ++ worldName: this.#name, |
| 84 | ++ grantUniveralAccess: true, |
| 85 | ++ }); |
| 86 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] Page.createIsolatedWorld result:`, sendRes); |
| 87 | ++ contextId = sendRes.executionContextId; |
| 88 | ++ } else { |
| 89 | ++ // main world |
| 90 | ++ // add the binding |
| 91 | ++ const bindingName = Math.random().toString() |
| 92 | ++ await this.#client.send('Runtime.addBinding', { |
| 93 | ++ name: bindingName, |
| 94 | ++ }) |
| 95 | ++ |
| 96 | ++ // listen for 'Runtime.bindingCalled' event |
| 97 | ++ const bindingCalledHandler = ({ name, executionContextId }: any) => { |
| 98 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log('[rebrowser-patches][bindingCalledHandler]', { name, executionContextId }) |
| 99 | ++ if (contextId > 0) { |
| 100 | ++ // already acquired the id |
| 101 | ++ return; |
| 102 | ++ } |
| 103 | ++ if (name !== bindingName) { |
| 104 | ++ // ignore irrelevant bindings |
| 105 | ++ return |
| 106 | ++ } |
| 107 | ++ contextId = executionContextId |
| 108 | ++ // remove this listener |
| 109 | ++ this.#client.off('Runtime.bindingCalled', bindingCalledHandler); |
| 110 | ++ } |
| 111 | ++ this.#client.on('Runtime.bindingCalled', bindingCalledHandler); |
| 112 | ++ |
| 113 | ++ // call the binding |
| 114 | ++ await this.#client.send('Runtime.evaluate', { |
| 115 | ++ expression: `self['${bindingName}']('1')` |
| 116 | ++ }) |
| 117 | ++ } |
| 118 | ++ } else if (fixMode === 'alwaysIsolated') { |
78 | 119 | + if (this.#id === -3) { |
79 | 120 | + throw new Error('[rebrowser-patches] web workers are not supported in alwaysIsolated mode') |
80 | 121 | + } |
|
119 | 160 | + } |
120 | 161 | + |
121 | 162 | + if (!contextId) { |
122 | | -+ throw new Error('[rebrowser-patches] acquireContextId failed') |
| 163 | ++ if (tryCount >= 3) { |
| 164 | ++ throw new Error('[rebrowser-patches] acquireContextId failed') |
| 165 | ++ } |
| 166 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] failed, try again (tryCount = ${tryCount})`) |
| 167 | ++ return this.acquireContextId(tryCount + 1) |
123 | 168 | + } |
124 | 169 | + |
125 | 170 | + this.#id = contextId |
|
128 | 173 | async #evaluate< |
129 | 174 | Params extends unknown[], |
130 | 175 | Func extends EvaluateFunc<Params> = EvaluateFunc<Params>, |
131 | | -@@ -375,6 +450,13 @@ |
| 176 | +@@ -375,6 +495,13 @@ |
132 | 177 | pageFunction: Func | string, |
133 | 178 | ...args: Params |
134 | 179 | ): Promise<HandleFor<Awaited<ReturnType<Func>>> | Awaited<ReturnType<Func>>> { |
|
373 | 418 | */ |
374 | 419 | evaluateHandle<Params extends unknown[], Func extends EvaluateFunc<Params> = EvaluateFunc<Params>>(pageFunction: Func | string, ...args: Params): Promise<HandleFor<Awaited<ReturnType<Func>>>>; |
375 | 420 | + clear(newId: any): void; |
376 | | -+ acquireContextId(): Promise<void>; |
| 421 | ++ acquireContextId(tryCount?: number): Promise<any>; |
377 | 422 | [disposeSymbol](): void; |
378 | 423 | } |
379 | 424 | //# sourceMappingURL=ExecutionContext.d.ts.map |
|
419 | 464 | clientEmitter.on('Runtime.consoleAPICalled', this.#onConsoleAPI.bind(this)); |
420 | 465 | clientEmitter.on(CDPSession_js_1.CDPSessionEvent.Disconnected, () => { |
421 | 466 | this[disposable_js_1.disposeSymbol](); |
422 | | -@@ -324,7 +331,75 @@ |
| 467 | +@@ -324,7 +331,120 @@ |
423 | 468 | async evaluateHandle(pageFunction, ...args) { |
424 | 469 | return await this.#evaluate(false, pageFunction, ...args); |
425 | 470 | } |
|
431 | 476 | + this.#puppeteerUtil = undefined; |
432 | 477 | + } |
433 | 478 | + // rebrowser-patches: get context id if it's missing |
434 | | -+ async acquireContextId() { |
| 479 | ++ async acquireContextId(tryCount = 1) { |
435 | 480 | + if (this.#id > 0) { |
436 | 481 | + return; |
437 | 482 | + } |
438 | | -+ const fixMode = process.env['REBROWSER_PATCHES_RUNTIME_FIX_MODE'] || 'alwaysIsolated'; |
439 | | -+ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] id = ${this.#id}, name = ${this.#name}, fixMode = ${fixMode}`); |
| 483 | ++ const fixMode = process.env['REBROWSER_PATCHES_RUNTIME_FIX_MODE'] || 'addBinding'; |
| 484 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] id = ${this.#id}, name = ${this.#name}, fixMode = ${fixMode}, tryCount = ${tryCount}`); |
440 | 485 | + let contextId; |
441 | | -+ if (fixMode === 'alwaysIsolated') { |
| 486 | ++ if (fixMode === 'addBinding') { |
| 487 | ++ if (this.#id === -2) { |
| 488 | ++ // isolated world |
| 489 | ++ const sendRes = await this.#client |
| 490 | ++ .send('Page.createIsolatedWorld', { |
| 491 | ++ frameId: this._frameId, |
| 492 | ++ worldName: this.#name, |
| 493 | ++ grantUniveralAccess: true, |
| 494 | ++ }); |
| 495 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] Page.createIsolatedWorld result:`, sendRes); |
| 496 | ++ contextId = sendRes.executionContextId; |
| 497 | ++ } |
| 498 | ++ else { |
| 499 | ++ // main world |
| 500 | ++ // add the binding |
| 501 | ++ const bindingName = Math.random().toString(); |
| 502 | ++ await this.#client.send('Runtime.addBinding', { |
| 503 | ++ name: bindingName, |
| 504 | ++ }); |
| 505 | ++ // listen for 'Runtime.bindingCalled' event |
| 506 | ++ const bindingCalledHandler = ({ name, executionContextId }) => { |
| 507 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log('[rebrowser-patches][bindingCalledHandler]', { name, executionContextId }); |
| 508 | ++ if (contextId > 0) { |
| 509 | ++ // already acquired the id |
| 510 | ++ return; |
| 511 | ++ } |
| 512 | ++ if (name !== bindingName) { |
| 513 | ++ // ignore irrelevant bindings |
| 514 | ++ return; |
| 515 | ++ } |
| 516 | ++ contextId = executionContextId; |
| 517 | ++ // remove this listener |
| 518 | ++ this.#client.off('Runtime.bindingCalled', bindingCalledHandler); |
| 519 | ++ }; |
| 520 | ++ this.#client.on('Runtime.bindingCalled', bindingCalledHandler); |
| 521 | ++ // call the binding |
| 522 | ++ await this.#client.send('Runtime.evaluate', { |
| 523 | ++ expression: `self['${bindingName}']('1')` |
| 524 | ++ }); |
| 525 | ++ } |
| 526 | ++ } |
| 527 | ++ else if (fixMode === 'alwaysIsolated') { |
442 | 528 | + if (this.#id === -3) { |
443 | 529 | + throw new Error('[rebrowser-patches] web workers are not supported in alwaysIsolated mode'); |
444 | 530 | + } |
|
481 | 567 | + this.#client.off('Runtime.executionContextCreated', executionContextCreatedHandler); |
482 | 568 | + } |
483 | 569 | + if (!contextId) { |
484 | | -+ throw new Error('[rebrowser-patches] acquireContextId failed'); |
| 570 | ++ if (tryCount >= 3) { |
| 571 | ++ throw new Error('[rebrowser-patches] acquireContextId failed'); |
| 572 | ++ } |
| 573 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] failed, try again (tryCount = ${tryCount})`); |
| 574 | ++ return this.acquireContextId(tryCount + 1); |
485 | 575 | + } |
486 | 576 | + this.#id = contextId; |
487 | 577 | + } |
|
721 | 811 | */ |
722 | 812 | evaluateHandle<Params extends unknown[], Func extends EvaluateFunc<Params> = EvaluateFunc<Params>>(pageFunction: Func | string, ...args: Params): Promise<HandleFor<Awaited<ReturnType<Func>>>>; |
723 | 813 | + clear(newId: any): void; |
724 | | -+ acquireContextId(): Promise<void>; |
| 814 | ++ acquireContextId(tryCount?: number): Promise<any>; |
725 | 815 | [disposeSymbol](): void; |
726 | 816 | } |
727 | 817 | //# sourceMappingURL=ExecutionContext.d.ts.map |
|
767 | 857 | clientEmitter.on('Runtime.consoleAPICalled', this.#onConsoleAPI.bind(this)); |
768 | 858 | clientEmitter.on(CDPSessionEvent.Disconnected, () => { |
769 | 859 | this[disposeSymbol](); |
770 | | -@@ -321,7 +328,75 @@ |
| 860 | +@@ -321,7 +328,120 @@ |
771 | 861 | async evaluateHandle(pageFunction, ...args) { |
772 | 862 | return await this.#evaluate(false, pageFunction, ...args); |
773 | 863 | } |
|
779 | 869 | + this.#puppeteerUtil = undefined; |
780 | 870 | + } |
781 | 871 | + // rebrowser-patches: get context id if it's missing |
782 | | -+ async acquireContextId() { |
| 872 | ++ async acquireContextId(tryCount = 1) { |
783 | 873 | + if (this.#id > 0) { |
784 | 874 | + return; |
785 | 875 | + } |
786 | | -+ const fixMode = process.env['REBROWSER_PATCHES_RUNTIME_FIX_MODE'] || 'alwaysIsolated'; |
787 | | -+ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] id = ${this.#id}, name = ${this.#name}, fixMode = ${fixMode}`); |
| 876 | ++ const fixMode = process.env['REBROWSER_PATCHES_RUNTIME_FIX_MODE'] || 'addBinding'; |
| 877 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] id = ${this.#id}, name = ${this.#name}, fixMode = ${fixMode}, tryCount = ${tryCount}`); |
788 | 878 | + let contextId; |
789 | | -+ if (fixMode === 'alwaysIsolated') { |
| 879 | ++ if (fixMode === 'addBinding') { |
| 880 | ++ if (this.#id === -2) { |
| 881 | ++ // isolated world |
| 882 | ++ const sendRes = await this.#client |
| 883 | ++ .send('Page.createIsolatedWorld', { |
| 884 | ++ frameId: this._frameId, |
| 885 | ++ worldName: this.#name, |
| 886 | ++ grantUniveralAccess: true, |
| 887 | ++ }); |
| 888 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] Page.createIsolatedWorld result:`, sendRes); |
| 889 | ++ contextId = sendRes.executionContextId; |
| 890 | ++ } |
| 891 | ++ else { |
| 892 | ++ // main world |
| 893 | ++ // add the binding |
| 894 | ++ const bindingName = Math.random().toString(); |
| 895 | ++ await this.#client.send('Runtime.addBinding', { |
| 896 | ++ name: bindingName, |
| 897 | ++ }); |
| 898 | ++ // listen for 'Runtime.bindingCalled' event |
| 899 | ++ const bindingCalledHandler = ({ name, executionContextId }) => { |
| 900 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log('[rebrowser-patches][bindingCalledHandler]', { name, executionContextId }); |
| 901 | ++ if (contextId > 0) { |
| 902 | ++ // already acquired the id |
| 903 | ++ return; |
| 904 | ++ } |
| 905 | ++ if (name !== bindingName) { |
| 906 | ++ // ignore irrelevant bindings |
| 907 | ++ return; |
| 908 | ++ } |
| 909 | ++ contextId = executionContextId; |
| 910 | ++ // remove this listener |
| 911 | ++ this.#client.off('Runtime.bindingCalled', bindingCalledHandler); |
| 912 | ++ }; |
| 913 | ++ this.#client.on('Runtime.bindingCalled', bindingCalledHandler); |
| 914 | ++ // call the binding |
| 915 | ++ await this.#client.send('Runtime.evaluate', { |
| 916 | ++ expression: `self['${bindingName}']('1')` |
| 917 | ++ }); |
| 918 | ++ } |
| 919 | ++ } |
| 920 | ++ else if (fixMode === 'alwaysIsolated') { |
790 | 921 | + if (this.#id === -3) { |
791 | 922 | + throw new Error('[rebrowser-patches] web workers are not supported in alwaysIsolated mode'); |
792 | 923 | + } |
|
829 | 960 | + this.#client.off('Runtime.executionContextCreated', executionContextCreatedHandler); |
830 | 961 | + } |
831 | 962 | + if (!contextId) { |
832 | | -+ throw new Error('[rebrowser-patches] acquireContextId failed'); |
| 963 | ++ if (tryCount >= 3) { |
| 964 | ++ throw new Error('[rebrowser-patches] acquireContextId failed'); |
| 965 | ++ } |
| 966 | ++ process.env['REBROWSER_PATCHES_DEBUG'] && console.log(`[rebrowser-patches][acquireContextId] failed, try again (tryCount = ${tryCount})`); |
| 967 | ++ return this.acquireContextId(tryCount + 1); |
833 | 968 | + } |
834 | 969 | + this.#id = contextId; |
835 | 970 | + } |
|
0 commit comments