Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

normalize url in puppeteer actions #13869

Merged
merged 9 commits into from
Sep 9, 2024
19 changes: 19 additions & 0 deletions components/puppeteer/actions/common/common.mjs
Original file line number Diff line number Diff line change
@@ -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;
},
},
};
18 changes: 9 additions & 9 deletions components/puppeteer/actions/get-html/get-html.mjs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import puppeteer from "../../puppeteer.app.mjs";
import common from "../common/common.mjs";

export default {
dannyroosevelt marked this conversation as resolved.
Show resolved Hide resolved
...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;
Expand Down
18 changes: 9 additions & 9 deletions components/puppeteer/actions/get-page-title/get-page-title.mjs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
46 changes: 27 additions & 19 deletions components/puppeteer/actions/get-pdf/get-pdf.mjs
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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: {
Expand All @@ -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,
},
},
Expand Down Expand Up @@ -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
Expand Down
66 changes: 38 additions & 28 deletions components/puppeteer/actions/screenshot-page/screenshot-page.mjs
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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: {
Expand All @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/puppeteer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/puppeteer",
"version": "1.0.1",
"version": "1.0.2",
"description": "Pipedream Puppeteer Components",
"main": "puppeteer.app.mjs",
"keywords": [
Expand Down
Loading