使用 JavaScript 截取网页屏幕截图?

发布于 2024-07-05 06:12:03 字数 189 浏览 8 评论 0原文

是否可以使用 JavaScript 截取网页的屏幕截图,然后将其提交回服务器?

我不太关心浏览器安全问题。 等等,因为实施将针对 HTA。 但这可能吗?

Is it possible to to take a screenshot of a webpage with JavaScript and then submit that back to the server?

I'm not so concerned with browser security issues. etc. as the implementation would be for HTA. But is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

懵少女 2024-07-12 06:12:04

我制作了一个简单的函数,使用 rasterizeHTML 构建 svg 和/或包含页面内容的图像。

看看:

https://github.com/orisha/tdg-screen-射手纯js

I made a simple function that uses rasterizeHTML to build a svg and/or an image with page contents.

Check it out :

https://github.com/orisha/tdg-screen-shooter-pure-js

暮倦 2024-07-12 06:12:03

您可以使用 HTA 和 VBScript 来实现这一点。 只需调用外部工具即可进行截图。 我忘了叫什么名字了,不过在 Windows Vista 上有一个可以截图的工具。 您甚至不需要额外安装。

至于自动 - 这完全取决于您使用的工具。 如果它有 API,我相信您可以通过几个 Visual Basic 调用来触发屏幕截图和保存过程,而用户不会知道您做了什么。

既然您提到了 HTA,我假设您使用的是 Windows,并且(可能)非常了解您的环境(例如操作系统和版本)。

You can achieve that using HTA and VBScript. Just call an external tool to do the screenshotting. I forgot what the name is, but on Windows Vista there is a tool to do screenshots. You don't even need an extra install for it.

As for as automatic - it totally depends on the tool you use. If it has an API, I am sure you can trigger the screenshot and saving process through a couple of Visual Basic calls without the user knowing that you did what you did.

Since you mentioned HTA, I am assuming you are on Windows and (probably) know your environment (e.g. OS and version) very well.

谜兔 2024-07-12 06:12:03

如果您愿意在服务器端执行此操作,可以使用 PhantomJS 等选项,该选项现已弃用。 最好的方法是使用 Headless Chrome,在 Node.JS 上使用 Puppeteer 之类的东西。 使用 Puppeteer 捕获网页非常简单,如下所示:

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 服务,有很多选择,例如

If you are willing to do it on the server side, there are options like PhantomJS, which is now deprecated. The best way to go would be Headless Chrome with something like Puppeteer on Node.JS. Capturing a web page using Puppeteer would be as simple as follows:

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();
})();

However it requires headless chrome to be able to run on your servers, which has some dependencies and might not be suitable on restricted environments. (Also, if you are not using Node.JS, you might need to handle installation / launching of browsers yourself.)

If you are willing to use a SaaS service, there are many options such as

烈酒灼喉 2024-07-12 06:12:03

Google 正在 Google+ 中做到这一点,一位才华横溢的开发人员对其进行了逆向工程并生成了 http://html2canvas.hertzen.com/ . 要在 IE 中工作,您需要一个画布支持库,例如 http://excanvas.sourceforge.net/

Google is doing this in Google+ and a talented developer reverse engineered it and produced http://html2canvas.hertzen.com/ . To work in IE you'll need a canvas support library such as http://excanvas.sourceforge.net/

你列表最软的妹 2024-07-12 06:12:03

我已经使用 ActiveX 控件为 HTA 完成了此操作。 在 VB6 中构建截屏控件非常容易。 我必须使用 keybd_event 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 节点发送到服务器,然后在服务器端创建屏幕截图。

I have done this for an HTA by using an ActiveX control. It was pretty easy to build the control in VB6 to take the screenshot. I had to use the keybd_event API call because SendKeys can't do PrintScreen. Here's the code for that:

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

That only gets you as far as getting the window to the clipboard.

Another option, if the window you want a screenshot of is an HTA would be to just use an XMLHTTPRequest to send the DOM nodes to the server, then create the screenshots server-side.

终弃我 2024-07-12 06:12:03

我发现的另一种可能的解决方案是 http://www.phantomjs.org/ 它允许人们非常轻松截取页面截图等。 虽然我对这个问题的最初要求不再有效(不同的工作),但我可能会将 PhantomJS 集成到未来的项目中。

Another possible solution that I've discovered is http://www.phantomjs.org/ which allows one to very easily take screenshots of pages and a whole lot more. Whilst my original requirements for this question aren't valid any more (different job), I will likely integrate PhantomJS into future projects.

ま柒月 2024-07-12 06:12:03

Pounder 是否可以通过将整个 body 元素设置到画布中然后使用 canvas2image 来做到这一点?

http://www.nihilogic.dk/labs/canvas2image/

Pounder's if this is possible to do by setting the whole body elements into a canvase then using canvas2image ?

http://www.nihilogic.dk/labs/canvas2image/

无敌元气妹 2024-07-12 06:12:03

一种可能的方法是,如果在 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 您可以执行以下操作:

exec("CreateScreenShot.exe -url http://.... -save C :/shots domain_page.png");

然后你就可以在服务器端看到屏幕截图了。

A possible way to do this, if running on windows and have .NET installed you can do:

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;
}

And then via PHP you can do:

exec("CreateScreenShot.exe -url http://.... -save C:/shots domain_page.png");

Then you have the screenshot in the server side.

梦在深巷 2024-07-12 06:12:03

