Skip to content

Commit ebbc0e6

Browse files
authored
Browserless - update base_url (#17880)
* updates * pnpm-lock.yaml * updates * update
1 parent 678012a commit ebbc0e6

File tree

7 files changed

+70
-43
lines changed

7 files changed

+70
-43
lines changed
Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,63 @@
1-
// legacy_hash_id: a_B0ip1E
2-
import { axios } from "@pipedream/platform";
1+
import browserless from "../../browserless.app.mjs";
2+
import fs from "fs";
33

44
export default {
55
key: "browserless-convert-html-to-pdf",
66
name: "Generate PDF from HTML String",
77
description: "See https://docs.browserless.io/docs/pdf.html",
8-
version: "0.4.1",
8+
version: "0.4.2",
99
type: "action",
1010
props: {
11-
browserless: {
12-
type: "app",
13-
app: "browserless",
14-
},
11+
browserless,
1512
html: {
1613
type: "string",
1714
label: "HTML String",
1815
description: "HTML to render as a PDF",
1916
},
17+
downloadPath: {
18+
type: "string",
19+
label: "Download Path",
20+
description: "Download the screenshot to the `/tmp` directory with the specified filename",
21+
optional: true,
22+
},
23+
syncDir: {
24+
type: "dir",
25+
accessMode: "write",
26+
sync: true,
27+
},
28+
},
29+
methods: {
30+
async downloadToTMP(screenshot) {
31+
const path = this.downloadPath.includes("/tmp")
32+
? this.downloadPath
33+
: `/tmp/${this.downloadPath}`;
34+
fs.writeFileSync(path, screenshot);
35+
return path;
36+
},
2037
},
2138
async run({ $ }) {
2239
const { html } = this;
2340

24-
const data = await axios($, {
25-
method: "POST",
26-
url: `https://chrome.browserless.io/pdf?token=${this.browserless.$auth.api_key}`,
27-
headers: {
28-
"Cache-Control": "no-cache",
29-
"Content-Type": "application/json",
30-
},
31-
responseType: "arraybuffer",
41+
const data = await this.browserless.convertHtmlToPdf({
42+
$,
3243
data: {
3344
html,
34-
options: {
35-
displayHeaderFooter: true,
36-
printBackground: false,
37-
format: "Letter",
38-
},
3945
},
4046
});
4147

42-
$.export("pdf", Buffer.from(data, "binary").toString("base64"));
48+
const result = {
49+
pdf: Buffer.from(data, "binary").toString("base64"),
50+
};
51+
52+
if (data && this.downloadPath) {
53+
const filePath = await this.downloadToTMP(data);
54+
result.filePath = filePath;
55+
}
56+
57+
if (data) {
58+
$.export("$summary", "Successfully generated PDF from HTML string.");
59+
}
60+
61+
return result;
4362
},
4463
};

components/browserless/actions/scrape-url-list/scrape-url-list.mjs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "browserless-scrape-url-list",
55
name: "Scrape URL List",
66
description: "Scrape content from a list of pages. [See the documentation](https://www.browserless.io/docs/scrape).",
7-
version: "0.0.2",
7+
version: "0.0.3",
88
type: "action",
99
props: {
1010
browserless,
@@ -28,23 +28,18 @@ export default {
2828
}
2929

3030
const result = {};
31-
const promises = [];
3231

3332
for (const url of this.urls) {
34-
promises.push(this.browserless.scrape({
33+
const response = await this.browserless.scrape({
3534
$,
3635
data: {
3736
url,
3837
elements: this.selectors?.map((selector) => ({
3938
selector,
4039
})),
4140
},
42-
}));
43-
}
44-
45-
const responses = await Promise.all(promises);
46-
for (let i = 0; i < promises.length; i++) {
47-
result[this.urls[i]] = responses[i].data[0].results[0].text;
41+
});
42+
result[url] = response.data[0].results[0].text;
4843
}
4944

5045
return result;

components/browserless/actions/scrape-url/scrape-url.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "browserless-scrape-url",
55
name: "Scrape URL",
66
description: "Scrape content from a page. [See the documentation](https://www.browserless.io/docs/scrape).",
7-
version: "0.0.2",
7+
version: "0.0.3",
88
type: "action",
99
props: {
1010
browserless,

components/browserless/actions/take-screenshot/take-screenshot.mjs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "browserless-take-screenshot",
66
name: "Take a Screenshot",
77
description: "Take a screenshot of a page. [See the documentation](https://www.browserless.io/docs/screenshot)",
8-
version: "0.5.4",
8+
version: "0.5.5",
99
type: "action",
1010
props: {
1111
browserless,
@@ -20,10 +20,10 @@ export default {
2020
description: "Download the screenshot to the `/tmp` directory with the specified filename",
2121
optional: true,
2222
},
23-
waitFor: {
23+
waitForSelector: {
2424
type: "string",
25-
label: "waitFor",
26-
description: "Allows you to wait for a selector to appear in the DOM, to wait for a timeout to happen, or to execute a custom function before screenshotting. See [more details in the API Doc](https://www.browserless.io/docs/screenshot#custom-behavior-with-waitfor)",
25+
label: "Selector",
26+
description: "Allows you to wait for a selector to appear in the DOM. See [more details in the API Doc](https://www.browserless.io/docs/screenshot#custom-behavior-with-waitfor)",
2727
optional: true,
2828
},
2929
syncDir: {
@@ -45,9 +45,10 @@ export default {
4545
const screenshot = await this.browserless.takeScreenshot({
4646
data: {
4747
url: this.url,
48-
waitFor: !isNaN(this.waitFor)
49-
? parseInt(this.waitFor)
50-
: this.waitFor,
48+
bestAttempt: true,
49+
waitForSelector: this.waitForSelector
50+
? `{ "selector": "${this.waitForSelector}" }`
51+
: undefined,
5152
},
5253
$,
5354
});

components/browserless/browserless.app.mjs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { axios } from "@pipedream/platform";
2+
import { ConfigurationError } from "@pipedream/platform";
23

34
export default {
45
type: "app",
56
app: "browserless",
67
propDefinitions: {},
78
methods: {
89
_baseUrl() {
9-
return "https://chrome.browserless.io";
10+
if (!this.$auth.base_url) {
11+
throw new ConfigurationError("Please reconnect your Browserless account because there are recent changes in Browserless API");
12+
}
13+
return `https://${this.$auth.base_url}`;
1014
},
1115
_auth() {
1216
return {
@@ -50,5 +54,13 @@ export default {
5054
...opts,
5155
});
5256
},
57+
convertHtmlToPdf(opts = {}) {
58+
return this._makeRequest({
59+
path: "/pdf",
60+
method: "post",
61+
responseType: "arraybuffer",
62+
...opts,
63+
});
64+
},
5365
},
5466
};

components/browserless/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/browserless",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "Pipedream Browserless Components",
55
"main": "browserless.app.mjs",
66
"keywords": [
@@ -13,7 +13,7 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16-
"@pipedream/platform": "^1.5.1",
16+
"@pipedream/platform": "^3.1.0",
1717
"puppeteer-core": "^19.7.5"
1818
}
1919
}

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)