"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[14951],{3905:(e,t,n)=>{n.d(t,{Zo:()=>u,kt:()=>d});var r=n(67294);function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function p(e){for(var t=1;t=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var l=r.createContext({}),c=function(e){var t=r.useContext(l),n=t;return e&&(n="function"==typeof e?e(t):p(p({},t),e)),n},u=function(e){var t=c(e.components);return r.createElement(l.Provider,{value:t},e.children)},s={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},m=r.forwardRef((function(e,t){var n=e.components,a=e.mdxType,o=e.originalType,l=e.parentName,u=i(e,["components","mdxType","originalType","parentName"]),m=c(n),d=a,f=m["".concat(l,".").concat(d)]||m[d]||s[d]||o;return n?r.createElement(f,p(p({ref:t},u),{},{components:n})):r.createElement(f,p({ref:t},u))}));function d(e,t){var n=arguments,a=t&&t.mdxType;if("string"==typeof e||a){var o=n.length,p=new Array(o);p[0]=m;var i={};for(var l in t)hasOwnProperty.call(t,l)&&(i[l]=t[l]);i.originalType=e,i.mdxType="string"==typeof e?e:a,p[1]=i;for(var c=2;c{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>i,default:()=>m,frontMatter:()=>p,metadata:()=>l,toc:()=>u});n(67294);var r=n(3905);function a(){return a=Object.assign||function(e){for(var t=1;t=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}const p={sidebar_label:"Page.exposeFunction"},i="Page.exposeFunction() method",l={unversionedId:"api/puppeteer.page.exposefunction",id:"version-19.8.4/api/puppeteer.page.exposefunction",title:"Page.exposeFunction() method",description:"The method adds a function called name on the page's window object. When called, the function executes puppeteerFunction in node.js and returns a Promise which resolves to the return value of puppeteerFunction.",source:"@site/versioned_docs/version-19.8.4/api/puppeteer.page.exposefunction.md",sourceDirName:"api",slug:"/api/puppeteer.page.exposefunction",permalink:"/api/puppeteer.page.exposefunction",draft:!1,tags:[],version:"19.8.4",frontMatter:{sidebar_label:"Page.exposeFunction"},sidebar:"api",previous:{title:"Page.evaluateOnNewDocument",permalink:"/api/puppeteer.page.evaluateonnewdocument"},next:{title:"Page.focus",permalink:"/api/puppeteer.page.focus"}},c={},u=[{value:"Signature:",id:"signature",level:4},{value:"Parameters",id:"parameters",level:2},{value:"Example 1",id:"example-1",level:2},{value:"Example 2",id:"example-2",level:2}],s={toc:u};function m(e){var{components:t}=e,n=o(e,["components"]);return(0,r.kt)("wrapper",a({},s,n,{components:t,mdxType:"MDXLayout"}),(0,r.kt)("h1",a({},{id:"pageexposefunction-method"}),"Page.exposeFunction() method"),(0,r.kt)("p",null,"The method adds a function called ",(0,r.kt)("inlineCode",{parentName:"p"},"name")," on the page's ",(0,r.kt)("inlineCode",{parentName:"p"},"window")," object. When called, the function executes ",(0,r.kt)("inlineCode",{parentName:"p"},"puppeteerFunction")," in node.js and returns a ",(0,r.kt)("inlineCode",{parentName:"p"},"Promise")," which resolves to the return value of ",(0,r.kt)("inlineCode",{parentName:"p"},"puppeteerFunction"),"."),(0,r.kt)("p",null,"If the puppeteerFunction returns a ",(0,r.kt)("inlineCode",{parentName:"p"},"Promise"),", it will be awaited."),(0,r.kt)("admonition",a({},{type:"note"}),(0,r.kt)("p",{parentName:"admonition"},"Functions installed via ",(0,r.kt)("inlineCode",{parentName:"p"},"page.exposeFunction")," survive navigations.")),(0,r.kt)("h4",a({},{id:"signature"}),"Signature:"),(0,r.kt)("pre",null,(0,r.kt)("code",a({parentName:"pre"},{className:"language-typescript"}),"class Page {\n exposeFunction(\n name: string,\n pptrFunction:\n | Function\n | {\n default: Function;\n }\n ): Promise;\n}\n")),(0,r.kt)("h2",a({},{id:"parameters"}),"Parameters"),(0,r.kt)("table",null,(0,r.kt)("thead",{parentName:"table"},(0,r.kt)("tr",{parentName:"thead"},(0,r.kt)("th",a({parentName:"tr"},{align:null}),"Parameter"),(0,r.kt)("th",a({parentName:"tr"},{align:null}),"Type"),(0,r.kt)("th",a({parentName:"tr"},{align:null}),"Description"))),(0,r.kt)("tbody",{parentName:"table"},(0,r.kt)("tr",{parentName:"tbody"},(0,r.kt)("td",a({parentName:"tr"},{align:null}),"name"),(0,r.kt)("td",a({parentName:"tr"},{align:null}),"string"),(0,r.kt)("td",a({parentName:"tr"},{align:null}),"Name of the function on the window object")),(0,r.kt)("tr",{parentName:"tbody"},(0,r.kt)("td",a({parentName:"tr"},{align:null}),"pptrFunction"),(0,r.kt)("td",a({parentName:"tr"},{align:null}),"Function ","|"," { default: Function; }"),(0,r.kt)("td",a({parentName:"tr"},{align:null}),"Callback function which will be called in Puppeteer's context.")))),(0,r.kt)("p",null,(0,r.kt)("strong",{parentName:"p"},"Returns:")),(0,r.kt)("p",null,"Promise","<","void",">"),(0,r.kt)("h2",a({},{id:"example-1"}),"Example 1"),(0,r.kt)("p",null,"An example of adding an ",(0,r.kt)("inlineCode",{parentName:"p"},"md5")," function into the page:"),(0,r.kt)("pre",null,(0,r.kt)("code",a({parentName:"pre"},{className:"language-ts"}),"import puppeteer from 'puppeteer';\nimport crypto from 'crypto';\n\n(async () => {\n const browser = await puppeteer.launch();\n const page = await browser.newPage();\n page.on('console', msg => console.log(msg.text()));\n await page.exposeFunction('md5', text =>\n crypto.createHash('md5').update(text).digest('hex')\n );\n await page.evaluate(async () => {\n // use window.md5 to compute hashes\n const myString = 'PUPPETEER';\n const myHash = await window.md5(myString);\n console.log(`md5 of ${myString} is ${myHash}`);\n });\n await browser.close();\n})();\n")),(0,r.kt)("h2",a({},{id:"example-2"}),"Example 2"),(0,r.kt)("p",null,"An example of adding a ",(0,r.kt)("inlineCode",{parentName:"p"},"window.readfile")," function into the page:"),(0,r.kt)("pre",null,(0,r.kt)("code",a({parentName:"pre"},{className:"language-ts"}),"import puppeteer from 'puppeteer';\nimport fs from 'fs';\n\n(async () => {\n const browser = await puppeteer.launch();\n const page = await browser.newPage();\n page.on('console', msg => console.log(msg.text()));\n await page.exposeFunction('readfile', async filePath => {\n return new Promise((resolve, reject) => {\n fs.readFile(filePath, 'utf8', (err, text) => {\n if (err) reject(err);\n else resolve(text);\n });\n });\n });\n await page.evaluate(async () => {\n // use window.readfile to read contents of a file\n const content = await window.readfile('/etc/hosts');\n console.log(content);\n });\n await browser.close();\n})();\n")))}m.isMDXComponent=!0}}]);