这可能不是您的理想解决方案,但仍然值得一提。

Snapsie 是一个开源软件,ActiveX 对象,使 Internet Explorer 屏幕截图能够被捕获并保存。 一旦 DLL 文件在客户端上注册,您应该能够捕获屏幕截图并使用 JavaScript 将文件上传到服务器。 缺点:需要在客户端注册DLL文件,并且仅适用于Internet Explorer。

This might not be the ideal solution for you, but it might still be worth mentioning.

Snapsie is an open source, ActiveX object that enables Internet Explorer screenshots to be captured and saved. Once the DLL file is registered on the client, you should be able to capture the screenshot and upload the file to the server withing JavaScript. Drawbacks: it needs to register the DLL file at the client and works only with Internet Explorer.

不疑不惑不回忆 2024-07-12 06:12:03

我们对报告错误也有类似的要求。 由于它是针对 Intranet 场景,因此我们能够使用浏览器插件(例如 Fireshot 适用于 Firefox 和 IE 屏幕截图 对于 Internet Explorer)。

We had a similar requirement for reporting bugs. Since it was for an intranet scenario, we were able to use browser addons (like Fireshot for Firefox and IE Screenshot for Internet Explorer).

青柠芒果 2024-07-12 06:12:03

这个问题很老了,但也许仍然有人对最先进的答案感兴趣:

您可以使用 getDisplayMedia

https://github .com/ondras/browsershot

This question is old but maybe there's still someone interested in a state-of-the-art answer:

You can use getDisplayMedia:

https://github.com/ondras/browsershot

流年里的时光 2024-07-12 06:12:03

SnapEngage 使用 Java 小程序 (1.5+) 用于制作浏览器屏幕截图。 AFAIK,java.awt.Robot 应该完成这项工作 - 用户只需允许小程序执行此操作(一次)。

我刚刚找到了一篇关于它的帖子:

The SnapEngage uses a Java applet (1.5+) to make a browser screenshot. AFAIK, java.awt.Robot should do the job - the user has just to permit the applet to do it (once).

And I have just found a post about it:

兔小萌 2024-07-12 06:12:03

我发现 dom-to-image 做得很好(比 html2canvas 好得多)。 请参阅以下问题& 答案: https://stackoverflow.com/a/32776834/207981

这个问题询问有关将其提交回服务器的问题,这应该是可能的,但如果您想下载图像,您需要将其与 FileSaver.js,如果您想下载包含多个图像文件的 zip,所有图像文件均在客户端生成,请查看 jszip

I found that dom-to-image did a good job (much better than html2canvas). See the following question & answer: https://stackoverflow.com/a/32776834/207981

This question asks about submitting this back to the server, which should be possible, but if you're looking to download the image(s) you'll want to combine it with FileSaver.js, and if you want to download a zip with multiple image files all generated client-side take a look at jszip.

不乱于心 2024-07-12 06:12:03

https://grabz.it 提供了一种在 Javascript 中截取屏幕截图的绝佳解决方案。

他们有一个灵活且易于使用的屏幕截图 API,可供任何类型的 JS 应用程序使用。

如果你想尝试一下,首先你应该获得授权 app key + Secret 和 < a href="https://grabz.it/api/javascript/download.ashx" rel="nofollow noreferrer">免费 SDK

然后,在您的应用程序中,实施步骤将是:

// 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("KEY", "SECRET").ConvertURL("http://www.google.com", 
{"width": 400, "height": 400, "format": "png", "delay", 10000}).Create();
</script>

仅此而已。
然后只需稍等片刻,图像就会自动出现在页面底部,无需重新加载页面。

屏幕截图机制还有其他功能,您可以在此处探索。

也可以将屏幕截图保存在本地。 为此,您需要使用 GrabzIt 服务器端 API。 如需了解更多信息,请查看此处的详细指南。

A great solution for screenshot taking in Javascript is the one by https://grabz.it.

They have a flexible and simple-to-use screenshot API which can be used by any type of JS application.

If you want to try it, at first you should get the authorization app key + secret and the free SDK

Then, in your app, the implementation steps would be:

// 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>

Screenshot could be customized with different parameters. For example:

GrabzIt("KEY", "SECRET").ConvertURL("http://www.google.com", 
{"width": 400, "height": 400, "format": "png", "delay", 10000}).Create();
</script>

That's all.
Then simply wait a short while and the image will automatically appear at the bottom of the page, without you needing to reload the page.

There are other functionalities to the screenshot mechanism which you can explore here.

It's also possible to save the screenshot locally. For that you will need to utilize GrabzIt server side API. For more info check the detailed guide here.

葬花如无物 2024-07-12 06:12:03

截至今天 2020 年 4 月 GitHub 库 html2Canvas
https://github.com/niklasvh/html2canvas

GitHub 20K star | Azure Pipeles:成功 | 下载量 130 万/月 |

quote : " JavaScript HTML 渲染器该脚本允许您直接在用户浏览器上获取网页或其部分内容的“屏幕截图”。屏幕截图基于 DOM,因此可能不会100% 准确地反映真实情况,因为它不会制作实际的屏幕截图,而是根据页面上的可用信息构建屏幕截图。

As of today Apr 2020 GitHub library html2Canvas
https://github.com/niklasvh/html2canvas

GitHub 20K stars | Azure pipeles : Succeeded | Downloads 1.3M/mo |

quote : " JavaScript HTML renderer The script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文