使用javascript捕获屏幕截图

时间:2014-03-07 03:11:12

标签: javascript html screenshot

我有一个javascript代码来捕获给定网址的截图。我也发现了它的演示..但我不知道如何将其添加到我的网站。有人可以帮我吗

http://ctrlq.org/code/19136-screenshots-javascript

var system = require('system');

// Web Address (URL) of the page to capture
var url  = system.args[1];

// File name of the captured image
var file = system.args[2]  + ".png";

var page = require('webpage').create();

// Browser size - height and width in pixels
// Change the viewport to 480x320 to emulate the iPhone
page.viewportSize = { width: 1200, height : 800 };

// Set the User Agent String 
// You can change it to iPad or Android for mobile screenshots
page.settings.userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5";

// Render the screenshot image
page.open ( url, function ( status ) {
  if ( status !== "success") {
       console.log("Could not open web page : " + url);
       phantom.exit();
   } else {
       window.setTimeout ( function() {
          page.render(file);
          console.log("Download the screenshot : " + file);
          phantom.exit();
       }, 1000);
   }
});`

2 个答案:

答案 0 :(得分:0)

这不是标准的Javascript,它是Phantomjs,无头Webkit可编程的Javascript。您无法将其放在页面上,但是您可以在服务器上运行它,并通过各种API为您的用户提供服务。

如果您想在客户端截取您的网页,请使用Html2canvas

答案 1 :(得分:0)

您还可以查看此question & answer,它是关于如何使用HTML5画布截取网页的截图。