puppeteer/assets/js/a0d4c24a.878288cc.js

2 lines
6.5 KiB
JavaScript
Raw Normal View History

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