Skip to content

Commit 3dd4945

Browse files
authored
fix(chromium): install binding function during initialization (#1320)
1 parent 65d10a5 commit 3dd4945

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/chromium/crPage.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,16 @@ export class CRPage implements PageDelegate {
9292
helper.addEventListener(this._client, 'Target.attachedToTarget', event => this._onAttachedToTarget(event)),
9393
helper.addEventListener(this._client, 'Target.detachedFromTarget', event => this._onDetachedFromTarget(event)),
9494
];
95-
this._page.frames().map(frame => this._client.send('Page.createIsolatedWorld', {
96-
frameId: frame._id,
97-
grantUniveralAccess: true,
98-
worldName: UTILITY_WORLD_NAME,
99-
}).catch(debugError)); // frames might be removed before we send this.
95+
for (const frame of this._page.frames()) {
96+
// Note: frames might be removed before we send these.
97+
this._client.send('Page.createIsolatedWorld', {
98+
frameId: frame._id,
99+
grantUniveralAccess: true,
100+
worldName: UTILITY_WORLD_NAME,
101+
}).catch(debugError);
102+
for (const binding of this._browserContext._pageBindings.values())
103+
frame.evaluate(binding.source).catch(debugError);
104+
}
100105
}),
101106
this._client.send('Log.enable', {}),
102107
this._client.send('Page.setLifecycleEventsEnabled', { enabled: true }),

src/page.ts

+3
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,8 @@ export class PageBinding {
612612

613613
function addPageBinding(bindingName: string) {
614614
const binding = (window as any)[bindingName];
615+
if (binding.__installed)
616+
return;
615617
(window as any)[bindingName] = (...args: any[]) => {
616618
const me = (window as any)[bindingName];
617619
let callbacks = me['callbacks'];
@@ -625,4 +627,5 @@ function addPageBinding(bindingName: string) {
625627
binding(JSON.stringify({name: bindingName, seq, args}));
626628
return promise;
627629
};
630+
(window as any)[bindingName].__installed = true;
628631
}

test/browsercontext.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, FF
326326
});
327327

328328
describe('BrowserContext.exposeFunction', () => {
329-
it.fail(CHROMIUM || FFOX)('should work', async({browser, server}) => {
329+
it.fail(FFOX)('should work', async({browser, server}) => {
330330
const context = await browser.newContext();
331331
await context.exposeFunction('add', (a, b) => a + b);
332332
const page = await context.newPage();

0 commit comments

Comments
 (0)