放置網站屏幕截圖等的最佳方法 into您的Node.js應用程序通過執行以下步驟:
這個圖書館是 僅服務器端。 如果您想使用客戶端JavaScript處理捕獲,則應使用我們的 客戶端JavaScript API 代替。
要使用API,您首先需要創建 客戶 對象,傳遞您的 應用程序密鑰和應用程序秘密 從您的GrabzIt帳戶到構造函數。
var grabzit = require('grabzit'); var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");
然後使用 圖片, PDF, 動畫 or 表 捕獲方法 網址 or 轉換HTML.
// To take a image screenshot client.url_to_image("https://www.tesla.com"); // Or to take a PDF screenshot client.url_to_pdf("https://www.tesla.com"); // Or to convert online videos into animated GIF's client.url_to_animation("http://www.example.com/video.avi"); // Or to capture table(s) client.url_to_table("https://www.tesla.com");
// To convert HTML into a image client.html_to_image("<html><body><h1>Hello World!</h1></body></html>"); // Or convert HTML into a PDF document client.html_to_pdf("<html><body><h1>Hello World!</h1></body></html>"); // Or convert HTML into a CSV, XLSX or JSON document client.html_to_table("<html><body><table><tr><th>Name</th><th>Age</th></tr> <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr> </table></body></html>");
// To convert HTML file into a image client.file_to_image("example.html"); // Or convert HTML file into a PDF document client.file_to_pdf("example.html"); // Or convert HTML file into a CSV, XLSX or JSON document client.file_to_table("tables.html");
接下來必須是捕獲 saved,為此,您必須使用 save
or save_to
方法,兩者都傳入一個函數來處理已發生的任何錯誤。 的 save 下面的方法調用包含 處理器 這將處理 回電話 來自GrabzIt和 save 屏幕截圖或截圖。
client.save("http://www.example.com/handler", function (error, id){ if (error != null){ throw error; } });
另一個選擇是使用 同步的 save_to 方法,這將迫使您的應用程序在屏幕截圖創建時等待,因此僅在 save 方法無法使用。
截圖一旦創建,它將自動 saved到提供的文件路徑。 請注意,Node.js庫僅在服務器端可用,因為它需要服務器功能才能正常工作。
client.save_to("images/result.jpg", function (error, id){ //this callback is called once the capture is downloaded if (error != null){ throw error; } });
這只是GrabzIt的Node.js API的概述,在創建屏幕快照,動畫GIF或轉換HTML時,還有更多選項,包括: 配置瀏覽器高度,瀏覽器寬度,屏幕截圖高度,屏幕截圖寬度,拍攝屏幕截圖之前的延遲以及屏幕截圖的圖像格式。 對於所有這些選項以及轉換網絡的能力 into PDF,CSV或動畫GIF,請參閱 客戶 文檔。