UNPKG

screenshotone-api-sdk

Version:

Use ScreenshotOne.com API to generate screenshots of any website.

823 lines (822 loc) 30.7 kB
import APIError from "./errors"; export { APIError }; /** * Represents an API client for the ScreenshotOne.com API. */ export declare class Client { private readonly accessKey; private readonly secretKey; private readonly apiBaseUrl; constructor(accessKey: string, secretKey: string, apiBaseUrl?: string); /** * Generates an URL that can be used to send GET requests for taking screenshots. * * Warning! Don't share the link publicly generated by this method publicly. * * @param options Options for taking the screenshot. * @returns An URL that will generate the screenshot if requested. */ generateTakeURL(options: TakeOptions): Promise<string>; /** * Generates the link that can be publicly available, but make sure * you read the documentation before at https://screenshotone.com/docs/signed-requests/. * * @param options * @returns A signed URL that will generate the screenshot if requested. */ generateSignedTakeURL(options: TakeOptions): Promise<string>; /** * Generates the link that can be publicly available, but make sure * you read the documentation before at https://screenshotone.com/docs/signed-requests/. * * @param options * @returns A signed URL that will generate the animated screenshot if requested. */ generateSignedAnimateURL(options: AnimateOptions): Promise<string>; /** * Generates a screenshot and return the image blob. * * @param options * @returns */ take(options: TakeOptions): Promise<Blob>; /** * Generates an animated screenshot and return the image blob. * * @param options * @returns */ animate(options: AnimateOptions): Promise<Blob>; /** * Generates an animated screenshot and return the image blob. * * @param options * @returns */ store(options: AnimateOptions | TakeOptions, path: string, bucket?: string, acl?: "public-read" | "", storageClass?: string, accessKeyId?: string, secretAccessKey?: string): Promise<{ bucket: string | null; key: string | null; }>; } /** * Represents options for taking screenshots. */ export declare class TakeOptions { private readonly query; private constructor(); /** * Initializes take screenshot options with provided website URL. */ static url(url: string): TakeOptions; /** * Initializes take screenshot options with provided HTML. */ static html(html: string): TakeOptions; /** * Initializes take screenshot options with provided Markdown. */ static markdown(markdown: string): TakeOptions; private put; /** * Selector is a CSS-like selector of the element to take a screenshot of. */ selector(selector: string): TakeOptions; /** * Available response types: * 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; * 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. * json — A method returns the response in the JSON format, but it is only suitable if you use options that are effective for JSON. By default, the JSON response will be empty. But for example, when you use caching, the JSON will be populated with additional data. * The default value is by_format. */ responseType(responseType: string): TakeOptions; /** * It determines the behavior of what to do when selector is not found. */ errorOnSelectorNotFound(errorOn: boolean): TakeOptions; /** * Capture beyond the viewport. */ captureBeyondViewport(beyond: boolean): TakeOptions; /** * When reduced_motion set to `true`, * the API will request the site to minimize the amount of non-essential motion it uses. * When the site supports it, it should use animations as least as possible. */ reducedMotion(reducedMotion: boolean): TakeOptions; /** * If you want to request the page and it is supported to be rendered for printers, specify `print`. * If the page is by default rendered for printers and you want it to be rendered for screens, * use `screen`. */ mediaType(mediaType: string): TakeOptions; /** * Set `true` to request site rendering, if supported, in the dark mode. * Set `false` to request site rendering in the light mode if supported. * If you don't set the parameter. The site is rendered in the default mode. */ darkMode(darkMode: boolean): TakeOptions; /** * The `hide_selectors` option allows hiding elements before taking a screenshot. * You can specify as many selectors as you wish. * All elements that match each selector will be hidden by * setting the `display` style property to none `!important`. */ hideSelectors(...selectors: string[]): TakeOptions; /** * The scripts_wait_until option allows you to wait until a given * set of events after the scripts were executed. * It accepts the same values as wait_until and can have multiple values: * - `load`: the navigation is successful when the load even is fired; * - `domcontentloaded`: the navigation is finished when the DOMContentLoaded even is fired; * - `networkidle0`: the navigation is finished when there are no more than 0 network connections for at least 500 ms; * _ `networkidle2`: consider navigation to be finished when there are no more than 2 network connections for at least 500 ms. */ scriptsWaitUntil(...until: string[]): TakeOptions; /** * Styles specifies custom CSS styles for the page. */ styles(styles: string): TakeOptions; /** * Scripts specifies custom scripts for the page. */ scripts(scripts: string): TakeOptions; /** * Renders the full page. */ fullPage(fullPage: boolean): TakeOptions; /** * Max height. */ fullPageMaxHeight(maxHeight: number): TakeOptions; /** * Sets the algorithm to use for full page screenshot—"by_sections" or ""/"default". */ fullPageAlgorithm(algorithm: string): TakeOptions; /** * Set `true` to scroll the page to the bottom. */ fullPageScroll(scroll: boolean): TakeOptions; /** * Sets the delay between scroll steps. */ fullPageScrollDelay(delay: number): TakeOptions; /** * Sets response format, one of: "png", "jpeg", "webp", "jpg", "gif", "jp2", "tiff", "avif", "heif", "pdf", "html". */ format(format: string): TakeOptions; /** * When the site responds within the range of 200-299 status code, * you can ignore errors and take a screenshot of the error page anyway. * To do that, set the option ignore_host_errors to true. * It is false by default. * * It is helpful when you want to create a gallery of error pages or, * for some reason, you need to render error pages. */ ignoreHostErrors(ignore: boolean): TakeOptions; /** * Use wait_until to wait until an event occurred before taking a screenshot or rendering HTML or PDF. * * The default value of wait_until is ['load']. Allowed values are: * - load: the navigation is successful when the load even is fired; * - domcontentloaded: the navigation is finished when the DOMContentLoaded even is fired; * - networkidle0: the navigation is finished when there are no more than 0 network connections for at least 500 ms; * - networkidle2: consider navigation to be finished when there are no more than 2 network connections for at least 500 ms. * * The parameter accepts many values. It means that screenshots will be taken after all events occur altogether. */ waitUntil(...until: string[]): TakeOptions; /** * Sets IP country code. */ ipCountryCode(countryCode: string): TakeOptions; /** * Renders image with the specified quality. Available for the next formats: "jpeg" ("jpg"), "webp". */ imageQuality(imageQuality: number): TakeOptions; /** * Renders a transparent background for the image. Works only if the site has not defined background color. * Available for the following response formats: "png", "webp". */ omitBackground(omitBackground: boolean): TakeOptions; /** * Instead of manually specifying viewport parameters like width and height, * you can specify a device to use for emulation. * In addition, other parameters of the viewport, including the user agent, * will be set automatically. */ viewportDevice(viewportDevice: string): TakeOptions; /** * Sets the width of the browser viewport (pixels). */ viewportWidth(viewportWidth: number): TakeOptions; /** * Sets the height of the browser viewport (pixels). */ viewportHeight(viewportHeight: number): TakeOptions; /** * The clip_x option specifies only the top coordinate (x) of the area to clip. */ clipX(clipX: number): TakeOptions; /** * The clip_y option specifies only the top coordinate (y) of the area to clip. */ clipY(clipY: number): TakeOptions; /** * The clip_width option specifies only the width of the area to clip. */ clipWidth(clipWidth: number): TakeOptions; /** * The clip_height option specifies only the width of the area to clip. */ clipHeight(clipHeight: number): TakeOptions; /** * Sets the device scale factor. Acceptable value is one of: 1, 2 or 3. */ deviceScaleFactor(deviceScaleFactor: number): TakeOptions; /** * Whether the meta viewport tag is taken into account. Defaults to `false`. */ viewportMobile(isMobile: boolean): TakeOptions; /** * The default value is `false`. Set to `true` if the viewport supports touch events. */ viewportHasTouch(hasTouch: boolean): TakeOptions; /** * The default value is `false`. Set to `true` if the viewport is in landscape mode. */ viewportLandscape(landscape: boolean): TakeOptions; /** * Sets geolocation latitude for the request. * Both latitude and longitude are required if one of them is set. */ geolocationLatitude(latitude: number): TakeOptions; /** * Sets geolocation longitude for the request. Both latitude and longitude are required if one of them is set. */ geolocationLongitude(longitude: number): TakeOptions; /** * Sets the geolocation accuracy in meters. */ geolocationAccuracy(accuracy: number): TakeOptions; /** * Blocks ads. */ blockAds(blockAds: boolean): TakeOptions; /** * Blocks banners by heuristics. */ blockBannersByHeuristics(block: boolean): TakeOptions; /** * Blocks cookie banners. */ blockCookieBanners(block: boolean): TakeOptions; /** * Blocks chats. */ blockChats(block: boolean): TakeOptions; /** * Blocks trackers. */ blockTrackers(blockTrackers: boolean): TakeOptions; /** * Blocks requests by specifying URL, domain, or even a simple pattern. */ blockRequests(...blockRequests: string[]): TakeOptions; /** * Blocks loading resources by type. Available resource types are: "document", "stylesheet", "image", "media", * "font", "script", "texttrack", "xhr", "fetch", "eventsource", "websocket", "manifest", "other". */ blockResources(...blockResources: string[]): TakeOptions; /** * Enables caching. */ cache(cache: boolean): TakeOptions; /** * Sets cache TTL. */ cacheTtl(cacheTTL: number): TakeOptions; /** * Sets cache key. */ cacheKey(cacheKey: string): TakeOptions; /** * Sets a user agent for the request. */ userAgent(userAgent: string): TakeOptions; /** * Sets an authorization header for the request. */ authorization(authorization: string): TakeOptions; /** * It scrolls the page if needed and ensures that the given * selector is present in the view when taking a screenshot. */ scrollIntoView(selector: string): TakeOptions; /** * After the given element appears in the viewport and its top coordinate is aligned with the viewport's top, * you can adjust the position a bit before taking a screenshot. */ scrollIntoViewAdjustTop(adjustTop: number): TakeOptions; /** * You can use your custom proxy to take screenshots * or render HTML with the proxy option. * * The https, http, socks4 and socks5 proxies are supported. */ proxy(proxy: string): TakeOptions; /** * Set cookies for the request. */ cookies(...cookies: string[]): TakeOptions; /** * Sets extra headers for the request. */ headers(...headers: string[]): this; /** * TimeZone sets time zone for the request. * Available time zones are: "America/Santiago", "Asia/Shanghai", "Europe/Berlin", "America/Guayaquil", * "Europe/Madrid", "Pacific/Majuro", "Asia/Kuala_Lumpur", "Pacific/Auckland", "Europe/Lisbon", "Europe/Kiev", * "Asia/Tashkent", "Europe/London". */ timeZone(timeZone: string): TakeOptions; /** * Sets delay. */ delay(delay: number): TakeOptions; /** * Sets timeout. */ timeout(timeout: number): TakeOptions; /** * Sets navigation timeout. */ navigationTimeout(timeout: number): TakeOptions; /** * Default value is false. Use store=true to trigger upload of the taken screenshot, * rendered HTML or PDF to the configured S3 bucket. * Make sure you configured access to S3. */ store(store: boolean): TakeOptions; /** * The parameter is required if you set store=true. * You must specify the key for the file, but don't specify an extension, * it will be added automatically based on the format you specified. * You can also specify "subdirectories" in the path part. */ storagePath(path: string): TakeOptions; storageACL(acl: string): TakeOptions; /** * You can override the default bucket you configured with storage_bucket=<bucket name>. */ storageBucket(bucket: string): TakeOptions; /** * Access key ID. It overrides the one specified in the dashboard configuration. */ storageAccessKeyId(accessKeyId: string): TakeOptions; /** * Secret access key. It overrides the one specified in the dashboard configuration. */ storageSecretAccessKey(secretAccessKey: string): TakeOptions; /** * Leave empty for Amazon S3, specify only when needed. Any S3-compatible storage is supported, * e.g. "https://<accountId>.r2.cloudflarestorage.com" for Cloudlfare R2 storage. */ storageEndpoint(endpoint: string): TakeOptions; /** * Storage class allows you to specify the object storage class. */ storageClass(storageClass: string): TakeOptions; /** * Sets the image width for thumbnail creation. */ imageWidth(width: number): TakeOptions; /** * Sets the image height for thumbnail creation. */ imageHeight(height: number): TakeOptions; /** * Sets whether to request GPU rendering. */ requestGpuRendering(request: boolean): TakeOptions; /** * Sets whether to print background for PDF. */ pdfPrintBackground(print: boolean): TakeOptions; /** * Sets whether to fit PDF to one page. */ pdfFitOnePage(fit: boolean): TakeOptions; /** * Sets the OpenAI API key for vision integration. */ openAiApiKey(key: string): TakeOptions; /** * Sets the vision prompt for OpenAI integration. */ visionPrompt(prompt: string): TakeOptions; /** * Sets the maximum tokens for OpenAI vision response. */ visionMaxTokens(tokens: number): TakeOptions; /** * Sets whether to bypass Content Security Policy. */ bypassCsp(bypass: boolean): TakeOptions; /** * Sets whether to wait for a specific selector. */ waitForSelector(selector: string): TakeOptions; /** * Sets whether to return the storage location. */ storageReturnLocation(return_location: boolean): TakeOptions; /** * Sets whether to include image size metadata. */ metadataImageSize(include: boolean): TakeOptions; /** * Sets whether to include fonts metadata. */ metadataFonts(include: boolean): TakeOptions; /** * Sets whether to include Open Graph metadata. */ metadataOpenGraph(include: boolean): TakeOptions; /** * Sets whether to include page title metadata. */ metadataPageTitle(include: boolean): TakeOptions; /** * Sets whether to include content metadata. */ metadataContent(include: boolean): TakeOptions; /** * Sets whether to include HTTP response status code metadata. */ metadataHttpResponseStatusCode(include: boolean): TakeOptions; /** * Sets whether to include HTTP response headers metadata. */ metadataHttpResponseHeaders(include: boolean): TakeOptions; /** * Sets whether to execute the request asynchronously. */ async(isAsync: boolean): TakeOptions; /** * Sets the webhook URL for asynchronous requests. */ webhookUrl(url: string): TakeOptions; /** * Sets whether to sign the webhook request. */ webhookSign(sign: boolean): TakeOptions; /** * Sets the text to check for in the content to force request failure. */ failIfContentContains(text: string): TakeOptions; /** * Sets whether to fail if GPU rendering fails. */ failIfGpuRenderingFails(fail: boolean): TakeOptions; toQuery(): URLSearchParams; } /** * Represents options for animating screenshots. */ export declare class AnimateOptions { private readonly query; private constructor(); /** * Initializes take screenshot options with provided website URL. */ static url(url: string): AnimateOptions; /** * Initializes take screenshot options with provided HTML. */ static html(html: string): AnimateOptions; /** * Initializes take screenshot options with provided Markdown. */ static markdown(markdown: string): AnimateOptions; private put; /** * Available response types: * 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; * 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. * The default value is by_format. */ responseType(responseType: string): this; /** * When reduced_motion set to `true`, * the API will request the site to minimize the amount of non-essential motion it uses. * When the site supports it, it should use animations as least as possible. */ reducedMotion(darkMode: boolean): AnimateOptions; /** * If you want to request the page and it is supported to be rendered for printers, specify `print`. * If the page is by default rendered for printers and you want it to be rendered for screens, * use `screen`. */ mediaType(mediaType: string): AnimateOptions; /** * Set `true` to request site rendering, if supported, in the dark mode. * Set `false` to request site rendering in the light mode if supported. * If you don't set the parameter. The site is rendered in the default mode. */ darkMode(darkMode: boolean): AnimateOptions; /** * The `hide_selectors` option allows hiding elements before taking a screenshot. * You can specify as many selectors as you wish. * All elements that match each selector will be hidden by * setting the `display` style property to none `!important`. */ hideSelectors(...selectors: string[]): AnimateOptions; /** * The scripts_wain_until option allows you to wait until a given * set of events after the scripts were executed. * It accepts the same values as wait_until and can have multiple values: * - `load`: the navigation is successful when the load even is fired; * - `domcontentloaded`: the navigation is finished when the DOMContentLoaded even is fired; * - `networkidle0`: the navigation is finished when there are no more than 0 network connections for at least 500 ms; * _ `networkidle2`: consider navigation to be finished when there are no more than 2 network connections for at least 500 ms. */ scriptsWaitUntil(...until: string[]): AnimateOptions; /** * Styles specifies custom CSS styles for the page. */ styles(styles: string): this; /** * You can use your custom proxy to take screenshots * or render HTML with the proxy option. * * The https, http, socks4 and socks5 proxies are supported. */ proxy(proxy: string): AnimateOptions; /** * Scripts specifies custom scripts for the page. */ scripts(scripts: string): this; /** * Sets response format, one of: "mp4", "avi", "webm", "mov" or "gif". */ format(format: string): AnimateOptions; /** * Renders a transparent background for the image. Works only if the site has not defined background color. * Available for the following response formats: "png", "webp". */ omitBackground(omitBackground: boolean): AnimateOptions; /** * Instead of manually specifying viewport parameters like width and height, * you can specify a device to use for emulation. * In addition, other parameters of the viewport, including the user agent, * will be set automatically. */ viewportDevice(viewportDevice: string): AnimateOptions; /** * Sets the width of the browser viewport (pixels). */ viewportWidth(viewportWidth: number): AnimateOptions; /** * Sets the height of the browser viewport (pixels). */ viewportHeight(viewportHeight: number): AnimateOptions; /** * Sets the device scale factor. Acceptable value is one of: 1, 2 or 3. */ deviceScaleFactor(deviceScaleFactor: number): AnimateOptions; /** * Whether the meta viewport tag is taken into account. Defaults to `false`. */ viewportMobile(isMobile: boolean): AnimateOptions; /** * The default value is `false`. Set to `true` if the viewport supports touch events. */ viewportHasTouch(hasTouch: boolean): AnimateOptions; /** * The default value is `false`. Set to `true` if the viewport is in landscape mode. */ viewportLandscape(landscape: boolean): AnimateOptions; /** * Sets geolocation latitude for the request. * Both latitude and longitude are required if one of them is set. */ geolocationLatitude(latitude: number): AnimateOptions; /** * Sets geolocation longitude for the request. Both latitude and longitude are required if one of them is set. */ geolocationLongitude(longitude: number): AnimateOptions; /** * Sets the geolocation accuracy in meters. */ geolocationAccuracy(accuracy: number): AnimateOptions; /** * Blocks ads. */ blockAds(blockAds: boolean): AnimateOptions; /** * Blocks cookie banners. */ blockCookieBanners(block: boolean): AnimateOptions; /** * Blocks chats. */ blockChats(block: boolean): AnimateOptions; /** * Blocks trackers. */ blockTrackers(blockTrackers: boolean): AnimateOptions; /** * Blocks requests by specifying URL, domain, or even a simple pattern. */ blockRequests(...blockRequests: string[]): AnimateOptions; /** * Blocks loading resources by type. Available resource types are: "document", "stylesheet", "image", "media", * "font", "script", "texttrack", "xhr", "fetch", "eventsource", "websocket", "manifest", "other". */ blockResources(...blockResources: string[]): AnimateOptions; /** * Blocks banners by heuristics. */ blockBannersByHeuristics(block: boolean): AnimateOptions; /** * Enables caching. */ cache(cache: boolean): AnimateOptions; /** * Sets cache TTL. */ cacheTtl(cacheTTL: number): AnimateOptions; /** * Sets cache key. */ cacheKey(cacheKey: string): AnimateOptions; /** * Sets a user agent for the request. */ userAgent(userAgent: string): AnimateOptions; /** * Sets an authorization header for the request. */ authorization(authorization: string): AnimateOptions; /** * Set cookies for the request. */ cookies(...cookies: string[]): AnimateOptions; /** * Sets extra headers for the request. */ headers(...headers: string[]): this; /** * TimeZone sets time zone for the request. * Available time zones are: "America/Santiago", "Asia/Shanghai", "Europe/Berlin", "America/Guayaquil", * "Europe/Madrid", "Pacific/Majuro", "Asia/Kuala_Lumpur", "Pacific/Auckland", "Europe/Lisbon", "Europe/Kiev", * "Asia/Tashkent", "Europe/London". */ timeZone(timeZone: string): AnimateOptions; /** * Sets delay. */ delay(delay: number): AnimateOptions; /** * Sets timeout. */ timeout(timeout: number): AnimateOptions; /** * Sets navigation timeout. */ navigationTimeout(timeout: number): AnimateOptions; /** * Default value is false. Use store=true to trigger upload of the taken screenshot, * rendered HTML or PDF to the configured S3 bucket. * Make sure you configured access to S3. */ store(store: boolean): AnimateOptions; /** * Access key ID. It overrides the one specified in the dashboard configuration. */ storageAccessKeyId(accessKeyId: string): AnimateOptions; /** * Secret access key. It overrides the one specified in the dashboard configuration. */ storageSecretAccessKey(secretAccessKey: string): AnimateOptions; /** * The parameter is required if you set store=true. * You must specify the key for the file, but don't specify an extension, * it will be added automatically based on the format you specified. * You can also specify "subdirectories" in the path part. */ storagePath(path: string): AnimateOptions; /** * You can override the default bucket you configured with storage_bucket=<bucket name>. */ storageBucket(bucket: string): AnimateOptions; /** * Storage class allows you to specify the object storage class. */ storageClass(storageClass: string): AnimateOptions; storageACL(acl: string): AnimateOptions; /** * Sets animation duration. */ duration(duration: number): AnimateOptions; /** * Sets animation duration. */ scenario(scenario: string): AnimateOptions; scrollDelay(delay: number): AnimateOptions; scrollDuration(duration: number): AnimateOptions; scrollStartImmediately(startImmediately: boolean): AnimateOptions; scrollBack(scrollBack: boolean): AnimateOptions; scrollBackAfterDuration(duration: number): AnimateOptions; scrollStartDelay(delay: number): AnimateOptions; scrollComplete(complete: boolean): AnimateOptions; scrollBy(pixels: number): AnimateOptions; /** * When the site responds out of the range of 200-299 status code, * you can ignore errors and take a screenshot of the error page anyway. * To do that, set the option "ignore_host_errors" to true. * It is false by default. * * It is helpful when you want to create a gallery of error pages or, * for some reason, you need to render error pages. */ ignoreHostErrors(ignore: boolean): AnimateOptions; /** * Sets whether to request GPU rendering. */ requestGpuRendering(request: boolean): AnimateOptions; /** * Sets whether to bypass Content Security Policy. */ bypassCsp(bypass: boolean): AnimateOptions; /** * Sets whether to wait for a specific selector. */ waitForSelector(selector: string): AnimateOptions; /** * Sets whether to return the storage location. */ storageReturnLocation(return_location: boolean): AnimateOptions; /** * Sets whether to execute the request asynchronously. */ async(isAsync: boolean): AnimateOptions; /** * Sets the webhook URL for asynchronous requests. */ webhookUrl(url: string): AnimateOptions; /** * Sets whether to sign the webhook request. */ webhookSign(sign: boolean): AnimateOptions; /** * Sets the text to check for in the content to force request failure. */ failIfContentContains(text: string): AnimateOptions; /** * Sets whether to fail if GPU rendering fails. */ failIfGpuRenderingFails(fail: boolean): AnimateOptions; /** * Sets the width of the animation. */ width(width: number): AnimateOptions; /** * Sets the height of the animation. */ height(height: number): AnimateOptions; /** * Sets the aspect ratio of the animation. */ aspectRatio(ratio: string): AnimateOptions; /** * Sets the scroll easing effect. */ scrollEasing(easing: string): AnimateOptions; /** * Sets whether to navigate while scrolling and record the new opened page. */ scrollTryNavigate(tryNavigate: boolean): AnimateOptions; /** * Sets when to navigate after scrolling (in milliseconds). */ scrollNavigateAfter(duration: number): AnimateOptions; /** * Sets the URL to navigate to while scrolling. */ scrollNavigateToURL(url: string): AnimateOptions; /** * Sets the link hints for navigation while scrolling. */ scrollNavigateLinkHints(...hints: string[]): AnimateOptions; /** * Sets the scroll back algorithm. */ scrollBackAlgorithm(algorithm: string): AnimateOptions; /** * Sets when to stop scrolling after the specified duration in milliseconds. */ scrollStopAfterDuration(duration: number): AnimateOptions; toQuery(): URLSearchParams; }