Chrome 扩展连接 Chrome 调试器并截取完整屏幕截图
Chrome Extension connect Chrome Debugger and take full screenshot
我想使用 chrome 插件在活动选项卡中截取全尺寸屏幕截图。
我知道有一个函数叫做chrome.tabs.captureVisibleTab(),但这对获取整页截图没有帮助。
我知道我们可以从 chrome 获取整页屏幕截图(devtools > ctrl+shift+p > Capture full size screenshot)。我想使用此功能或其他功能截屏。
使用我在下面给出的代码,我可以在 linux 机器上截取整页屏幕截图以达到我的目的。但是当我 运行 在 windows 机器上安装插件时,我得到这样的图像:
这是我的代码。从底部开始阅读可能更有帮助:
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 如何区分表面和视图(如果您的代码有效),但是对于 Windows 您必须更改此设置:
"Page.captureScreenshot",
{
format: "jpeg",
quality: 60,
fromSurface: false,
为此:
"Page.captureScreenshot",
{
format: "jpeg",
quality: 60,
fromSurface: true,
您的代码将生成包含此修复程序的整页屏幕截图。我不确定您是否更改了从中获取此代码的示例中的 fromSurface
属性 值,因为在文档中他们说此 属性 将强制 API在视图中制作屏幕截图,如果设置为 false
,这对于裁剪结果是有意义的。
我想使用 chrome 插件在活动选项卡中截取全尺寸屏幕截图。
我知道有一个函数叫做chrome.tabs.captureVisibleTab(),但这对获取整页截图没有帮助。
我知道我们可以从 chrome 获取整页屏幕截图(devtools > ctrl+shift+p > Capture full size screenshot)。我想使用此功能或其他功能截屏。
使用我在下面给出的代码,我可以在 linux 机器上截取整页屏幕截图以达到我的目的。但是当我 运行 在 windows 机器上安装插件时,我得到这样的图像:
这是我的代码。从底部开始阅读可能更有帮助:
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 如何区分表面和视图(如果您的代码有效),但是对于 Windows 您必须更改此设置:
"Page.captureScreenshot",
{
format: "jpeg",
quality: 60,
fromSurface: false,
为此:
"Page.captureScreenshot",
{
format: "jpeg",
quality: 60,
fromSurface: true,
您的代码将生成包含此修复程序的整页屏幕截图。我不确定您是否更改了从中获取此代码的示例中的 fromSurface
属性 值,因为在文档中他们说此 属性 将强制 API在视图中制作屏幕截图,如果设置为 false
,这对于裁剪结果是有意义的。