|
|
|
"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[75035],{57944:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>a,default:()=>d,frontMatter:()=>s,metadata:()=>i,toc:()=>p});var o=t(85893),r=t(11151);const s={sidebar_label:"Page.exposeFunction"},a="Page.exposeFunction() method",i={id:"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-21.6.1/api/puppeteer.page.exposefunction.md",sourceDirName:"api",slug:"/api/puppeteer.page.exposefunction",permalink:"/api/puppeteer.page.exposefunction",draft:!1,unlisted:!1,tags:[],version:"21.6.1",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={},p=[{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}];function l(e){const n={admonition:"admonition",code:"code",h1:"h1",h2:"h2",h4:"h4",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",...(0,r.a)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)(n.h1,{id:"pageexposefunction-method",children:"Page.exposeFunction() method"}),"\n",(0,o.jsxs)(n.p,{children:["The method adds a function called ",(0,o.jsx)(n.code,{children:"name"})," on the page's ",(0,o.jsx)(n.code,{children:"window"})," object. When called, the function executes ",(0,o.jsx)(n.code,{children:"puppeteerFunction"})," in node.js and returns a ",(0,o.jsx)(n.code,{children:"Promise"})," which resolves to the return value of ",(0,o.jsx)(n.code,{children:"puppeteerFunction"}),"."]}),"\n",(0,o.jsxs)(n.p,{children:["If the puppeteerFunction returns a ",(0,o.jsx)(n.code,{children:"Promise"}),", it will be awaited."]}),"\n",(0,o.jsxs)(n.admonition,{type:"note",children:[(0,o.jsxs)(n.p,{children:["Functions installed via ",(0,o.jsx)(n.code,{children:"page.exposeFunction"})," survive navigations."]}),(0,o.jsxs)(n.admonition,{type:"note",children:[(0,o.jsx)(n.h4,{id:"signature",children:"Signature:"}),(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-typescript",children:"class Page {\n abstract exposeFunction(\n name: string,\n pptrFunction:\n | Function\n | {\n default: Function;\n }\n ): Promise<void>;\n}\n"})}),(0,o.jsx)(n.h2,{id:"parameters",children:"Parameters"}),(0,o.jsxs)(n.table,{children:[(0,o.jsx)(n.thead,{children:(0,o.jsxs)(n.tr,{children:[(0,o.jsx)(n.th,{children:"Parameter"}),(0,o.jsx)(n.th,{children:"Type"}),(0,o.jsx)(n.th,{children:"Description"})]})}),(0,o.jsxs)(n.tbody,{children:[(0,o.jsxs)(n.tr,{children:[(0,o.jsx)(n.td,{children:"name"}),(0,o.jsx)(n.td,{children:"string"}),(0,o.jsx)(n.td,{children:"Name of the function on the window object"})]}),(0,o.jsxs)(n.tr,{children:[(0,o.jsx)(n.td,{children:"pptrFunction"}),(0,o.jsx)(n.td,{children:"Function | { default: Function; }"}),(0,o.jsx)(n.td,{children:"Callback function which will be called in Puppeteer's context."})]})]})]}),(0,o.jsx)(n.p,{children:(0,o.jsx)(n.strong,{children:"Returns:"})}),(0,o.jsx)(n.p,{children:"Promise<void>"}),(0,o.jsx)(n.h2,{id:"example-1",children:"Example 1"}),(0,o.jsxs)(n.p,{children:["An example of adding an ",(0,o.jsx)(n.code,{children:"md5"})," function into the page:"]}),(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-ts",children:"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 );
|