screenshotone-api-sdk 1.0.25 → 1.0.27

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 CHANGED
@@ -26,12 +26,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.TakeOptions = exports.Client = void 0;
29
+ exports.AnimateOptions = exports.TakeOptions = exports.Client = void 0;
30
30
  const crypto = __importStar(require("crypto"));
31
31
  const big_js_1 = __importDefault(require("big.js"));
32
32
  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
+ const API_ANIMATE_PATH = "/animate";
35
36
  /**
36
37
  * Represents an API client for the ScreenshotOne.com API.
37
38
  */
@@ -40,6 +41,14 @@ class Client {
40
41
  this.accessKey = accessKey;
41
42
  this.secretKey = secretKey;
42
43
  }
44
+ /**
45
+ * Generates an URL that can be used to send GET requests for taking screenshots.
46
+ *
47
+ * Warning! Don't share the link publicly generated by this method publicly.
48
+ *
49
+ * @param options Options for taking the screenshot.
50
+ * @returns An URL that will generate the screenshot if requested.
51
+ */
43
52
  generateTakeURL(options) {
44
53
  const query = options.toQuery();
45
54
  query.append("access_key", this.accessKey);
@@ -50,8 +59,63 @@ class Client {
50
59
  queryString += '&signature=' + signature;
51
60
  return `${API_BASE_URL}${API_TAKE_PATH}?${queryString}`;
52
61
  }
62
+ /**
63
+ * Generates the link that can be publicly available, but make sure
64
+ * you read the documentation before at https://screenshotone.com/docs/signed-requests/.
65
+ *
66
+ * @param options
67
+ * @returns A signed URL that will generate the screenshot if requested.
68
+ */
69
+ generateSignedTakeURL(options) {
70
+ const query = options.toQuery();
71
+ query.append("access_key", this.accessKey);
72
+ let queryString = query.toString();
73
+ const signature = crypto.createHmac("sha256", this.secretKey)
74
+ .update(queryString, 'utf-8')
75
+ .digest('hex');
76
+ queryString += '&signature=' + signature;
77
+ return `${API_BASE_URL}${API_TAKE_PATH}?${queryString}`;
78
+ }
79
+ /**
80
+ * Generates the link that can be publicly available, but make sure
81
+ * you read the documentation before at https://screenshotone.com/docs/signed-requests/.
82
+ *
83
+ * @param options
84
+ * @returns A signed URL that will generate the animated screenshot if requested.
85
+ */
86
+ generateSignedAnimateURL(options) {
87
+ const query = options.toQuery();
88
+ query.append("access_key", this.accessKey);
89
+ let queryString = query.toString();
90
+ const signature = crypto.createHmac("sha256", this.secretKey)
91
+ .update(queryString, 'utf-8')
92
+ .digest('hex');
93
+ queryString += '&signature=' + signature;
94
+ return `${API_BASE_URL}${API_ANIMATE_PATH}?${queryString}`;
95
+ }
96
+ /**
97
+ * Generates a screenshot and return the image blob.
98
+ *
99
+ * @param options
100
+ * @returns
101
+ */
53
102
  async take(options) {
54
- const url = this.generateTakeURL(options);
103
+ const url = this.generateSignedTakeURL(options);
104
+ const response = await (0, cross_fetch_1.default)(url);
105
+ if (response.ok) {
106
+ return await response.blob();
107
+ }
108
+ const data = await response.json();
109
+ throw new Error(`failed to take screenshot, response returned ${response.status} ${response.statusText}: ${data?.message}`);
110
+ }
111
+ /**
112
+ * Generates an animated screenshot and return the image blob.
113
+ *
114
+ * @param options
115
+ * @returns
116
+ */
117
+ async animate(options) {
118
+ const url = this.generateSignedAnimateURL(options);
55
119
  const response = await (0, cross_fetch_1.default)(url);
56
120
  if (response.ok) {
57
121
  return await response.blob();
@@ -375,6 +439,24 @@ class TakeOptions {
375
439
  this.put("authorization", authorization);
376
440
  return this;
377
441
  }
442
+ /**
443
+ * It scrolls the page if needed and ensures that the given
444
+ * selector is present in the view when taking a screenshot.
445
+ */
446
+ scrollIntoView(selector) {
447
+ this.put("scroll_into_view", selector);
448
+ return this;
449
+ }
450
+ /**
451
+ * You can use your custom proxy to take screenshots
452
+ * or render HTML with the proxy option.
453
+ *
454
+ * The https, http, socks4 and socks5 proxies are supported.
455
+ */
456
+ proxy(proxy) {
457
+ this.put("proxy", proxy);
458
+ return this;
459
+ }
378
460
  /**
379
461
  * Set cookies for the request.
380
462
  */
@@ -451,3 +533,394 @@ class TakeOptions {
451
533
  }
452
534
  }
453
535
  exports.TakeOptions = TakeOptions;
536
+ /**
537
+ * Represents options for animating screenshots.
538
+ */
539
+ class AnimateOptions {
540
+ constructor() {
541
+ const query = new URLSearchParams();
542
+ this.query = query;
543
+ }
544
+ /**
545
+ * Initializes take screenshot options with provided website URL.
546
+ */
547
+ static url(url) {
548
+ const options = new AnimateOptions();
549
+ options.put("url", url);
550
+ return options;
551
+ }
552
+ /**
553
+ * Initializes take screenshot options with provided HTML.
554
+ */
555
+ static html(html) {
556
+ const options = new AnimateOptions();
557
+ options.put("html", html);
558
+ return options;
559
+ }
560
+ /**
561
+ * Initializes take screenshot options with provided Markdown.
562
+ */
563
+ static markdown(markdown) {
564
+ const options = new AnimateOptions();
565
+ options.put("markdown", markdown);
566
+ return options;
567
+ }
568
+ put(key, ...values) {
569
+ for (const value of values) {
570
+ this.query.append(key, value);
571
+ }
572
+ }
573
+ ;
574
+ /**
575
+ * Available response types:
576
+ * by_format — return exactly the response defined in the format option. If format=png, the response will be the binary representation of the png with the content type header image/png;
577
+ * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don’t care about the results. It also speeds up the response since there are no networking costs involved.
578
+ * The default value is by_format.
579
+ */
580
+ responseType(responseType) {
581
+ this.put("response_type", responseType);
582
+ return this;
583
+ }
584
+ /**
585
+ * When reduced_motion set to `true`,
586
+ * the API will request the site to minimize the amount of non-essential motion it uses.
587
+ * When the site supports it, it should use animations as least as possible.
588
+ */
589
+ reducedMotion(darkMode) {
590
+ this.put("reduced_motion", darkMode ? "true" : "false");
591
+ return this;
592
+ }
593
+ /**
594
+ * If you want to request the page and it is supported to be rendered for printers, specify `print`.
595
+ * If the page is by default rendered for printers and you want it to be rendered for screens,
596
+ * use `screen`.
597
+ */
598
+ mediaType(mediaType) {
599
+ this.put("media_type", mediaType);
600
+ return this;
601
+ }
602
+ /**
603
+ * Set `true` to request site rendering, if supported, in the dark mode.
604
+ * Set `false` to request site rendering in the light mode if supported.
605
+ * If you don’t set the parameter. The site is rendered in the default mode.
606
+ */
607
+ darkMode(darkMode) {
608
+ this.put("dark_mode", darkMode ? "true" : "false");
609
+ return this;
610
+ }
611
+ /**
612
+ * The `hide_selectors` option allows hiding elements before taking a screenshot.
613
+ * You can specify as many selectors as you wish.
614
+ * All elements that match each selector will be hidden by
615
+ * setting the `display` style property to none `!important`.
616
+ */
617
+ hideSelectors(...selectors) {
618
+ this.put("hide_selectors", ...selectors);
619
+ return this;
620
+ }
621
+ /**
622
+ * The scripts_wain_until option allows you to wait until a given
623
+ * set of events after the scripts were executed.
624
+ * It accepts the same values as wait_until and can have multiple values:
625
+ * - `load`: the navigation is successful when the load even is fired;
626
+ * - `domcontentloaded`: the navigation is finished when the DOMContentLoaded even is fired;
627
+ * - `networkidle0`: the navigation is finished when there are no more than 0 network connections for at least 500 ms;
628
+ * _ `networkidle2`: consider navigation to be finished when there are no more than 2 network connections for at least 500 ms.
629
+ */
630
+ scriptsWaitUntil(...until) {
631
+ this.put("scripts_wait_until", ...until);
632
+ return this;
633
+ }
634
+ /**
635
+ * Styles specifies custom CSS styles for the page.
636
+ */
637
+ styles(styles) {
638
+ this.put("styles", styles);
639
+ return this;
640
+ }
641
+ /**
642
+ * You can use your custom proxy to take screenshots
643
+ * or render HTML with the proxy option.
644
+ *
645
+ * The https, http, socks4 and socks5 proxies are supported.
646
+ */
647
+ proxy(proxy) {
648
+ this.put("proxy", proxy);
649
+ return this;
650
+ }
651
+ /**
652
+ * Scripts specifies custom scripts for the page.
653
+ */
654
+ scripts(scripts) {
655
+ this.put("scripts", scripts);
656
+ return this;
657
+ }
658
+ /**
659
+ * Sets response format, one of: "mp4", "avi", "webm", "mov" or "gif".
660
+ */
661
+ format(format) {
662
+ this.put("format", format);
663
+ return this;
664
+ }
665
+ /**
666
+ * Renders a transparent background for the image. Works only if the site has not defined background color.
667
+ * Available for the following response formats: "png", "webp".
668
+ */
669
+ omitBackground(omitBackground) {
670
+ this.put("omit_background", omitBackground ? "true" : "false");
671
+ return this;
672
+ }
673
+ /**
674
+ * Instead of manually specifying viewport parameters like width and height,
675
+ * you can specify a device to use for emulation.
676
+ * In addition, other parameters of the viewport, including the user agent,
677
+ * will be set automatically.
678
+ */
679
+ viewportDevice(viewportDevice) {
680
+ this.put("viewport_device", viewportDevice);
681
+ return this;
682
+ }
683
+ /**
684
+ * Sets the width of the browser viewport (pixels).
685
+ */
686
+ viewportWidth(viewportWidth) {
687
+ this.put("viewport_width", viewportWidth.toString());
688
+ return this;
689
+ }
690
+ /**
691
+ * Sets the height of the browser viewport (pixels).
692
+ */
693
+ viewportHeight(viewportHeight) {
694
+ this.put("viewport_height", viewportHeight.toString());
695
+ return this;
696
+ }
697
+ /**
698
+ * Sets the device scale factor. Acceptable value is one of: 1, 2 or 3.
699
+ */
700
+ deviceScaleFactor(deviceScaleFactor) {
701
+ this.put("device_scale_factor", deviceScaleFactor.toString());
702
+ return this;
703
+ }
704
+ /**
705
+ * Whether the meta viewport tag is taken into account. Defaults to `false`.
706
+ */
707
+ viewportMobile(isMobile) {
708
+ this.put("viewport_mobile", isMobile ? "true" : "false");
709
+ return this;
710
+ }
711
+ /**
712
+ * The default value is `false`. Set to `true` if the viewport supports touch events.
713
+ */
714
+ viewportHasTouch(hasTouch) {
715
+ this.put("viewport_has_touch", hasTouch ? "true" : "false");
716
+ return this;
717
+ }
718
+ /**
719
+ * The default value is `false`. Set to `true` if the viewport is in landscape mode.
720
+ */
721
+ viewportLandscape(landscape) {
722
+ this.put("viewport_landscape", landscape ? "true" : "false");
723
+ return this;
724
+ }
725
+ /**
726
+ * Sets geolocation latitude for the request.
727
+ * Both latitude and longitude are required if one of them is set.
728
+ */
729
+ geolocationLatitude(latitude) {
730
+ this.put("geolocation_latitude", new big_js_1.default(latitude).toFixed(20).replace(/0+$/, ""));
731
+ return this;
732
+ }
733
+ /**
734
+ * Sets geolocation longitude for the request. Both latitude and longitude are required if one of them is set.
735
+ */
736
+ geolocationLongitude(longitude) {
737
+ this.put("geolocation_longitude", new big_js_1.default(longitude).toFixed(20).replace(/0+$/, ""));
738
+ return this;
739
+ }
740
+ /**
741
+ * Sets the geolocation accuracy in meters.
742
+ */
743
+ geolocationAccuracy(accuracy) {
744
+ this.put("geolocation_accuracy", accuracy.toString());
745
+ return this;
746
+ }
747
+ /**
748
+ * Blocks ads.
749
+ */
750
+ blockAds(blockAds) {
751
+ this.put("block_ads", blockAds ? "true" : "false");
752
+ return this;
753
+ }
754
+ /**
755
+ * Blocks cookie banners.
756
+ */
757
+ blockCookieBanners(block) {
758
+ this.put("block_cookie_banners", block ? "true" : "false");
759
+ return this;
760
+ }
761
+ /**
762
+ * Blocks chats.
763
+ */
764
+ blockChats(block) {
765
+ this.put("block_chats", block ? "true" : "false");
766
+ return this;
767
+ }
768
+ /**
769
+ * Blocks trackers.
770
+ */
771
+ blockTrackers(blockTrackers) {
772
+ this.put("block_trackers", blockTrackers ? "true" : "false");
773
+ return this;
774
+ }
775
+ /**
776
+ * Blocks requests by specifying URL, domain, or even a simple pattern.
777
+ */
778
+ blockRequests(...blockRequests) {
779
+ this.put("block_requests", ...blockRequests);
780
+ return this;
781
+ }
782
+ /**
783
+ * Blocks loading resources by type. Available resource types are: "document", "stylesheet", "image", "media",
784
+ * "font", "script", "texttrack", "xhr", "fetch", "eventsource", "websocket", "manifest", "other".
785
+ */
786
+ blockResources(...blockResources) {
787
+ this.put("block_resources", ...blockResources);
788
+ return this;
789
+ }
790
+ /**
791
+ * Enables caching.
792
+ */
793
+ cache(cache) {
794
+ this.put("cache", cache ? "true" : "false");
795
+ return this;
796
+ }
797
+ /**
798
+ * Sets cache TTL.
799
+ */
800
+ cacheTtl(cacheTTL) {
801
+ this.put("cache_ttl", cacheTTL.toString());
802
+ return this;
803
+ }
804
+ /**
805
+ * Sets cache key.
806
+ */
807
+ cacheKey(cacheKey) {
808
+ this.put("cache_key", cacheKey);
809
+ return this;
810
+ }
811
+ /**
812
+ * Sets a user agent for the request.
813
+ */
814
+ userAgent(userAgent) {
815
+ this.put("user_agent", userAgent);
816
+ return this;
817
+ }
818
+ /**
819
+ * Sets an authorization header for the request.
820
+ */
821
+ authorization(authorization) {
822
+ this.put("authorization", authorization);
823
+ return this;
824
+ }
825
+ /**
826
+ * Set cookies for the request.
827
+ */
828
+ cookies(...cookies) {
829
+ this.put("cookies", ...cookies);
830
+ return this;
831
+ }
832
+ /**
833
+ * Sets extra headers for the request.
834
+ */
835
+ headers(...headers) {
836
+ this.put("headers", ...headers);
837
+ return this;
838
+ }
839
+ /**
840
+ * TimeZone sets time zone for the request.
841
+ * Available time zones are: "America/Santiago", "Asia/Shanghai", "Europe/Berlin", "America/Guayaquil",
842
+ * "Europe/Madrid", "Pacific/Majuro", "Asia/Kuala_Lumpur", "Pacific/Auckland", "Europe/Lisbon", "Europe/Kiev",
843
+ * "Asia/Tashkent", "Europe/London".
844
+ */
845
+ timeZone(timeZone) {
846
+ this.put("time_zone", timeZone);
847
+ return this;
848
+ }
849
+ /**
850
+ * Sets delay.
851
+ */
852
+ delay(delay) {
853
+ this.put("delay", delay.toString());
854
+ return this;
855
+ }
856
+ /**
857
+ * Sets timeout.
858
+ */
859
+ timeout(timeout) {
860
+ this.put("timeout", timeout.toString());
861
+ return this;
862
+ }
863
+ /**
864
+ * Default value is false. Use store=true to trigger upload of the taken screenshot,
865
+ * rendered HTML or PDF to the configured S3 bucket.
866
+ * Make sure you configured access to S3.
867
+ */
868
+ store(store) {
869
+ this.put("store", store ? "true" : "false");
870
+ return this;
871
+ }
872
+ /**
873
+ * The parameter is required if you set store=true.
874
+ * You must specify the key for the file, but don’t specify an extension,
875
+ * it will be added automatically based on the format you specified.
876
+ * You can also specify “subdirectories” in the path part.
877
+ */
878
+ storagePath(path) {
879
+ this.put("storage_path", path);
880
+ return this;
881
+ }
882
+ /**
883
+ * You can override the default bucket you configured with storage_bucket=<bucket name>.
884
+ */
885
+ storageBucket(bucket) {
886
+ this.put("storage_bucket", bucket);
887
+ return this;
888
+ }
889
+ /**
890
+ * Storage class allows you to specify the object storage class.
891
+ */
892
+ storageClass(storageClass) {
893
+ this.put("storage_class", storageClass);
894
+ return this;
895
+ }
896
+ /**
897
+ * Sets animation duration.
898
+ */
899
+ duration(duration) {
900
+ this.put("duration", duration.toString());
901
+ return this;
902
+ }
903
+ /**
904
+ * Sets animation duration.
905
+ */
906
+ scenario(scenario) {
907
+ this.put("scenario", scenario);
908
+ return this;
909
+ }
910
+ scrollDelay(delay) {
911
+ this.put("scroll_delay", delay.toString());
912
+ return this;
913
+ }
914
+ scrollDuration(duration) {
915
+ this.put("scroll_duration", duration.toString());
916
+ return this;
917
+ }
918
+ scrollStartImmediately(startImmediately) {
919
+ this.put("scroll_start_immediately", startImmediately ? "true" : "false");
920
+ return this;
921
+ }
922
+ toQuery() {
923
+ return new URLSearchParams(this.query.toString());
924
+ }
925
+ }
926
+ exports.AnimateOptions = AnimateOptions;
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.25",
4
+ "version": "1.0.27",
5
5
  "description": "Use ScreenshotOne.com API to generate screenshots of any website.",
6
6
  "repository": {
7
7
  "type": "git",