puppeteer/assets/js/89c78f53.25a2e8b1.js

1 line
8.1 KiB
JavaScript

"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[78530],{3905:(e,t,n)=>{n.d(t,{Zo:()=>s,kt:()=>d});var a=n(67294);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function p(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},i=Object.keys(e);for(a=0;a<i.length;a++)n=i[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(a=0;a<i.length;a++)n=i[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var l=a.createContext({}),u=function(e){var t=a.useContext(l),n=t;return e&&(n="function"==typeof e?e(t):o(o({},t),e)),n},s=function(e){var t=u(e.components);return a.createElement(l.Provider,{value:t},e.children)},c={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},m=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,i=e.originalType,l=e.parentName,s=p(e,["components","mdxType","originalType","parentName"]),m=u(n),d=r,g=m["".concat(l,".").concat(d)]||m[d]||c[d]||i;return n?a.createElement(g,o(o({ref:t},s),{},{components:n})):a.createElement(g,o({ref:t},s))}));function d(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var i=n.length,o=new Array(i);o[0]=m;var p={};for(var l in t)hasOwnProperty.call(t,l)&&(p[l]=t[l]);p.originalType=e,p.mdxType="string"==typeof e?e:r,o[1]=p;for(var u=2;u<i;u++)o[u]=n[u];return a.createElement.apply(null,o)}return a.createElement.apply(null,n)}m.displayName="MDXCreateElement"},91978:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>l,contentTitle:()=>o,default:()=>c,frontMatter:()=>i,metadata:()=>p,toc:()=>u});var a=n(87462),r=(n(67294),n(3905));const i={},o="Page.waitForFunction() method",p={unversionedId:"api/puppeteer.page.waitforfunction",id:"api/puppeteer.page.waitforfunction",title:"Page.waitForFunction() method",description:"The waitForFunction can be used to observe viewport size change:",source:"@site/../docs/api/puppeteer.page.waitforfunction.md",sourceDirName:"api",slug:"/api/puppeteer.page.waitforfunction",permalink:"/api/puppeteer.page.waitforfunction",draft:!1,tags:[],version:"current",frontMatter:{},sidebar:"sidebar",previous:{title:"Page.waitForFrame() method",permalink:"/api/puppeteer.page.waitforframe"},next:{title:"Page.waitForNavigation() method",permalink:"/api/puppeteer.page.waitfornavigation"}},l={},u=[{value:"Parameters",id:"parameters",level:2}],s={toc:u};function c(e){let{components:t,...n}=e;return(0,r.kt)("wrapper",(0,a.Z)({},s,n,{components:t,mdxType:"MDXLayout"}),(0,r.kt)("h1",{id:"pagewaitforfunction-method"},"Page.waitForFunction() method"),(0,r.kt)("p",null,"The ",(0,r.kt)("inlineCode",{parentName:"p"},"waitForFunction")," can be used to observe viewport size change:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre"},"const puppeteer = require('puppeteer');\n(async () => {\nconst browser = await puppeteer.launch();\nconst page = await browser.newPage();\nconst watchDog = page.waitForFunction('window.innerWidth < 100');\nawait page.setViewport({ width: 50, height: 50 });\nawait watchDog;\nawait browser.close();\n})();\n")),(0,r.kt)("p",null,"To pass arguments from node.js to the predicate of ",(0,r.kt)("inlineCode",{parentName:"p"},"page.waitForFunction")," function:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre"},"const selector = '.foo';\nawait page.waitForFunction(\n(selector) => !!document.querySelector(selector),\n{},\nselector\n);\n")),(0,r.kt)("p",null,"The predicate of ",(0,r.kt)("inlineCode",{parentName:"p"},"page.waitForFunction")," can be asynchronous too:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre"},"const username = 'github-username';\nawait page.waitForFunction(\nasync (username) => {\nconst githubResponse = await fetch(\n `https://api.github.com/users/${username}`\n);\nconst githubUser = await githubResponse.json();\n// show the avatar\nconst img = document.createElement('img');\nimg.src = githubUser.avatar_url;\n// wait 3 seconds\nawait new Promise((resolve, reject) => setTimeout(resolve, 3000));\nimg.remove();\n},\n{},\nusername\n);\n")),(0,r.kt)("p",null,(0,r.kt)("strong",{parentName:"p"},"Signature:")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-typescript"},"waitForFunction<Params extends unknown[], Func extends EvaluateFunc<Params> = EvaluateFunc<Params>>(pageFunction: Func | string, options?: {\n timeout?: number;\n polling?: string | number;\n }, ...args: Params): Promise<HandleFor<Awaited<ReturnType<Func>>>>;\n")),(0,r.kt)("h2",{id:"parameters"},"Parameters"),(0,r.kt)("table",null,(0,r.kt)("thead",{parentName:"table"},(0,r.kt)("tr",{parentName:"thead"},(0,r.kt)("th",{parentName:"tr",align:null},"Parameter"),(0,r.kt)("th",{parentName:"tr",align:null},"Type"),(0,r.kt)("th",{parentName:"tr",align:null},"Description"))),(0,r.kt)("tbody",{parentName:"table"},(0,r.kt)("tr",{parentName:"tbody"},(0,r.kt)("td",{parentName:"tr",align:null},"pageFunction"),(0,r.kt)("td",{parentName:"tr",align:null},"Func ","|"," string"),(0,r.kt)("td",{parentName:"tr",align:null},"Function to be evaluated in browser context")),(0,r.kt)("tr",{parentName:"tbody"},(0,r.kt)("td",{parentName:"tr",align:null},"options"),(0,r.kt)("td",{parentName:"tr",align:null},"{ timeout?: number; polling?: string ","|"," number; }"),(0,r.kt)("td",{parentName:"tr",align:null},(0,r.kt)("i",null,"(Optional)")," Optional waiting parameters")),(0,r.kt)("tr",{parentName:"tbody"},(0,r.kt)("td",{parentName:"tr",align:null},"args"),(0,r.kt)("td",{parentName:"tr",align:null},"Params"),(0,r.kt)("td",{parentName:"tr",align:null},"Arguments to pass to ",(0,r.kt)("code",null,"pageFunction"))))),(0,r.kt)("p",null,(0,r.kt)("strong",{parentName:"p"},"Returns:")),(0,r.kt)("p",null,"Promise","<",(0,r.kt)("a",{parentName:"p",href:"/api/puppeteer.handlefor"},"HandleFor"),"<","Awaited","<","ReturnType","<","Func",">",">",">",">"),(0,r.kt)("p",null,"Promise which resolves when the ",(0,r.kt)("inlineCode",{parentName:"p"},"pageFunction")," returns a truthy value. It resolves to a JSHandle of the truthy value."),(0,r.kt)("p",null,"The optional waiting parameter can be:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("p",{parentName:"li"},(0,r.kt)("inlineCode",{parentName:"p"},"Polling"),": An interval at which the ",(0,r.kt)("inlineCode",{parentName:"p"},"pageFunction")," is executed, defaults to ",(0,r.kt)("inlineCode",{parentName:"p"},"raf"),". If ",(0,r.kt)("inlineCode",{parentName:"p"},"polling")," is a number, then it is treated as an interval in milliseconds at which the function would be executed. If polling is a string, then it can be one of the following values:",(0,r.kt)("br",null)," - ",(0,r.kt)("inlineCode",{parentName:"p"},"raf"),": to constantly execute ",(0,r.kt)("inlineCode",{parentName:"p"},"pageFunction")," in ",(0,r.kt)("inlineCode",{parentName:"p"},"requestAnimationFrame")," callback. This is the tightest polling mode which is suitable to observe styling changes.",(0,r.kt)("br",null)," - ",(0,r.kt)("inlineCode",{parentName:"p"},"mutation"),": to execute pageFunction on every DOM mutation.")),(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("p",{parentName:"li"},(0,r.kt)("inlineCode",{parentName:"p"},"timeout"),": maximum time to wait for in milliseconds. Defaults to ",(0,r.kt)("inlineCode",{parentName:"p"},"30000")," (30 seconds). Pass ",(0,r.kt)("inlineCode",{parentName:"p"},"0")," to disable timeout. The default value can be changed by using the ",(0,r.kt)("a",{parentName:"p",href:"/api/puppeteer.page.setdefaulttimeout"},"page.setDefaultTimeout(timeout)")," method."))))}c.isMDXComponent=!0}}]);