|
| 1 | +import { ConfigurationError } from "@pipedream/platform"; |
| 2 | +import common from "../common/base.mjs"; |
| 3 | +import utils from "../../common/utils.mjs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + ...common, |
| 7 | + key: "cloudflare_browser_rendering-get-html-content", |
| 8 | + name: "Get HTML Content", |
| 9 | + description: "Fetches rendered HTML content from provided URL or HTML. [See the documentation](https://developers.cloudflare.com/api/resources/browser_rendering/subresources/content/)", |
| 10 | + version: "0.0.1", |
| 11 | + type: "action", |
| 12 | + methods: { |
| 13 | + getHtmlContent(args = {}) { |
| 14 | + return this.app.post({ |
| 15 | + path: "/content", |
| 16 | + ...args, |
| 17 | + }); |
| 18 | + }, |
| 19 | + }, |
| 20 | + async run({ $ }) { |
| 21 | + const { |
| 22 | + getHtmlContent, |
| 23 | + html, |
| 24 | + url, |
| 25 | + viewportHeight, |
| 26 | + viewportWidth, |
| 27 | + viewportDeviceScaleFactor, |
| 28 | + viewportHasTouch, |
| 29 | + viewportIsLandscape, |
| 30 | + viewportIsMobile, |
| 31 | + userAgent, |
| 32 | + additionalSettings, |
| 33 | + } = this; |
| 34 | + |
| 35 | + if (!html && !url) { |
| 36 | + throw new ConfigurationError("Either **HTML** or **URL** is required"); |
| 37 | + } |
| 38 | + |
| 39 | + if ((viewportHeight && !viewportWidth) || (!viewportHeight && viewportWidth)) { |
| 40 | + throw new ConfigurationError("Both **Viewport - Height** and **Viewport - Width** are required when either is provided"); |
| 41 | + } |
| 42 | + |
| 43 | + const response = await getHtmlContent({ |
| 44 | + $, |
| 45 | + data: { |
| 46 | + html, |
| 47 | + url, |
| 48 | + ...(viewportHeight && { |
| 49 | + viewport: { |
| 50 | + height: viewportHeight, |
| 51 | + width: viewportWidth, |
| 52 | + deviceScaleFactor: viewportDeviceScaleFactor, |
| 53 | + hasTouch: viewportHasTouch, |
| 54 | + isLandscape: viewportIsLandscape, |
| 55 | + isMobile: viewportIsMobile, |
| 56 | + }, |
| 57 | + }), |
| 58 | + userAgent, |
| 59 | + ...utils.parseJson(additionalSettings), |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + $.export("$summary", "Successfully fetched HTML content"); |
| 64 | + return response; |
| 65 | + }, |
| 66 | +}; |
0 commit comments