/function
This function endpoint allows you to POST
Puppeteer code.
We will run the Puppeteer code on our Headless Browsers and return the result to you.
Basic Example
This endpoint requires 2 parameters: code
and context
.
The code
function is a javascript function which gets called with an object containing page
and context
.
page
is the page object used by Puppeteer and is provided by us.
Code:
module.exports = async ({ page, context }) => {
const { url } = context;
await page.goto(url);
const data = await page.content();
return {
data,
type: 'application/html',
};
};
Context:
{
"url": "https://headlesstesting.com"
}
This simple example will run a Puppeteer script on our service and do the following:
- Start a Headless Chrome Browser (latest version) on our cloud.
-
We will run the
code
you specified on the Headless Browser and pass thecontext
to your function. - In this example, the Puppeteer script will instruct the Headless Browser to navigate to our website and retrieve the HTML source.
-
The return result will be returned to you (HTML source code) with a
application/html
content-type.
API Call:
curl -X POST \
"https://chrome.headlesstesting.com/function?token=[YOUR-TOKEN]" \
-H 'Content-Type: application/json' \
-d '{
"code": "module.exports=async({page:a,context:b})=>{const{url:c}=b;await a.goto(c);const d=await a.content();return{data:d,type:\"application/html\"}};",
"context": {
"url": "https://headlesstesting.com/"
}
}'