screenshotone-api-sdk 1.0.19 → 1.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +113 -6
- package/jest.config.js +6 -0
- package/package.json +4 -1
- package/src/main.ts +140 -6
- package/tests/main.test.ts +21 -0
package/dist/main.js
CHANGED
|
@@ -33,7 +33,7 @@ const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
|
33
33
|
const API_BASE_URL = "https://api.screenshotone.com";
|
|
34
34
|
const API_TAKE_PATH = "/take";
|
|
35
35
|
/**
|
|
36
|
-
* Represents an API client for the
|
|
36
|
+
* Represents an API client for the ScreenshotOne.com API.
|
|
37
37
|
*/
|
|
38
38
|
class Client {
|
|
39
39
|
constructor(accessKey, secretKey) {
|
|
@@ -44,6 +44,7 @@ class Client {
|
|
|
44
44
|
const query = options.toQuery();
|
|
45
45
|
query.append("access_key", this.accessKey);
|
|
46
46
|
let queryString = query.toString();
|
|
47
|
+
console.log(queryString);
|
|
47
48
|
const signature = crypto.createHmac("sha256", this.secretKey)
|
|
48
49
|
.update(queryString, 'utf-8')
|
|
49
50
|
.digest('hex');
|
|
@@ -65,22 +66,33 @@ exports.Client = Client;
|
|
|
65
66
|
* Represents options for taking screenshots.
|
|
66
67
|
*/
|
|
67
68
|
class TakeOptions {
|
|
68
|
-
constructor(
|
|
69
|
+
constructor() {
|
|
69
70
|
const query = new URLSearchParams();
|
|
70
|
-
query.append("url", url);
|
|
71
71
|
this.query = query;
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
74
74
|
* Initializes take screenshot options with provided website URL.
|
|
75
75
|
*/
|
|
76
76
|
static url(url) {
|
|
77
|
-
|
|
77
|
+
const options = new TakeOptions();
|
|
78
|
+
options.put("url", url);
|
|
79
|
+
return options;
|
|
78
80
|
}
|
|
79
81
|
/**
|
|
80
82
|
* Initializes take screenshot options with provided HTML.
|
|
81
83
|
*/
|
|
82
|
-
static html(
|
|
83
|
-
|
|
84
|
+
static html(html) {
|
|
85
|
+
const options = new TakeOptions();
|
|
86
|
+
options.put("html", html);
|
|
87
|
+
return options;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Initializes take screenshot options with provided Markdown.
|
|
91
|
+
*/
|
|
92
|
+
static markdown(markdown) {
|
|
93
|
+
const options = new TakeOptions();
|
|
94
|
+
options.put("markdown", markdown);
|
|
95
|
+
return options;
|
|
84
96
|
}
|
|
85
97
|
put(key, ...values) {
|
|
86
98
|
for (const value of values) {
|
|
@@ -102,6 +114,56 @@ class TakeOptions {
|
|
|
102
114
|
this.put("error_on_selector_not_found", errorOn ? "true" : "false");
|
|
103
115
|
return this;
|
|
104
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* When reduced_motion set to `true`,
|
|
119
|
+
* the API will request the site to minimize the amount of non-essential motion it uses.
|
|
120
|
+
* When the site supports it, it should use animations as least as possible.
|
|
121
|
+
*/
|
|
122
|
+
reducedMotion(darkMode) {
|
|
123
|
+
this.put("reduced_motion", darkMode ? "true" : "false");
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* If you want to request the page and it is supported to be rendered for printers, specify `print`.
|
|
128
|
+
* If the page is by default rendered for printers and you want it to be rendered for screens,
|
|
129
|
+
* use `screen`.
|
|
130
|
+
*/
|
|
131
|
+
mediaType(mediaType) {
|
|
132
|
+
this.put("media_type", mediaType);
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Set `true` to request site rendering, if supported, in the dark mode.
|
|
137
|
+
* Set `false` to request site rendering in the light mode if supported.
|
|
138
|
+
* If you don’t set the parameter. The site is rendered in the default mode.
|
|
139
|
+
*/
|
|
140
|
+
darkMode(darkMode) {
|
|
141
|
+
this.put("dark_mode", darkMode ? "true" : "false");
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The `hide_selectors` option allows hiding elements before taking a screenshot.
|
|
146
|
+
* You can specify as many selectors as you wish.
|
|
147
|
+
* All elements that match each selector will be hidden by
|
|
148
|
+
* setting the `display` style property to none `!important`.
|
|
149
|
+
*/
|
|
150
|
+
hideSelectors(...selectors) {
|
|
151
|
+
this.put("hide_selectors", ...selectors);
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The scripts_wain_until option allows you to wait until a given
|
|
156
|
+
* set of events after the scripts were executed.
|
|
157
|
+
* It accepts the same values as wait_until and can have multiple values:
|
|
158
|
+
* - `load`: the navigation is successful when the load even is fired;
|
|
159
|
+
* - `domcontentloaded`: the navigation is finished when the DOMContentLoaded even is fired;
|
|
160
|
+
* - `networkidle0`: the navigation is finished when there are no more than 0 network connections for at least 500 ms;
|
|
161
|
+
* _ `networkidle2`: consider navigation to be finished when there are no more than 2 network connections for at least 500 ms.
|
|
162
|
+
*/
|
|
163
|
+
scriptsWaitUntil(...until) {
|
|
164
|
+
this.put("scripts_wait_until", ...until);
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
105
167
|
/**
|
|
106
168
|
* Styles specifies custom CSS styles for the page.
|
|
107
169
|
*/
|
|
@@ -145,6 +207,16 @@ class TakeOptions {
|
|
|
145
207
|
this.put("omit_background", omitBackground ? "true" : "false");
|
|
146
208
|
return this;
|
|
147
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Instead of manually specifying viewport parameters like width and height,
|
|
212
|
+
* you can specify a device to use for emulation.
|
|
213
|
+
* In addition, other parameters of the viewport, including the user agent,
|
|
214
|
+
* will be set automatically.
|
|
215
|
+
*/
|
|
216
|
+
viewportDevice(viewportDevice) {
|
|
217
|
+
this.put("viewport_device", viewportDevice);
|
|
218
|
+
return this;
|
|
219
|
+
}
|
|
148
220
|
/**
|
|
149
221
|
* Sets the width of the browser viewport (pixels).
|
|
150
222
|
*/
|
|
@@ -166,6 +238,27 @@ class TakeOptions {
|
|
|
166
238
|
this.put("device_scale_factor", deviceScaleFactor.toString());
|
|
167
239
|
return this;
|
|
168
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Whether the meta viewport tag is taken into account. Defaults to `false`.
|
|
243
|
+
*/
|
|
244
|
+
viewportMobile(isMobile) {
|
|
245
|
+
this.put("viewport_mobile", isMobile ? "true" : "false");
|
|
246
|
+
return this;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* The default value is `false`. Set to `true` if the viewport supports touch events.
|
|
250
|
+
*/
|
|
251
|
+
viewportHasTouch(hasTouch) {
|
|
252
|
+
this.put("viewport_has_touch", hasTouch ? "true" : "false");
|
|
253
|
+
return this;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* The default value is `false`. Set to `true` if the viewport is in landscape mode.
|
|
257
|
+
*/
|
|
258
|
+
viewportLandscape(landscape) {
|
|
259
|
+
this.put("viewport_landscape", landscape ? "true" : "false");
|
|
260
|
+
return this;
|
|
261
|
+
}
|
|
169
262
|
/**
|
|
170
263
|
* Sets geolocation latitude for the request.
|
|
171
264
|
* Both latitude and longitude are required if one of them is set.
|
|
@@ -195,6 +288,20 @@ class TakeOptions {
|
|
|
195
288
|
this.put("block_ads", blockAds ? "true" : "false");
|
|
196
289
|
return this;
|
|
197
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* Blocks cookie banners.
|
|
293
|
+
*/
|
|
294
|
+
blockCookieBanners(block) {
|
|
295
|
+
this.put("block_cookie_banners", block ? "true" : "false");
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Blocks chats.
|
|
300
|
+
*/
|
|
301
|
+
blockChats(block) {
|
|
302
|
+
this.put("block_chats", block ? "true" : "false");
|
|
303
|
+
return this;
|
|
304
|
+
}
|
|
198
305
|
/**
|
|
199
306
|
* Blocks trackers.
|
|
200
307
|
*/
|
package/jest.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "screenshotone-api-sdk",
|
|
3
3
|
"homepage": "https://screenshotone.com",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.20",
|
|
5
5
|
"description": "Use ScreenshotOne.com API to generate screenshots of any website.",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -31,7 +31,10 @@
|
|
|
31
31
|
"types": "dist/@types/main.d.ts",
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/big.js": "^6.1.3",
|
|
34
|
+
"@types/jest": "^29.0.1",
|
|
34
35
|
"@types/node": "^17.0.40",
|
|
36
|
+
"jest": "^29.0.3",
|
|
37
|
+
"ts-jest": "^29.0.0",
|
|
35
38
|
"typescript": "^4.7.3"
|
|
36
39
|
},
|
|
37
40
|
"dependencies": {
|
package/src/main.ts
CHANGED
|
@@ -6,7 +6,7 @@ const API_BASE_URL = "https://api.screenshotone.com";
|
|
|
6
6
|
const API_TAKE_PATH = "/take";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Represents an API client for the
|
|
9
|
+
* Represents an API client for the ScreenshotOne.com API.
|
|
10
10
|
*/
|
|
11
11
|
export class Client {
|
|
12
12
|
private readonly accessKey: string;
|
|
@@ -22,6 +22,8 @@ export class Client {
|
|
|
22
22
|
query.append("access_key", this.accessKey);
|
|
23
23
|
let queryString = query.toString();
|
|
24
24
|
|
|
25
|
+
console.log(queryString);
|
|
26
|
+
|
|
25
27
|
const signature = crypto.createHmac("sha256", this.secretKey)
|
|
26
28
|
.update(queryString, 'utf-8')
|
|
27
29
|
.digest('hex');
|
|
@@ -49,9 +51,8 @@ export class Client {
|
|
|
49
51
|
export class TakeOptions {
|
|
50
52
|
private readonly query: URLSearchParams;
|
|
51
53
|
|
|
52
|
-
private constructor(
|
|
54
|
+
private constructor() {
|
|
53
55
|
const query = new URLSearchParams();
|
|
54
|
-
query.append("url", url);
|
|
55
56
|
|
|
56
57
|
this.query = query;
|
|
57
58
|
}
|
|
@@ -60,14 +61,30 @@ export class TakeOptions {
|
|
|
60
61
|
* Initializes take screenshot options with provided website URL.
|
|
61
62
|
*/
|
|
62
63
|
static url(url: string): TakeOptions {
|
|
63
|
-
|
|
64
|
+
const options = new TakeOptions();
|
|
65
|
+
options.put("url", url);
|
|
66
|
+
|
|
67
|
+
return options;
|
|
64
68
|
}
|
|
65
69
|
|
|
66
70
|
/**
|
|
67
71
|
* Initializes take screenshot options with provided HTML.
|
|
68
72
|
*/
|
|
69
|
-
static html(
|
|
70
|
-
|
|
73
|
+
static html(html: string): TakeOptions {
|
|
74
|
+
const options = new TakeOptions();
|
|
75
|
+
options.put("html", html);
|
|
76
|
+
|
|
77
|
+
return options;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Initializes take screenshot options with provided Markdown.
|
|
82
|
+
*/
|
|
83
|
+
static markdown(markdown: string): TakeOptions {
|
|
84
|
+
const options = new TakeOptions();
|
|
85
|
+
options.put("markdown", markdown);
|
|
86
|
+
|
|
87
|
+
return options;
|
|
71
88
|
}
|
|
72
89
|
|
|
73
90
|
private put(key: string, ...values: string[]) {
|
|
@@ -94,6 +111,66 @@ export class TakeOptions {
|
|
|
94
111
|
return this;
|
|
95
112
|
}
|
|
96
113
|
|
|
114
|
+
/**
|
|
115
|
+
* When reduced_motion set to `true`,
|
|
116
|
+
* the API will request the site to minimize the amount of non-essential motion it uses.
|
|
117
|
+
* When the site supports it, it should use animations as least as possible.
|
|
118
|
+
*/
|
|
119
|
+
reducedMotion(darkMode: boolean): TakeOptions {
|
|
120
|
+
this.put("reduced_motion", darkMode ? "true" : "false");
|
|
121
|
+
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* If you want to request the page and it is supported to be rendered for printers, specify `print`.
|
|
127
|
+
* If the page is by default rendered for printers and you want it to be rendered for screens,
|
|
128
|
+
* use `screen`.
|
|
129
|
+
*/
|
|
130
|
+
mediaType(mediaType: string): TakeOptions {
|
|
131
|
+
this.put("media_type", mediaType);
|
|
132
|
+
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Set `true` to request site rendering, if supported, in the dark mode.
|
|
138
|
+
* Set `false` to request site rendering in the light mode if supported.
|
|
139
|
+
* If you don’t set the parameter. The site is rendered in the default mode.
|
|
140
|
+
*/
|
|
141
|
+
darkMode(darkMode: boolean): TakeOptions {
|
|
142
|
+
this.put("dark_mode", darkMode ? "true" : "false");
|
|
143
|
+
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The `hide_selectors` option allows hiding elements before taking a screenshot.
|
|
149
|
+
* You can specify as many selectors as you wish.
|
|
150
|
+
* All elements that match each selector will be hidden by
|
|
151
|
+
* setting the `display` style property to none `!important`.
|
|
152
|
+
*/
|
|
153
|
+
hideSelectors(...selectors: string[]): TakeOptions {
|
|
154
|
+
this.put("hide_selectors", ...selectors);
|
|
155
|
+
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The scripts_wain_until option allows you to wait until a given
|
|
161
|
+
* set of events after the scripts were executed.
|
|
162
|
+
* It accepts the same values as wait_until and can have multiple values:
|
|
163
|
+
* - `load`: the navigation is successful when the load even is fired;
|
|
164
|
+
* - `domcontentloaded`: the navigation is finished when the DOMContentLoaded even is fired;
|
|
165
|
+
* - `networkidle0`: the navigation is finished when there are no more than 0 network connections for at least 500 ms;
|
|
166
|
+
* _ `networkidle2`: consider navigation to be finished when there are no more than 2 network connections for at least 500 ms.
|
|
167
|
+
*/
|
|
168
|
+
scriptsWaitUntil(...until: string[]): TakeOptions {
|
|
169
|
+
this.put("scripts_wait_until", ...until);
|
|
170
|
+
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
|
|
97
174
|
/**
|
|
98
175
|
* Styles specifies custom CSS styles for the page.
|
|
99
176
|
*/
|
|
@@ -150,6 +227,18 @@ export class TakeOptions {
|
|
|
150
227
|
return this;
|
|
151
228
|
}
|
|
152
229
|
|
|
230
|
+
/**
|
|
231
|
+
* Instead of manually specifying viewport parameters like width and height,
|
|
232
|
+
* you can specify a device to use for emulation.
|
|
233
|
+
* In addition, other parameters of the viewport, including the user agent,
|
|
234
|
+
* will be set automatically.
|
|
235
|
+
*/
|
|
236
|
+
viewportDevice(viewportDevice: string): TakeOptions {
|
|
237
|
+
this.put("viewport_device", viewportDevice);
|
|
238
|
+
|
|
239
|
+
return this;
|
|
240
|
+
}
|
|
241
|
+
|
|
153
242
|
/**
|
|
154
243
|
* Sets the width of the browser viewport (pixels).
|
|
155
244
|
*/
|
|
@@ -177,6 +266,33 @@ export class TakeOptions {
|
|
|
177
266
|
return this;
|
|
178
267
|
}
|
|
179
268
|
|
|
269
|
+
/**
|
|
270
|
+
* Whether the meta viewport tag is taken into account. Defaults to `false`.
|
|
271
|
+
*/
|
|
272
|
+
viewportMobile(isMobile: boolean): TakeOptions {
|
|
273
|
+
this.put("viewport_mobile", isMobile ? "true" : "false");
|
|
274
|
+
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* The default value is `false`. Set to `true` if the viewport supports touch events.
|
|
280
|
+
*/
|
|
281
|
+
viewportHasTouch(hasTouch: boolean): TakeOptions {
|
|
282
|
+
this.put("viewport_has_touch", hasTouch ? "true" : "false");
|
|
283
|
+
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The default value is `false`. Set to `true` if the viewport is in landscape mode.
|
|
289
|
+
*/
|
|
290
|
+
viewportLandscape(landscape: boolean): TakeOptions {
|
|
291
|
+
this.put("viewport_landscape", landscape ? "true" : "false");
|
|
292
|
+
|
|
293
|
+
return this;
|
|
294
|
+
}
|
|
295
|
+
|
|
180
296
|
/**
|
|
181
297
|
* Sets geolocation latitude for the request.
|
|
182
298
|
* Both latitude and longitude are required if one of them is set.
|
|
@@ -214,6 +330,24 @@ export class TakeOptions {
|
|
|
214
330
|
return this;
|
|
215
331
|
}
|
|
216
332
|
|
|
333
|
+
/**
|
|
334
|
+
* Blocks cookie banners.
|
|
335
|
+
*/
|
|
336
|
+
blockCookieBanners(block: boolean): TakeOptions {
|
|
337
|
+
this.put("block_cookie_banners", block ? "true" : "false");
|
|
338
|
+
|
|
339
|
+
return this;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Blocks chats.
|
|
344
|
+
*/
|
|
345
|
+
blockChats(block: boolean): TakeOptions {
|
|
346
|
+
this.put("block_chats", block ? "true" : "false");
|
|
347
|
+
|
|
348
|
+
return this;
|
|
349
|
+
}
|
|
350
|
+
|
|
217
351
|
/**
|
|
218
352
|
* Blocks trackers.
|
|
219
353
|
*/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { TakeOptions, Client } from '../src/main';
|
|
2
|
+
|
|
3
|
+
describe('testing client', () => {
|
|
4
|
+
const client = new Client('RcLsdM6uhIN6gw', 'MW2vfkkgLTzGGw');
|
|
5
|
+
|
|
6
|
+
test('example.com with block ads URL generated and signed correctly', () => {
|
|
7
|
+
const options = TakeOptions.url('https://example.com').blockAds(true);
|
|
8
|
+
|
|
9
|
+
expect(client.generateTakeURL(options)).toBe('https://api.screenshotone.com/take?url=https%3A%2F%2Fexample.com&block_ads=true&access_key=RcLsdM6uhIN6gw&signature=3cf1edeafc139f41191928c1c5b8b04fe1d5722560244ccdd76a55d69120bbac');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('example.com with block ads, full page and user agent URL generated and signed correctly', () => {
|
|
13
|
+
const options = TakeOptions
|
|
14
|
+
.url('https://example.com')
|
|
15
|
+
.blockAds(true)
|
|
16
|
+
.fullPage(true)
|
|
17
|
+
.userAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36');
|
|
18
|
+
|
|
19
|
+
expect(client.generateTakeURL(options)).toBe('https://api.screenshotone.com/take?url=https%3A%2F%2Fexample.com&block_ads=true&full_page=true&user_agent=Mozilla%2F5.0+%28X11%3B+Linux+x86_64%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F51.0.2704.103+Safari%2F537.36&access_key=RcLsdM6uhIN6gw&signature=ea217db7d746c8bf9b710257d62ef09670b003137d239ee4c81eae3645b4a901');
|
|
20
|
+
});
|
|
21
|
+
});
|