diff --git a/components/puppeteer/actions/common/common.mjs b/components/puppeteer/actions/common/common.mjs new file mode 100644 index 0000000000000..b42ee037a62ff --- /dev/null +++ b/components/puppeteer/actions/common/common.mjs @@ -0,0 +1,19 @@ +export default { + props: { + url: { + type: "string", + label: "URL", + description: + "The URL of the page to scrape. For example, `https://example.com`.", + }, + }, + methods: { + normalizeUrl() { + let url = this.url; + if (!url.startsWith("http")) { + url = `https://${url}`; + } + return url; + }, + }, +}; diff --git a/components/puppeteer/actions/get-html/get-html.mjs b/components/puppeteer/actions/get-html/get-html.mjs index ffcbc2a7ec38d..b4af0c99cc5de 100644 --- a/components/puppeteer/actions/get-html/get-html.mjs +++ b/components/puppeteer/actions/get-html/get-html.mjs @@ -1,28 +1,28 @@ import puppeteer from "../../puppeteer.app.mjs"; +import common from "../common/common.mjs"; export default { + ...common, key: "puppeteer-get-html", name: "Get HTML", - description: "Get the HTML of a webpage using Puppeteer. [See the documentation](https://pptr.dev/api/puppeteer.page.content)", - version: "1.0.1", + description: + "Get the HTML of a webpage using Puppeteer. [See the documentation](https://pptr.dev/api/puppeteer.page.content) for details.", + version: "1.0.2", type: "action", props: { puppeteer, - url: { - type: "string", - label: "URL", - description: "The URL of the page to scrape.", - }, + ...common.props, }, async run({ $ }) { + const url = this.normalizeUrl(); const browser = await this.puppeteer.launch(); const page = await browser.newPage(); - await page.goto(this.url); + await page.goto(url); const html = await page.content(); await browser.close(); if (html) { - $.export("$summary", "Successfully retrieved HTML from page."); + $.export("$summary", `Successfully retrieved HTML from ${url}`); } return html; diff --git a/components/puppeteer/actions/get-page-title/get-page-title.mjs b/components/puppeteer/actions/get-page-title/get-page-title.mjs index 386a76c5216ed..b85ab74337be7 100644 --- a/components/puppeteer/actions/get-page-title/get-page-title.mjs +++ b/components/puppeteer/actions/get-page-title/get-page-title.mjs @@ -1,28 +1,28 @@ import puppeteer from "../../puppeteer.app.mjs"; +import common from "../common/common.mjs"; export default { + ...common, key: "puppeteer-get-page-title", name: "Get Page Title", - description: "Get the title of a webpage using Puppeteer. [See the documentation](https://pptr.dev/api/puppeteer.page.title)", - version: "1.0.1", + description: + "Get the title of a webpage using Puppeteer. [See the documentation](https://pptr.dev/api/puppeteer.page.title)", + version: "1.0.2", type: "action", props: { puppeteer, - url: { - type: "string", - label: "URL", - description: "The URL of the webpage to get the title from.", - }, + ...common.props, }, async run({ $ }) { + const url = this.normalizeUrl(); const browser = await this.puppeteer.launch(); const page = await browser.newPage(); - await page.goto(this.url); + await page.goto(url); const title = await page.title(); await browser.close(); if (title) { - $.export("$summary", `Successfully retrieved title ${title}.`); + $.export("$summary", `Successfully retrieved the title from ${url}.`); } return title; diff --git a/components/puppeteer/actions/get-pdf/get-pdf.mjs b/components/puppeteer/actions/get-pdf/get-pdf.mjs index d7a6d3d3a52cb..683e1a281314a 100644 --- a/components/puppeteer/actions/get-pdf/get-pdf.mjs +++ b/components/puppeteer/actions/get-pdf/get-pdf.mjs @@ -1,24 +1,24 @@ import puppeteer from "../../puppeteer.app.mjs"; import constants from "../../common/constants.mjs"; +import common from "../common/common.mjs"; import fs from "fs"; export default { + ...common, key: "puppeteer-get-pdf", name: "Get PDF", - description: "Generate a PDF of a page using Puppeteer. [See the documentation](https://pptr.dev/api/puppeteer.page.pdf)", - version: "1.0.1", + description: + "Generate a PDF of a page using Puppeteer. [See the documentation](https://pptr.dev/api/puppeteer.page.pdf)", + version: "1.0.2", type: "action", props: { puppeteer, - url: { - type: "string", - label: "URL", - description: "The URL of the page to scrape.", - }, + ...common.props, downloadPath: { type: "string", label: "Download Path", - description: "Download the PDF to the `/tmp` directory with the specified filename", + description: + "Download the PDF to the `/tmp` directory with the specified filename", optional: true, }, displayHeaderFooter: { @@ -44,13 +44,15 @@ export default { headerTemplate: { type: "string", label: "Header Template", - description: "HTML template for the print header. Should be valid HTML with the following classes used to inject values into them: `date` - formatted print date, `title` - document title, `url` - document location, `pageNumber` - current page number, `totalPages` - total pages in the document.", + description: + "HTML template for the print header. Should be valid HTML with the following classes used to inject values into them: `date` - formatted print date, `title` - document title, `url` - document location, `pageNumber` - current page number, `totalPages` - total pages in the document.", optional: true, }, height: { type: "string", label: "Height", - description: "Sets the height of paper. You can pass in a number or a string with a unit.", + description: + "Sets the height of paper. You can pass in a number or a string with a unit.", optional: true, }, landscape: { @@ -87,7 +89,8 @@ export default { omitBackground: { type: "boolean", label: "Omit Background", - description: "Hides default white background and allows generating pdfs with transparency.", + description: + "Hides default white background and allows generating pdfs with transparency.", optional: true, default: false, }, @@ -100,7 +103,8 @@ export default { preferCSSPageSize: { type: "boolean", label: "Prefer CSS Page Size", - description: "Give any CSS @page size declared in the page priority over what is declared in the width or height or format option.", + description: + "Give any CSS @page size declared in the page priority over what is declared in the width or height or format option.", optional: true, default: false, }, @@ -114,7 +118,8 @@ export default { scale: { type: "string", label: "Scale", - description: "Scales the rendering of the web page. Amount must be between 0.1 and 2.", + description: + "Scales the rendering of the web page. Amount must be between 0.1 and 2.", optional: true, }, timeout: { @@ -127,7 +132,8 @@ export default { width: { type: "string", label: "Width", - description: "Sets the width of paper. You can pass in a number or a string with a unit.", + description: + "Sets the width of paper. You can pass in a number or a string with a unit.", optional: true, }, }, @@ -165,18 +171,20 @@ export default { width: this.width, }; + const url = this.normalizeUrl(); const browser = await this.puppeteer.launch(); const page = await browser.newPage(); - await page.goto(this.url); + await page.goto(url); const pdf = await page.pdf(options); await browser.close(); - const filePath = pdf && this.downloadPath - ? await this.downloadToTMP(pdf) - : undefined; + const filePath = + pdf && this.downloadPath + ? await this.downloadToTMP(pdf) + : undefined; if (pdf) { - $.export("$summary", "Successfully generated PDF from page."); + $.export("$summary", `Successfully generated PDF from ${url}`); } return filePath diff --git a/components/puppeteer/actions/screenshot-page/screenshot-page.mjs b/components/puppeteer/actions/screenshot-page/screenshot-page.mjs index 6af498373160a..8951065064bf2 100644 --- a/components/puppeteer/actions/screenshot-page/screenshot-page.mjs +++ b/components/puppeteer/actions/screenshot-page/screenshot-page.mjs @@ -1,25 +1,25 @@ import puppeteer from "../../puppeteer.app.mjs"; import constants from "../../common/constants.mjs"; +import common from "../common/common.mjs"; import fs from "fs"; import { ConfigurationError } from "@pipedream/platform"; export default { + ...common, key: "puppeteer-screenshot-page", name: "Screenshot a Page", - description: "Captures a screenshot of a page using Puppeteer. [See the documentation](https://pptr.dev/api/puppeteer.page.screenshot)", - version: "1.0.1", + description: + "Captures a screenshot of a page using Puppeteer. [See the documentation](https://pptr.dev/api/puppeteer.page.screenshot)", + version: "1.0.2", type: "action", props: { puppeteer, - url: { - type: "string", - label: "URL", - description: "The URL of the page to scrape.", - }, + ...common.props, downloadPath: { type: "string", label: "Download Path", - description: "Download the screenshot to the `/tmp` directory with the specified filename", + description: + "Download the screenshot to the `/tmp` directory with the specified filename", optional: true, }, captureBeyondViewport: { @@ -71,7 +71,8 @@ export default { fromSurface: { type: "boolean", label: "From Surface", - description: "Capture the screenshot from the surface, rather than the view.", + description: + "Capture the screenshot from the surface, rather than the view.", optional: true, default: false, }, @@ -85,7 +86,8 @@ export default { omitBackground: { type: "boolean", label: "Omit Background", - description: "Hides default white background and allows capturing screenshots with transparency.", + description: + "Hides default white background and allows capturing screenshots with transparency.", optional: true, default: false, }, @@ -99,7 +101,8 @@ export default { quality: { type: "integer", label: "Quality", - description: "Quality of the image, between 0-100. Not applicable to png images.", + description: + "Quality of the image, between 0-100. Not applicable to png images.", optional: true, }, type: { @@ -121,20 +124,25 @@ export default { }, }, async run({ $ }) { - if ((this.clipHeight || this.clipWidth || this.clipX || this.clipY) - && !(this.clipHeight && this.clipWidth && this.clipX && this.clipY)) { - throw new ConfigurationError("Clip height, width, X, and Y must be specified to create clip."); + if ( + (this.clipHeight || this.clipWidth || this.clipX || this.clipY) && + !(this.clipHeight && this.clipWidth && this.clipX && this.clipY) + ) { + throw new ConfigurationError( + "Clip height, width, X, and Y must be specified to create clip.", + ); } - const clip = this.clipHeight || this.clipWidth || this.clipX || this.clipY - ? { - height: parseFloat(this.clipHeight), - scale: parseFloat(this.clipScale), - width: parseFloat(this.clipWidth), - x: parseFloat(this.clipX), - y: parseFloat(this.clipY), - } - : undefined; + const clip = + this.clipHeight || this.clipWidth || this.clipX || this.clipY + ? { + height: parseFloat(this.clipHeight), + scale: parseFloat(this.clipScale), + width: parseFloat(this.clipWidth), + x: parseFloat(this.clipX), + y: parseFloat(this.clipY), + } + : undefined; const options = { captureBeyondViewport: this.captureBeyondViewport, @@ -148,18 +156,20 @@ export default { type: this.type, }; + const url = this.normalizeUrl(); const browser = await this.puppeteer.launch(); const page = await browser.newPage(); - await page.goto(this.url); + await page.goto(url); const screenshot = await page.screenshot(options); await browser.close(); - const filePath = screenshot && this.downloadPath - ? await this.downloadToTMP(screenshot) - : undefined; + const filePath = + screenshot && this.downloadPath + ? await this.downloadToTMP(screenshot) + : undefined; if (screenshot) { - $.export("$summary", "Successfully captured screenshot from page."); + $.export("$summary", `Successfully captured screenshot from ${url}`); } return filePath diff --git a/components/puppeteer/package.json b/components/puppeteer/package.json index 8b6eed6959edf..e38f1b6c7e38c 100644 --- a/components/puppeteer/package.json +++ b/components/puppeteer/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/puppeteer", - "version": "1.0.1", + "version": "1.0.2", "description": "Pipedream Puppeteer Components", "main": "puppeteer.app.mjs", "keywords": [