-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
34 lines (32 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const axios = require("axios");
const fs = require("fs");
class captureWebsite {
constructor(page) {
this.viewport = page["viewport"];
this.screenshot = this.constructor.getScreen;
}
static async getScreen(url, path) {
if (!url) return new Error("url is required");
if (!path) return new Error("path is required");
if (!this.viewport) return new Error("viewport is required");
const viewport = `${this.viewport.width}x${this.viewport.width}`;
return new Promise((resolve, reject) => {
axios
.get("https://api.roxza.me/v1/capture?link=" + url + "&viewport=" + viewport, {
responseType: "arraybuffer"
})
.then((getData) => {
try {
fs.writeFileSync(path, getData.data);
resolve(true);
} catch (e) {
throw new Error("Something went wrong.");
}
})
.catch((err) => {
throw new Error("Unable to connect to website.");
});
});
}
}
module.exports = captureWebsite;