You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
exposeFunction: globalThis[(prefix + name)] is not a function #69
The method corresponding to globalThis[prefix + name] should be this:
async #onBindingCalled(event: Protocol.Runtime.BindingCalledEvent,): Promise<void>{if(event.executionContextId!==this.#id){return;}letpayload: BindingPayload;try{payload=JSON.parse(event.payload);}catch{// The binding was either called by something in the page or it was// called before our wrapper was initialized.return;}const{ type, name, seq, args, isTrivial }=payload;if(type!=='internal'){this.emit('bindingcalled',event);return;}if(!this.#bindings.has(name)){this.emit('bindingcalled',event);return;}try{constbinding=this.#bindings.get(name);awaitbinding?.run(this,seq,args,isTrivial);}catch(err){debugError(err);}}
Added an addExposedFunctionBindingWithEvent method to handle this, available for now.
#bindings =newSet<string>();
@throwIfDetachedasyncaddExposedFunctionBindingWithEvent(binding: Binding): Promise<void>{consteventName=CDP_BINDING_PREFIX+binding.name;// Add a flag to control the loopconstflagName=`${eventName}_running`;awaitthis.#client.send('Page.addScriptToEvaluateOnNewDocument',{source: ` let ${eventName}_callbacks = []; window['${flagName}'] = true; window['${eventName}'] = (...args) => { for(const callback of ${eventName}_callbacks) { callback(...args); }${eventName}_callbacks = []; }; window['get_${eventName}'] = async function() { return new Promise((resolve, reject) => {${eventName}_callbacks.push(resolve); }); }; `,runImmediately: true,});this.#bindings.add(binding.name)// Start the polling loop with the flagvoid(async()=>{while(true){try{constmainWorld=this.worlds[MAIN_WORLD];if(!mainWorld.context||mainWorld.disposed||!this.#bindings.has(binding.name)){break;}const{ exceptionDetails,result: remoteObject}=awaitthis.#client.send('Runtime.evaluate',{expression: `window['get_${eventName}']()`,awaitPromise: true});if(exceptionDetails){thrownewError(exceptionDetails.text);}if(!remoteObject.value)continue;mainWorld.emitter.emit('bindingcalled',{name: binding.name,payload: remoteObject.value,executionContextId: mainWorld.context.id});}catch(e){debugError(e)// Add a small delay to prevent tight-loop CPU usage on errorsawaitnewPromise(resolve=>setTimeout(resolve,10));}}})();}
@throwIfDetachedasyncaddExposedFunctionBinding(binding: Binding): Promise<void>{// If a frame has not started loading, it might never start. Rely on// addScriptToEvaluateOnNewDocument in that case.if(this!==this._frameManager.mainFrame()&&!this._hasStartedLoading){return;}if(process.env['REBROWSER_PATCHES_RUNTIME_FIX_MODE']!=='0'){this.addExposedFunctionBindingWithEvent(binding)}awaitPromise.all([this.#client.send('Runtime.addBinding',{name: CDP_BINDING_PREFIX+binding.name,}),this.evaluate(binding.initSource).catch(debugError),]);}
@throwIfDetachedasyncremoveExposedFunctionBinding(binding: Binding): Promise<void>{
...
if(process.env['REBROWSER_PATCHES_RUNTIME_FIX_MODE']!=='0'&&this.#bindings.has(binding.name)){this.#bindings.delete(binding.name)}...}
I'm really confused with this issue. I cannot reproduce it. Are you sure that it's relevant to the patches?
page.exposeFunction is called after page.evaluateOnNewDocument? This way DOMContentLoaded probably will be fired before page.exposeFunction executed leading to window.reportSnapshot is undefined error.
I'm really confused with this issue. I cannot reproduce it. Are you sure that it's relevant to the patches?
page.exposeFunction is called after page.evaluateOnNewDocument? This way DOMContentLoaded probably will be fired before page.exposeFunction executed leading to window.reportSnapshot is undefined error.
Could you please share the full code example?
I tried disabling the patches, and it works fine. However, when the patches are enabled, the issue occurs.
The problem is not that window.reportSnapshot is undefined, because puppeteer defines reportSnapshot on the window object through Object.assign(globalThis, {[name](...args)}), so it is accessible.
The issue lies in the fact that the method inside Object.assign(globalThis, {[name](...args)}), specifically globalThis[prefix + name], is not defined.
By debugging the puppeteer source code, the issue seems to be in the addExposedFunctionBinding method:
Please vote +1 to get it fixed sooner (though this may not help).
I don't think it's feasible to try to fix it with CDP - all solutions I can think of are quite hacky and won't guarantee that bindings will be enabled before Page.addScriptToEvaluateOnNewDocument.
Your solution with polling totally makes sense, thanks for sharing this. That could be used until the bug is fixed, but I won't add this to this repo's codebase as I consider this bug out of scope of this project.
P.S. Actually, it might be possible to "fix" it by using Debugger.enable and Debugger.setBreakpointByUrl with lineNumber: 0, but not quite sure. Just a tip for somebody who really needs to work on this issue.
Just curious if anyone else has encountered the same issue related to the missing binding. It is quite consistent with my project as most of our client sites don't allow any page refresh but reload. The rebrowser patch can help you get through the cloudflare challenge but it will cause a hang-up in such cases.
had same issue, want to expose a function to log sth to the node process, but it's not worked, and page.on('console') is also not work for the patch
Just curious if anyone else has encountered the same issue related to the missing binding. It is quite consistent with my project as most of our client sites don't allow any page refresh but reload. The rebrowser patch can help you get through the cloudflare challenge but it will cause a hang-up in such cases.
#132 makes page.exposeFunction() work in the default addBinding mode, for Puppeteer and Playwright. That includes calls made while the page is still loading (like the DOMContentLoaded example here) and calls after reloads. Chrome still drops CDP bindings on navigation when Runtime.enable isn't used (crbug.com/382473087 is still open), so the patch adds the binding again when each navigation commits, and the page-side function waits for it. alwaysIsolated mode still can't support it, because it never touches the page's main world. Ships in the next release.
Released in v1.1.1: rebrowser-puppeteer-core / rebrowser-puppeteer25.14.100, rebrowser-playwright-core / rebrowser-playwright1.64.100, and rebrowser-playwright1.64.100 on PyPI. (npm doesn't allow re-publishing 25.14.0 / 1.64.1, so this fix release uses .100.) page.exposeFunction() works in the default mode now, including calls during page load and after reloads. alwaysIsolated mode still can't support it. Closing.
Activity
big-camel commentedon Nov 29, 2024
The method corresponding to
globalThis[prefix + name]should be this:big-camel commentedon Nov 29, 2024
Added an
addExposedFunctionBindingWithEventmethod to handle this, available for now.nwebson commentedon Dec 4, 2024
I'm really confused with this issue. I cannot reproduce it. Are you sure that it's relevant to the patches?
page.exposeFunctionis called afterpage.evaluateOnNewDocument? This wayDOMContentLoadedprobably will be fired beforepage.exposeFunctionexecuted leading towindow.reportSnapshot is undefinederror.Could you please share the full code example?
big-camel commentedon Dec 5, 2024
I tried disabling the patches, and it works fine. However, when the patches are enabled, the issue occurs.
The problem is not that
window.reportSnapshot is undefined, becausepuppeteerdefinesreportSnapshoton thewindowobject throughObject.assign(globalThis, {[name](...args)}), so it is accessible.The issue lies in the fact that the method inside
Object.assign(globalThis, {[name](...args)}), specificallyglobalThis[prefix + name], is not defined.By debugging the puppeteer source code, the issue seems to be in the
addExposedFunctionBindingmethod:Therefore, the full method name for
globalThis[prefix + name]should bepuppeteer_reportSnapshot, butwindow.puppeteer_reportSnapshotis undefined.Below is my simplified reproduction code:
nwebson commentedon Dec 5, 2024
Thanks for such a detailed report. The issue is that without having
Runtime.enable, all added bindings will be cleared after the page is refreshed.I consider this behavior a Chrome bug, as the CDP documentation clearly states:
I just filed a bug report with Chromium: https://issues.chromium.org/issues/382473087
Please vote +1 to get it fixed sooner (though this may not help).
I don't think it's feasible to try to fix it with CDP - all solutions I can think of are quite hacky and won't guarantee that bindings will be enabled before
Page.addScriptToEvaluateOnNewDocument.Your solution with polling totally makes sense, thanks for sharing this. That could be used until the bug is fixed, but I won't add this to this repo's codebase as I consider this bug out of scope of this project.
P.S. Actually, it might be possible to "fix" it by using
Debugger.enableandDebugger.setBreakpointByUrlwithlineNumber: 0, but not quite sure. Just a tip for somebody who really needs to work on this issue.sgtghost commentedon Jan 22, 2025
Just curious if anyone else has encountered the same issue related to the missing binding. It is quite consistent with my project as most of our client sites don't allow any page refresh but reload. The rebrowser patch can help you get through the cloudflare challenge but it will cause a hang-up in such cases.
fred8617 commentedon Jan 30, 2025
had same issue, want to expose a function to log sth to the node process, but it's not worked, and page.on('console') is also not work for the patch
fred8617 commentedon Feb 1, 2025
using a very tricky way to solve the issue
use
page.evaluateOnNewDocumentto mock theexposeFunctionthen use the event listener
rebrowsernet commentedon Oct 18, 2026
#132 makes
page.exposeFunction()work in the defaultaddBindingmode, for Puppeteer and Playwright. That includes calls made while the page is still loading (like theDOMContentLoadedexample here) and calls after reloads. Chrome still drops CDP bindings on navigation whenRuntime.enableisn't used (crbug.com/382473087 is still open), so the patch adds the binding again when each navigation commits, and the page-side function waits for it.alwaysIsolatedmode still can't support it, because it never touches the page's main world. Ships in the next release.rebrowsernet commentedon Oct 18, 2026
Released in v1.1.1:
rebrowser-puppeteer-core/rebrowser-puppeteer25.14.100,rebrowser-playwright-core/rebrowser-playwright1.64.100, andrebrowser-playwright1.64.100 on PyPI. (npm doesn't allow re-publishing 25.14.0 / 1.64.1, so this fix release uses.100.)page.exposeFunction()works in the default mode now, including calls during page load and after reloads.alwaysIsolatedmode still can't support it. Closing.