puppeteer/assets/js/7451acb9.60e54c71.js
2021-10-07 15:21:45 +00:00

1 line
7.4 KiB
JavaScript

"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[65147],{3905:function(e,t,n){n.d(t,{Zo:function(){return c},kt:function(){return m}});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 p(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 o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?p(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):p(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(e,t){if(null==e)return{};var n,r,a=function(e,t){if(null==e)return{};var n,r,a={},p=Object.keys(e);for(r=0;r<p.length;r++)n=p[r],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var p=Object.getOwnPropertySymbols(e);for(r=0;r<p.length;r++)n=p[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var u=r.createContext({}),l=function(e){var t=r.useContext(u),n=t;return e&&(n="function"==typeof e?e(t):o(o({},t),e)),n},c=function(e){var t=l(e.components);return r.createElement(u.Provider,{value:t},e.children)},s={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},d=r.forwardRef((function(e,t){var n=e.components,a=e.mdxType,p=e.originalType,u=e.parentName,c=i(e,["components","mdxType","originalType","parentName"]),d=l(n),m=a,f=d["".concat(u,".").concat(m)]||d[m]||s[m]||p;return n?r.createElement(f,o(o({ref:t},c),{},{components:n})):r.createElement(f,o({ref:t},c))}));function m(e,t){var n=arguments,a=t&&t.mdxType;if("string"==typeof e||a){var p=n.length,o=new Array(p);o[0]=d;var i={};for(var u in t)hasOwnProperty.call(t,u)&&(i[u]=t[u]);i.originalType=e,i.mdxType="string"==typeof e?e:a,o[1]=i;for(var l=2;l<p;l++)o[l]=n[l];return r.createElement.apply(null,o)}return r.createElement.apply(null,n)}d.displayName="MDXCreateElement"},44790:function(e,t,n){n.r(t),n.d(t,{frontMatter:function(){return i},contentTitle:function(){return u},metadata:function(){return l},toc:function(){return c},default:function(){return d}});var r=n(87462),a=n(63366),p=(n(67294),n(3905)),o=["components"],i={},u=void 0,l={unversionedId:"puppeteer.page.exposefunction",id:"version-10.1.0/puppeteer.page.exposefunction",isDocsHomePage:!1,title:"puppeteer.page.exposefunction",description:"Home &gt; puppeteer &gt; Page &gt; exposeFunction",source:"@site/versioned_docs/version-10.1.0/puppeteer.page.exposefunction.md",sourceDirName:".",slug:"/puppeteer.page.exposefunction",permalink:"/puppeteer/docs/puppeteer.page.exposefunction",editUrl:"https://github.com/facebook/puppeteer/edit/main/website/versioned_docs/version-10.1.0/puppeteer.page.exposefunction.md",version:"10.1.0",frontMatter:{},sidebar:"version-10.1.0/docs",previous:{title:"puppeteer.page.evaluateonnewdocument",permalink:"/puppeteer/docs/puppeteer.page.evaluateonnewdocument"},next:{title:"puppeteer.page.focus",permalink:"/puppeteer/docs/puppeteer.page.focus"}},c=[{value:"Page.exposeFunction() method",id:"pageexposefunction-method",children:[]},{value:"Parameters",id:"parameters",children:[]},{value:"Example",id:"example",children:[]}],s={toc:c};function d(e){var t=e.components,n=(0,a.Z)(e,o);return(0,p.kt)("wrapper",(0,r.Z)({},s,n,{components:t,mdxType:"MDXLayout"}),(0,p.kt)("p",null,(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/index"},"Home")," ",">"," ",(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/puppeteer"},"puppeteer")," ",">"," ",(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/puppeteer.page"},"Page")," ",">"," ",(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/puppeteer.page.exposefunction"},"exposeFunction")),(0,p.kt)("h2",{id:"pageexposefunction-method"},"Page.exposeFunction() method"),(0,p.kt)("p",null,"The method adds a function called ",(0,p.kt)("inlineCode",{parentName:"p"},"name")," on the page's ",(0,p.kt)("inlineCode",{parentName:"p"},"window")," object. When called, the function executes ",(0,p.kt)("inlineCode",{parentName:"p"},"puppeteerFunction")," in node.js and returns a ",(0,p.kt)("inlineCode",{parentName:"p"},"Promise")," which resolves to the return value of ",(0,p.kt)("inlineCode",{parentName:"p"},"puppeteerFunction"),"."),(0,p.kt)("p",null,"If the puppeteerFunction returns a ",(0,p.kt)("inlineCode",{parentName:"p"},"Promise"),", it will be awaited."),(0,p.kt)("p",null,"NOTE: Functions installed via ",(0,p.kt)("inlineCode",{parentName:"p"},"page.exposeFunction")," survive navigations."),(0,p.kt)("b",null,"Signature:"),(0,p.kt)("pre",null,(0,p.kt)("code",{parentName:"pre",className:"language-typescript"},"exposeFunction(name: string, puppeteerFunction: Function): Promise<void>;\n")),(0,p.kt)("h2",{id:"parameters"},"Parameters"),(0,p.kt)("table",null,(0,p.kt)("thead",{parentName:"table"},(0,p.kt)("tr",{parentName:"thead"},(0,p.kt)("th",{parentName:"tr",align:null},"Parameter"),(0,p.kt)("th",{parentName:"tr",align:null},"Type"),(0,p.kt)("th",{parentName:"tr",align:null},"Description"))),(0,p.kt)("tbody",{parentName:"table"},(0,p.kt)("tr",{parentName:"tbody"},(0,p.kt)("td",{parentName:"tr",align:null},"name"),(0,p.kt)("td",{parentName:"tr",align:null},"string"),(0,p.kt)("td",{parentName:"tr",align:null},"Name of the function on the window object")),(0,p.kt)("tr",{parentName:"tbody"},(0,p.kt)("td",{parentName:"tr",align:null},"puppeteerFunction"),(0,p.kt)("td",{parentName:"tr",align:null},"Function"),(0,p.kt)("td",{parentName:"tr",align:null},"Callback function which will be called in Puppeteer's context.")))),(0,p.kt)("b",null,"Returns:"),(0,p.kt)("p",null,"Promise","<","void",">"),(0,p.kt)("h2",{id:"example"},"Example"),(0,p.kt)("p",null,"An example of adding an ",(0,p.kt)("inlineCode",{parentName:"p"},"md5")," function into the page:"),(0,p.kt)("pre",null,(0,p.kt)("code",{parentName:"pre",className:"language-js"},"const puppeteer = require('puppeteer');\nconst crypto = require('crypto');\n\n(async () => {\nconst browser = await puppeteer.launch();\nconst page = await browser.newPage();\npage.on('console', (msg) => console.log(msg.text()));\nawait page.exposeFunction('md5', (text) =>\ncrypto.createHash('md5').update(text).digest('hex')\n);\nawait page.evaluate(async () => {\n// use window.md5 to compute hashes\nconst myString = 'PUPPETEER';\nconst myHash = await window.md5(myString);\nconsole.log(`md5 of ${myString} is ${myHash}`);\n});\nawait browser.close();\n})();\n\n")),(0,p.kt)("p",null,"An example of adding a ",(0,p.kt)("inlineCode",{parentName:"p"},"window.readfile")," function into the page:"),(0,p.kt)("pre",null,(0,p.kt)("code",{parentName:"pre",className:"language-js"},"const puppeteer = require('puppeteer');\nconst fs = require('fs');\n\n(async () => {\nconst browser = await puppeteer.launch();\nconst page = await browser.newPage();\npage.on('console', (msg) => console.log(msg.text()));\nawait page.exposeFunction('readfile', async (filePath) => {\nreturn new Promise((resolve, reject) => {\nfs.readFile(filePath, 'utf8', (err, text) => {\n if (err) reject(err);\n else resolve(text);\n });\n});\n});\nawait page.evaluate(async () => {\n// use window.readfile to read contents of a file\nconst content = await window.readfile('/etc/hosts');\nconsole.log(content);\n});\nawait browser.close();\n})();\n\n")))}d.isMDXComponent=!0}}]);