@@ -297,6 +297,7 @@ await context.close();
297
297
- [ browserContext.clearPermissions()] ( #browsercontextclearpermissions )
298
298
- [ browserContext.close()] ( #browsercontextclose )
299
299
- [ browserContext.cookies([ urls] )] ( #browsercontextcookiesurls )
300
+ - [ browserContext.exposeBinding(name, playwrightBinding)] ( #browsercontextexposebindingname-playwrightbinding )
300
301
- [ browserContext.exposeFunction(name, playwrightFunction)] ( #browsercontextexposefunctionname-playwrightfunction )
301
302
- [ browserContext.grantPermissions(permissions[ ] [ , options ] )] ( #browsercontextgrantpermissionspermissions-options )
302
303
- [ browserContext.newPage()] ( #browsercontextnewpage )
@@ -421,20 +422,54 @@ will be closed.
421
422
If no URLs are specified, this method returns all cookies.
422
423
If URLs are specified, only cookies that affect those URLs are returned.
423
424
425
+ #### browserContext.exposeBinding(name, playwrightBinding)
426
+ - ` name ` <[ string] > Name of the function on the window object.
427
+ - ` playwrightBinding ` <[ function] > Callback function that will be called in the Playwright's context.
428
+ - returns: <[ Promise] >
429
+
430
+ The method adds a function called ` name ` on the ` window ` object of every frame in every page in the context.
431
+ When called, the function executes ` playwrightBinding ` in Node.js and returns a [ Promise] which resolves to the return value of ` playwrightBinding ` .
432
+ If the ` playwrightBinding ` returns a [ Promise] , it will be awaited.
433
+
434
+ The first argument of the ` playwrightBinding ` function contains information about the caller:
435
+ ` { browserContext: BrowserContext, page: Page, frame: Frame } ` .
436
+
437
+ See [ page.exposeBinding(name, playwrightBinding)] ( #pageexposebindingname-playwrightbinding ) for page-only version.
438
+
439
+ An example of exposing page URL to all frames in all pages in the context:
440
+ ``` js
441
+ const { webkit } = require (' playwright' ); // Or 'chromium' or 'firefox'.
442
+
443
+ (async () => {
444
+ const browser = await webkit .launch ({ headless: false });
445
+ const context = await browser .newContext ();
446
+ await context .exposeBinding (' pageURL' , ({ page }) => page .url ());
447
+ const page = await context .newPage ();
448
+ await page .setContent (`
449
+ <script>
450
+ async function onClick() {
451
+ document.querySelector('div').textContent = await window.pageURL();
452
+ }
453
+ </script>
454
+ <button onclick="onClick()">Click me</button>
455
+ <div></div>
456
+ ` );
457
+ await page .click (' button' );
458
+ })();
459
+ ```
460
+
424
461
#### browserContext.exposeFunction(name, playwrightFunction)
425
462
- ` name ` <[ string] > Name of the function on the window object.
426
463
- ` playwrightFunction ` <[ function] > Callback function that will be called in the Playwright's context.
427
464
- returns: <[ Promise] >
428
465
429
466
The method adds a function called ` name ` on the ` window ` object of every frame in every page in the context.
430
- When called, the function executes ` playwrightFunction ` in node .js and returns a [ Promise] which resolves to the return value of ` playwrightFunction ` .
467
+ When called, the function executes ` playwrightFunction ` in Node .js and returns a [ Promise] which resolves to the return value of ` playwrightFunction ` .
431
468
432
469
If the ` playwrightFunction ` returns a [ Promise] , it will be awaited.
433
470
434
471
See [ page.exposeFunction(name, playwrightFunction)] ( #pageexposefunctionname-playwrightfunction ) for page-only version.
435
472
436
- > ** NOTE** Functions installed via ` page.exposeFunction ` survive navigations.
437
-
438
473
An example of adding an ` md5 ` function to all pages in the context:
439
474
``` js
440
475
const { webkit } = require (' playwright' ); // Or 'chromium' or 'firefox'.
@@ -678,6 +713,7 @@ page.removeListener('request', logRequest);
678
713
- [ page.emulateMedia(options)] ( #pageemulatemediaoptions )
679
714
- [ page.evaluate(pageFunction[ , arg] )] ( #pageevaluatepagefunction-arg )
680
715
- [ page.evaluateHandle(pageFunction[ , arg] )] ( #pageevaluatehandlepagefunction-arg )
716
+ - [ page.exposeBinding(name, playwrightBinding)] ( #pageexposebindingname-playwrightbinding )
681
717
- [ page.exposeFunction(name, playwrightFunction)] ( #pageexposefunctionname-playwrightfunction )
682
718
- [ page.fill(selector, value[ , options] )] ( #pagefillselector-value-options )
683
719
- [ page.focus(selector[ , options] )] ( #pagefocusselector-options )
@@ -1165,13 +1201,51 @@ console.log(await resultHandle.jsonValue());
1165
1201
await resultHandle .dispose ();
1166
1202
```
1167
1203
1204
+ #### page.exposeBinding(name, playwrightBinding)
1205
+ - ` name ` <[ string] > Name of the function on the window object.
1206
+ - ` playwrightBinding ` <[ function] > Callback function that will be called in the Playwright's context.
1207
+ - returns: <[ Promise] >
1208
+
1209
+ The method adds a function called ` name ` on the ` window ` object of every frame in this page.
1210
+ When called, the function executes ` playwrightBinding ` in Node.js and returns a [ Promise] which resolves to the return value of ` playwrightBinding ` .
1211
+ If the ` playwrightBinding ` returns a [ Promise] , it will be awaited.
1212
+
1213
+ The first argument of the ` playwrightBinding ` function contains information about the caller:
1214
+ ` { browserContext: BrowserContext, page: Page, frame: Frame } ` .
1215
+
1216
+ See [ browserContext.exposeBinding(name, playwrightBinding)] ( #browsercontextexposebindingname-playwrightbinding ) for the context-wide version.
1217
+
1218
+ > ** NOTE** Functions installed via ` page.exposeBinding ` survive navigations.
1219
+
1220
+ An example of exposing page URL to all frames in a page:
1221
+ ``` js
1222
+ const { webkit } = require (' playwright' ); // Or 'chromium' or 'firefox'.
1223
+
1224
+ (async () => {
1225
+ const browser = await webkit .launch ({ headless: false });
1226
+ const context = await browser .newContext ();
1227
+ const page = await context .newPage ();
1228
+ await page .exposeBinding (' pageURL' , ({ page }) => page .url ());
1229
+ await page .setContent (`
1230
+ <script>
1231
+ async function onClick() {
1232
+ document.querySelector('div').textContent = await window.pageURL();
1233
+ }
1234
+ </script>
1235
+ <button onclick="onClick()">Click me</button>
1236
+ <div></div>
1237
+ ` );
1238
+ await page .click (' button' );
1239
+ })();
1240
+ ```
1241
+
1168
1242
#### page.exposeFunction(name, playwrightFunction)
1169
1243
- ` name ` <[ string] > Name of the function on the window object
1170
1244
- ` playwrightFunction ` <[ function] > Callback function which will be called in Playwright's context.
1171
1245
- returns: <[ Promise] >
1172
1246
1173
1247
The method adds a function called ` name ` on the ` window ` object of every frame in the page.
1174
- When called, the function executes ` playwrightFunction ` in node .js and returns a [ Promise] which resolves to the return value of ` playwrightFunction ` .
1248
+ When called, the function executes ` playwrightFunction ` in Node .js and returns a [ Promise] which resolves to the return value of ` playwrightFunction ` .
1175
1249
1176
1250
If the ` playwrightFunction ` returns a [ Promise] , it will be awaited.
1177
1251
@@ -1720,7 +1794,7 @@ const { webkit } = require('playwright'); // Or 'chromium' or 'firefox'.
1720
1794
})();
1721
1795
```
1722
1796
1723
- To pass an argument from node .js to the predicate of ` page.waitForFunction ` function:
1797
+ To pass an argument from Node .js to the predicate of ` page.waitForFunction ` function:
1724
1798
1725
1799
``` js
1726
1800
const selector = ' .foo' ;
@@ -2389,7 +2463,7 @@ const { firefox } = require('playwright'); // Or 'chromium' or 'webkit'.
2389
2463
})();
2390
2464
```
2391
2465
2392
- To pass an argument from node .js to the predicate of ` frame.waitForFunction ` function:
2466
+ To pass an argument from Node .js to the predicate of ` frame.waitForFunction ` function:
2393
2467
2394
2468
``` js
2395
2469
const selector = ' .foo' ;
@@ -4027,6 +4101,7 @@ const backgroundPage = await context.waitForEvent('backgroundpage');
4027
4101
- [ browserContext.clearPermissions()] ( #browsercontextclearpermissions )
4028
4102
- [ browserContext.close()] ( #browsercontextclose )
4029
4103
- [ browserContext.cookies([ urls] )] ( #browsercontextcookiesurls )
4104
+ - [ browserContext.exposeBinding(name, playwrightBinding)] ( #browsercontextexposebindingname-playwrightbinding )
4030
4105
- [ browserContext.exposeFunction(name, playwrightFunction)] ( #browsercontextexposefunctionname-playwrightfunction )
4031
4106
- [ browserContext.grantPermissions(permissions[ ] [ , options ] )] ( #browsercontextgrantpermissionspermissions-options )
4032
4107
- [ browserContext.newPage()] ( #browsercontextnewpage )
0 commit comments