screenshotone-api-sdk 1.0.17 → 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/README.md CHANGED
@@ -1,34 +1,48 @@
1
- # tssdk
1
+ # jssdk
2
2
 
3
- [![Build](https://github.com/screenshotone/tssdk/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/screenshotone/tssdk/actions/workflows/build.yml)
3
+ [![Build](https://github.com/screenshotone/jssdk/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/screenshotone/jssdk/actions/workflows/build.yml)
4
+ [![NPM package](https://img.shields.io/npm/v/screenshotone-api-sdk.svg?branch=main)](https://www.npmjs.com/package/screenshotone-api-sdk)
4
5
 
5
6
  An official [Screenshot API](https://screenshotone.com/) client for JavaScript and TypeScript.
6
7
 
7
8
  It takes minutes to start taking screenshots. Just [sign up](https://screenshotone.com/) to get access and secret keys, import the client, and you are ready to go.
8
9
 
10
+ The SDK client is synchronized with the latest [screenshot API options](https://screenshotone.com/docs/options/).
11
+
9
12
  ## Installation
10
13
 
11
14
  ```shell
12
-
15
+ npm install screenshotone-api-sdk --save
13
16
  ```
14
17
 
15
18
  ## Usage
16
19
 
17
- Import the library:
18
- ```go
19
-
20
- ```
21
-
22
- Generate a screenshot URL without executing request:
23
- ```go
24
-
25
- ```
26
-
27
- Take a screenshot and save the image in the file:
28
- ```go
29
-
20
+ Generate a screenshot URL without executing the request. Or download the screenshot. It is up to you:
21
+ ```javascript
22
+ import * as fs from 'fs';
23
+ import * as screenshotone from 'screenshotone-api-sdk';
24
+
25
+ // create API client
26
+ const client = new screenshotone.Client("<access key>", "<secret key>");
27
+
28
+ // set up options
29
+ const options = screenshotone.TakeOptions
30
+ .url("https://example.com")
31
+ .delay(3)
32
+ .blockAds(true);
33
+
34
+ // generate URL
35
+ const url = client.generateTakeURL(options);
36
+ console.log(url);
37
+ // expected output: https://api.screenshotone.com/take?url=https%3A%2F%2Fexample.com&delay=3&block_ads=true&access_key=%3Caccess+key%3E&signature=7f3419ece2c53ed2c7923c7d5deef290d662c3643822bf69ec8259ce10b3ea61
38
+
39
+ // or download the screenshot
40
+ const imageBlob = await client.take(options);
41
+ const buffer = Buffer.from(await imageBlob.arrayBuffer());
42
+ fs.writeFileSync("example.png", buffer)
43
+ // the screenshot is store in the example.png file
30
44
  ```
31
45
 
32
46
  ## License
33
47
 
34
- `screenshotone/tssdk` is released under [the MIT license](LICENSE).
48
+ `screenshotone/jssdk` is released under [the MIT license](LICENSE).
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 screenshotone.com API.
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,16 +66,33 @@ exports.Client = Client;
65
66
  * Represents options for taking screenshots.
66
67
  */
67
68
  class TakeOptions {
68
- constructor(url) {
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
- return new TakeOptions(url);
77
+ const options = new TakeOptions();
78
+ options.put("url", url);
79
+ return options;
80
+ }
81
+ /**
82
+ * Initializes take screenshot options with provided 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;
78
96
  }
79
97
  put(key, ...values) {
80
98
  for (const value of values) {
@@ -82,6 +100,84 @@ class TakeOptions {
82
100
  }
83
101
  }
84
102
  ;
103
+ /**
104
+ * Selector is a CSS-like selector of the element to take a screenshot of.
105
+ */
106
+ selector(selector) {
107
+ this.put("selector", selector);
108
+ return this;
109
+ }
110
+ /**
111
+ * It determines the behavior of what to do when selector is not found.
112
+ */
113
+ errorOnSelectorNotFound(errorOn) {
114
+ this.put("error_on_selector_not_found", errorOn ? "true" : "false");
115
+ return this;
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
+ }
167
+ /**
168
+ * Styles specifies custom CSS styles for the page.
169
+ */
170
+ styles(styles) {
171
+ this.put("styles", styles);
172
+ return this;
173
+ }
174
+ /**
175
+ * Scripts specifies custom scripts for the page.
176
+ */
177
+ scripts(scripts) {
178
+ this.put("scripts", scripts);
179
+ return this;
180
+ }
85
181
  /**
86
182
  * Renders the full page.
87
183
  */
@@ -111,6 +207,16 @@ class TakeOptions {
111
207
  this.put("omit_background", omitBackground ? "true" : "false");
112
208
  return this;
113
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
+ }
114
220
  /**
115
221
  * Sets the width of the browser viewport (pixels).
116
222
  */
@@ -132,6 +238,27 @@ class TakeOptions {
132
238
  this.put("device_scale_factor", deviceScaleFactor.toString());
133
239
  return this;
134
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
+ }
135
262
  /**
136
263
  * Sets geolocation latitude for the request.
137
264
  * Both latitude and longitude are required if one of them is set.
@@ -161,6 +288,20 @@ class TakeOptions {
161
288
  this.put("block_ads", blockAds ? "true" : "false");
162
289
  return this;
163
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
+ }
164
305
  /**
165
306
  * Blocks trackers.
166
307
  */
package/jest.config.js ADDED
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ transform: { '^.+\\.ts?$': 'ts-jest' },
3
+ testEnvironment: 'node',
4
+ testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
5
+ moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
6
+ };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "screenshotone-api-sdk",
3
3
  "homepage": "https://screenshotone.com",
4
- "version": "1.0.17",
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",
8
- "url": "git+https://github.com/screenshotone/tssdk.git"
8
+ "url": "git+https://github.com/screenshotone/jssdk.git"
9
9
  },
10
10
  "keywords": [
11
11
  "screenshot",
@@ -25,13 +25,16 @@
25
25
  "license": "MIT",
26
26
  "bugs": {
27
27
  "email": "support@screenshotone.com",
28
- "url": "https://github.com/screenshotone/tssdk/issues"
28
+ "url": "https://github.com/screenshotone/jssdk/issues"
29
29
  },
30
30
  "main": "dist/main.js",
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 screenshotone.com API.
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(url: string) {
54
+ private constructor() {
53
55
  const query = new URLSearchParams();
54
- query.append("url", url);
55
56
 
56
57
  this.query = query;
57
58
  }
@@ -60,7 +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
- return new TakeOptions(url);
64
+ const options = new TakeOptions();
65
+ options.put("url", url);
66
+
67
+ return options;
68
+ }
69
+
70
+ /**
71
+ * Initializes take screenshot options with provided HTML.
72
+ */
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;
64
88
  }
65
89
 
66
90
  private put(key: string, ...values: string[]) {
@@ -69,6 +93,103 @@ export class TakeOptions {
69
93
  }
70
94
  };
71
95
 
96
+ /**
97
+ * Selector is a CSS-like selector of the element to take a screenshot of.
98
+ */
99
+ selector(selector: string) {
100
+ this.put("selector", selector);
101
+
102
+ return this;
103
+ }
104
+
105
+ /**
106
+ * It determines the behavior of what to do when selector is not found.
107
+ */
108
+ errorOnSelectorNotFound(errorOn: boolean) {
109
+ this.put("error_on_selector_not_found", errorOn ? "true" : "false");
110
+
111
+ return this;
112
+ }
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
+
174
+ /**
175
+ * Styles specifies custom CSS styles for the page.
176
+ */
177
+ styles(styles: string) {
178
+ this.put("styles", styles);
179
+
180
+ return this;
181
+ }
182
+
183
+ /**
184
+ * Scripts specifies custom scripts for the page.
185
+ */
186
+ scripts(scripts: string) {
187
+ this.put("scripts", scripts);
188
+
189
+ return this;
190
+ }
191
+
192
+
72
193
  /**
73
194
  * Renders the full page.
74
195
  */
@@ -106,6 +227,18 @@ export class TakeOptions {
106
227
  return this;
107
228
  }
108
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
+
109
242
  /**
110
243
  * Sets the width of the browser viewport (pixels).
111
244
  */
@@ -133,6 +266,33 @@ export class TakeOptions {
133
266
  return this;
134
267
  }
135
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
+
136
296
  /**
137
297
  * Sets geolocation latitude for the request.
138
298
  * Both latitude and longitude are required if one of them is set.
@@ -170,6 +330,24 @@ export class TakeOptions {
170
330
  return this;
171
331
  }
172
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
+
173
351
  /**
174
352
  * Blocks trackers.
175
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
+ });