简体   繁体   English

在浏览器控制台中使用 JavaScript 调用 Firefox 的截图助手 function

[英]Calling Firefox's :screenshot helper function in the Browser Console with JavaScript

I am trying to write a small script to copy in the web console of a live page, and part of what this script needs to do is take several screenshots of the document body.我正在尝试编写一个小脚本以在实时页面的 web 控制台中复制,该脚本需要做的部分工作是对文档正文进行几张截图。 This script will be for personal use, for a very specific task, so I think it would not be a problem to use Firefox's built in helper function :screenshot , instead of a more cross-compatible solution.这个脚本将供个人使用,用于非常具体的任务,所以我认为使用 Firefox 的内置帮助程序 function :screenshot不会有问题,而不是更交叉兼容的解决方案。

I have read this question about the same topic, which explains why it is not possible to call such helper functions from the webpage's console in JavaScript. But what I thought I could do instead, is to use Firefox's browser console, which gives access to the entire browser.我读过这个关于同一主题的问题,它解释了为什么无法从 JavaScript 的网页控制台调用此类辅助函数。但我认为我可以做的是使用 Firefox 的浏览器控制台,它可以访问整个浏览器。 Again, I have been literaly just copying and pasting functions into the console to use while interacting with the page, so if I can call the :screenshot function programmatically from the browser console I would just need to figure out how to access the DOM of a particular document or tab and I will get the same result.同样,我一直只是将函数复制并粘贴到控制台以在与页面交互时使用,所以如果我可以从浏览器控制台以编程方式调用:screenshot function 我只需要弄清楚如何访问特定document或选项卡,我会得到相同的结果。

I have tried to import and use html2canvas, but it did not work most probably because the content that I am trying to screenshot resides inside a shadow-root .我尝试导入和使用 html2canvas,但它很可能不起作用,因为我尝试截屏的内容位于shadow-root中。 I know that one alternative is to write my own extension, but I would like to avoid such a job for this task.我知道另一种选择是编写我自己的扩展,但我想避免为此任务做这样的工作。 Eventually, do you know if it is possible to achieve similar results in a Chromium based browser (Brave)?最终,您知道是否有可能在基于 Chromium 的浏览器 (Brave) 中实现类似的结果吗?

Thank you very much:D非常感谢:D

The good news is that, yes, you can invoke the devtools screenshot functionality from the browser console.好消息是,是的,您可以从浏览器控制台调用 devtools 屏幕截图功能。 Conveniently the default selected node is the document body.方便地,默认选择的节点是文档主体。

(async()=>{
    const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm");
    const { gDevTools } = require("devtools/client/framework/devtools");
    const { TargetFactory } = require("devtools/client/framework/target");
    const target = await TargetFactory.forTab(gBrowser.selectedTab);
    const toolbox = await gDevTools.showToolbox(target, "inspector");
    const inspector = toolbox.getPanel("inspector");
    inspector.screenshotNode();
})()

Now the bad news.现在是坏消息。 Accessing the DOM of content page from the browser console is unbearably tricky:(从浏览器控制台访问内容页面的 DOM 非常棘手:(

You are going about solving the problem in the wrong manner.您将以错误的方式解决问题。 Firefox since version 57 has built in tools to provide what you want. Firefox 自版本 57 以来内置了工具来提供您想要的东西。 To accomplish this you want to use Firefox's headless mode with Webdriver if needed.要完成此操作,您需要在需要时将Firefox 的无头模式与Webdriver 结合使用。

The simple example from MDN is来自 MDN 的简单示例是

/path/to/firefox -P my-profile -headless --screenshot https://developer.mozilla.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM