用JavaScript拍摄网页截图?
是否可以使用JavaScript截取网页的屏幕截图,然后将其提交回服务器用JavaScript拍摄网页截图?,javascript,hta,Javascript,Hta,是否可以使用JavaScript截取网页的屏幕截图,然后将其提交回服务器 我不太关心浏览器的安全问题。等等,因为实施将是为了。但是有可能吗?您可以使用HTA和。只需调用一个外部工具来进行屏幕截图。我忘了它的名字了,但是在WindowsVista上有一个截图工具。你甚至不需要额外的安装 至于自动-这完全取决于你使用的工具。如果它有一个API,我相信你可以通过几个Visual Basic调用触发屏幕截图和保存过程,而用户不知道你做了什么 由于您提到HTA,我假设您在Windows上,并且(可能)非常
我不太关心浏览器的安全问题。等等,因为实施将是为了。但是有可能吗?您可以使用HTA和。只需调用一个外部工具来进行屏幕截图。我忘了它的名字了,但是在WindowsVista上有一个截图工具。你甚至不需要额外的安装 至于自动-这完全取决于你使用的工具。如果它有一个API,我相信你可以通过几个Visual Basic调用触发屏幕截图和保存过程,而用户不知道你做了什么
由于您提到HTA,我假设您在Windows上,并且(可能)非常了解您的环境(例如操作系统和版本) 我们对报告bug也有类似的要求。因为它是用于内部网场景,所以我们可以使用浏览器插件(如Firefox和InternetExplorer)。我使用ActiveX控件为HTA实现了这一点。在VB6中构建控件以获取屏幕截图非常容易。我不得不使用keybd_事件API调用,因为SendKeys不能执行PrintScreen。下面是代码:
Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Public Const CaptWindow = 2
Public Sub ScreenGrab()
keybd_event &H12, 0, 0, 0
keybd_event &H2C, CaptWindow, 0, 0
keybd_event &H2C, CaptWindow, &H2, 0
keybd_event &H12, 0, &H2, 0
End Sub
这只会让你把窗口放到剪贴板上
另一种选择是,如果您想要屏幕截图的窗口是HTA,则只需使用XMLHTTPRequest将DOM节点发送到服务器,然后在服务器端创建屏幕截图。这可能不是理想的解决方案,但仍然值得一提 是一个开源的对象,可以捕获和保存Internet Explorer屏幕截图。在客户端上注册DLL文件后,您应该能够捕获屏幕截图并使用JavaScript将文件上载到服务器。缺点:它需要在客户端注册DLL文件,并且只能与InternetExplorer一起使用。使用(1.5+)制作浏览器屏幕截图。好的,
java.awt.Robot
应该做这项工作-用户只需允许小程序做(一次)
我刚刚找到一篇关于它的帖子:
- 堆栈溢出问题
- 博文
一种可能的方法,如果在windows上运行并安装了.NET,您可以执行以下操作:
public Bitmap GenerateScreenshot(string url)
{
// This method gets a screenshot of the webpage
// rendered at its full size (height and width)
return GenerateScreenshot(url, -1, -1);
}
public Bitmap GenerateScreenshot(string url, int width, int height)
{
// Load the webpage into a WebBrowser control
WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
// Set the size of the WebBrowser control
wb.Width = width;
wb.Height = height;
if (width == -1)
{
// Take Screenshot of the web pages full width
wb.Width = wb.Document.Body.ScrollRectangle.Width;
}
if (height == -1)
{
// Take Screenshot of the web pages full height
wb.Height = wb.Document.Body.ScrollRectangle.Height;
}
// Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
wb.Dispose();
return bitmap;
}
然后通过PHP,您可以执行以下操作:
public Bitmap GenerateScreenshot(string url)
{
// This method gets a screenshot of the webpage
// rendered at its full size (height and width)
return GenerateScreenshot(url, -1, -1);
}
public Bitmap GenerateScreenshot(string url, int width, int height)
{
// Load the webpage into a WebBrowser control
WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
// Set the size of the WebBrowser control
wb.Width = width;
wb.Height = height;
if (width == -1)
{
// Take Screenshot of the web pages full width
wb.Width = wb.Document.Body.ScrollRectangle.Width;
}
if (height == -1)
{
// Take Screenshot of the web pages full height
wb.Height = wb.Document.Body.ScrollRectangle.Height;
}
// Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
wb.Dispose();
return bitmap;
}
exec(“CreateScreenShot.exe-urlhttp://.... -保存C:/shots domain_page.png”)代码>
然后,您可以在服务器端看到屏幕截图。谷歌正在Google+中进行此项工作,一位有才华的开发人员对其进行了反向工程并制作了此产品。要在IE中工作,您将需要一个画布支持库,如我发现的另一个可能的解决方案是,它允许您非常轻松地拍摄页面截图和更多内容。虽然我对这个问题的最初要求不再有效(不同的工作),但我可能会将PhantomJS集成到未来的项目中。Javascript截图的一个很好的解决方案是one by
他们有一个灵活且易于使用的屏幕截图API,可以被任何类型的JS应用程序使用
如果你想尝试,首先你应该得到授权和
然后,在您的应用程序中,实施步骤将是:
// include the grabzit.min.js library in the web page you want the capture to appear
<script src="grabzit.min.js"></script>
//use the key and the secret to login, capture the url
<script>
GrabzIt("KEY", "SECRET").ConvertURL("http://www.google.com").Create();
</script>
//将grabzit.min.js库包含在希望捕获显示的网页中
//使用密钥和密码登录,捕获url
GrabzIt(“KEY”、“SECRET”).ConvertURL(“http://www.google.com)创建();
屏幕截图可以定制不同的功能。例如:
GrabzIt("KEY", "SECRET").ConvertURL("http://www.google.com",
{"width": 400, "height": 400, "format": "png", "delay", 10000}).Create();
</script>
GrabzIt(“KEY”、“SECRET”).ConvertURL(“http://www.google.com",
{“宽度”:400,“高度”:400,“格式”:“png”,“延迟”,10000});
就这些。
然后只需稍等片刻,图像将自动显示在页面底部,而无需重新加载页面
屏幕截图机制还有其他功能,您可以进行探索
也可以在本地保存屏幕截图。为此,您需要利用GrabzIt服务器端API。有关更多信息,请查看详细指南。我发现,dom to image做得很好(比html2canvas好得多)。请参阅以下问题和答案:
此问题询问如何将此文件提交回服务器,这应该是可能的,但如果您希望下载图像,您将希望将其与之结合,如果您希望下载包含多个图像文件的zip,所有生成的图像文件都在客户端查看。如果您愿意在服务器端执行此操作,有一些选项,例如,现在已弃用。最好的方法是使用类似于on Node.JS的无头Chrome。使用Puppeter捕获网页将非常简单,如下所示:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
然而,它需要headless chrome能够在您的服务器上运行,这有一些依赖性,可能不适合在受限环境中运行。(另外,如果您没有使用Node.JS,您可能需要自己处理浏览器的安装/启动。)
如果您愿意使用SaaS服务,有很多选择,例如
这个问题由来已久,但也许还有人对最先进的答案感兴趣:
您可以使用:
从今天起2020年4月GitHub库html2Canvas
GitHub 20K stars| Azure pipeles:成功|下载130万次/mo|
引用:“JavaScript HTML渲染器该脚本允许您拍摄”屏幕截图“的网页或其中的一部分,直接在用户浏览器上。屏幕截图基于DOM,因此可能不会100%精确到真实的表示形式,因为它不会生成实际的屏幕截图,而是基于t