简体   繁体   English

Chrome 扩展程序连接 Chrome Debugger 并截取完整屏幕截图

[英]Chrome Extension connect Chrome Debugger and take full screenshot

I want to take a full size screenshot in the active tab using a chrome plugin.我想使用 chrome 插件在活动选项卡中截取全尺寸屏幕截图。

I know there is a function called chrome.tabs.captureVisibleTab () , but this does not help to get a full page screenshot.我知道有一个名为chrome.tabs.captureVisibleTab ()的函数,但这无助于获得完整的页面截图。

I know we can get a full page screenshot from chrome (devtools > ctrl+shift+p > Capture full size screenshot).我知道我们可以从 chrome 获取整页屏幕截图(devtools > ctrl+shift+p > Capture full size screenshot)。 I want to take a screenshot using this feature or a different feature.我想使用此功能或其他功能截取屏幕截图。

With the code I have given below, I can take full page screenshots on a linux machine for my purpose.使用我在下面给出的代码,我可以出于我的目的在 linux 机器上截取整页屏幕截图。 But when I run the plugin on a windows machine, I get an image like this:但是当我在 Windows 机器上运行插件时,我得到这样的图像: 在此处输入图片说明

this is my code.这是我的代码。 It may be more helpful to start reading from the bottom:从底部开始阅读可能更有帮助:

chrome.tabs.onUpdated.addListener(attachToDebugger);

function clearDeviceMetricsOverride(tabId, base_64_data) {
  chrome.debugger.sendCommand(
    {
      tabId: tabId,
    },
    "Emulation.clearDeviceMetricsOverride",
    function () {
      postData(base_64_data, tabId);
    }
  );
}


function captureScreenshot(tabId) {
  console.log(`{page}: captureScreenshot: status=aboutTo, tabId=${tabId}`);

  chrome.debugger.sendCommand(
    { tabId: tabId },
    "Page.captureScreenshot",
    {
      format: "jpeg",
      quality: 60,
      fromSurface: false,
    },
    (response) => {
      if (chrome.runtime.lastError) {
        console.log(`{back}: captureScreenshot: status=failed, tabId=${tabId}`);
      } else {
        var dataType = typeof response.data;
        console.log(
          `{back}: captureScreenshot: status=success, tabId=${tabId}, dataType=${dataType}`
        );
        let base_64_data = "data:image/jpg;base64," + response.data;
        setTimeout(() => {
          clearDeviceMetricsOverride(tabId, base_64_data);
        }, 500);
      }
    }
  );

  console.log(`{page}: captureScreenshot: status=commandSent, tabId=${tabId}`);
}

//---------------------------------------------------------------------------
function setDeviceMetricsOverride(tabId, height, width) {
  chrome.debugger.sendCommand(
    {
      tabId: tabId,
    },
    "Emulation.setDeviceMetricsOverride",
    { height: height, width: width, deviceScaleFactor: 1, mobile: false },
    function () {
      setTimeout(() => {
        captureScreenshot(tabId);
      }, 500);
    }
  );
}

//---------------------------------------------------------------------------

function getLayoutMetrics(tabId) {
  chrome.debugger.sendCommand(
    {
      tabId: tabId,
    },
    "Page.getLayoutMetrics",
    {},
    function (object) {
      console.log("---- get layout w: " + object.contentSize.width);
      console.log("---- get layout h: " + object.contentSize.height);
      const { height, width } = object.contentSize;
      setDeviceMetricsOverride(tabId, height, width);
    }
  );
}

//---------------------------------------------------------------------------

function setColorlessBackground(tabId) {
  console.log(`{back}: setColorlessBackground: status=aboutTo, tabId=${tabId}`);

  chrome.debugger.sendCommand(
    { tabId: tabId },
    "Emulation.setDefaultBackgroundColorOverride",
    { color: { r: 0, g: 0, b: 0, a: 0 } },
    function () {
      console.log(
        `{back}: setColorlessBackground: status=enabled, tabId=${tabId}`
      );
      getLayoutMetrics(tabId);
    }
  );

  console.log(
    `{back}: setColorlessBackground: status=commandSent, tabId=${tabId}`
  );
}

//---------------------------------------------------------------------------

function enableDTPage(tabId) {
  console.log(`{back}: enableDTPage: status=aboutTo, tabId=${tabId}`);

  chrome.debugger.sendCommand({ tabId: tabId }, "Page.enable", {}, function () {
    console.log(`{back}: enableDTPage: status=enabled, tabId=${tabId}`);
    setColorlessBackground(tabId);
  });

  console.log(`{back}: enableDTPage: status=commandSent, tabId=${tabId}`);
}

//---------------------------------------------------------------------------

function attachToDebugger(tabId, changeInfo, tab) {
  try {
    if (tab.status == "complete") {
      chrome.debugger.attach({ tabId: tabId }, "1.0", () => {
        if (chrome.runtime.lastError) {
          console.log(
            `{back}: debugger attach failed: error=${chrome.runtime.lastError.message}`
          );
        } else {
          console.log(`{back}: debugger attach success: tabId=${tabId}`);
          enableDTPage(tabId);
        }
      });
    }
  } catch {}
}

With the code I have given below, I can take full page screenshots on a linux machine for my purpose.使用我在下面给出的代码,我可以出于我的目的在 linux 机器上截取整页屏幕截图。

Not sure how Linux differentiates the surface and the view (if your code works), but for Windows you have to change this:不确定 Linux 如何区分表面和视图(如果您的代码有效),但对于 Windows,您必须更改这一点:

"Page.captureScreenshot",
    {
      format: "jpeg",
      quality: 60,
      fromSurface: false,

To this:对此:

"Page.captureScreenshot",
    {
      format: "jpeg",
      quality: 60,
      fromSurface: true,

Your code will produce full page screenshot with this fix.您的代码将使用此修复程序生成整页屏幕截图。 I'm not sure you changed the fromSurface property value from the example you took this code from, since in the docs they say that this property will force the API to make a screenshot within the view, if set to false , which makes sense for result to become cropped.我不确定您是否更改了fromSurface此代码的示例中的fromSurface属性值,因为在文档中他们说此属性将强制 API 在视图中制作屏幕截图,如果设置为false ,这对结果被裁剪。

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

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