谷歌的“报告错误”或“反馈工具”让你选择浏览器窗口的一个区域来创建一个截图,并提交你对错误的反馈。
截图来自Jason Small,发布在一个重复的问题中。
他们是怎么做到的?谷歌的JavaScript反馈API从这里加载,反馈模块的概述将演示截图功能。
谷歌的“报告错误”或“反馈工具”让你选择浏览器窗口的一个区域来创建一个截图,并提交你对错误的反馈。
截图来自Jason Small,发布在一个重复的问题中。
他们是怎么做到的?谷歌的JavaScript反馈API从这里加载,反馈模块的概述将演示截图功能。
当前回答
PoC
正如Niklas所提到的,你可以使用html2canvas库在浏览器中使用JS进行截图。我将通过提供一个使用这个库截屏的例子(“概念证明”)来扩展他的回答:
function report() { let region = document.querySelector("body"); // whole screen html2canvas(region, { onrendered: function(canvas) { let pngUrl = canvas.toDataURL(); // png in dataURL format let img = document.querySelector(".screen"); img.src = pngUrl; // here you can allow user to set bug-region // and send it with 'pngUrl' to server }, }); } .container { margin-top: 10px; border: solid 1px black; } <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script> <div>Screenshot tester</div> <button onclick="report()">Take screenshot</button> <div class="container"> <img width="75%" class="screen"> </div>
在onrender中的report()函数中,在获得图像作为数据URI后,您可以将其显示给用户,并允许他通过鼠标绘制“bug区域”,然后将截图和区域坐标发送到服务器。
在这个例子中,async/await版本使用了很好的makeScreenshot()函数。
更新
简单的例子,它允许你截屏,选择区域,描述bug和发送POST请求(这里是jsfiddle)(主要函数是report())。
async function report() { let screenshot = await makeScreenshot(); // png dataUrl let img = q(".screen"); img.src = screenshot; let c = q(".bug-container"); c.classList.remove('hide') let box = await getBox(); c.classList.add('hide'); send(screenshot,box); // sed post request with bug image, region and description alert('To see POST requset with image go to: chrome console > network tab'); } // ----- Helper functions let q = s => document.querySelector(s); // query selector helper window.report = report; // bind report be visible in fiddle html async function makeScreenshot(selector="body") { return new Promise((resolve, reject) => { let node = document.querySelector(selector); html2canvas(node, { onrendered: (canvas) => { let pngUrl = canvas.toDataURL(); resolve(pngUrl); }}); }); } async function getBox(box) { return new Promise((resolve, reject) => { let b = q(".bug"); let r = q(".region"); let scr = q(".screen"); let send = q(".send"); let start=0; let sx,sy,ex,ey=-1; r.style.width=0; r.style.height=0; let drawBox= () => { r.style.left = (ex > 0 ? sx : sx+ex ) +'px'; r.style.top = (ey > 0 ? sy : sy+ey) +'px'; r.style.width = Math.abs(ex) +'px'; r.style.height = Math.abs(ey) +'px'; } //console.log({b,r, scr}); b.addEventListener("click", e=>{ if(start==0) { sx=e.pageX; sy=e.pageY; ex=0; ey=0; drawBox(); } start=(start+1)%3; }); b.addEventListener("mousemove", e=>{ //console.log(e) if(start==1) { ex=e.pageX-sx; ey=e.pageY-sy drawBox(); } }); send.addEventListener("click", e=>{ start=0; let a=100/75 //zoom out img 75% resolve({ x:Math.floor(((ex > 0 ? sx : sx+ex )-scr.offsetLeft)*a), y:Math.floor(((ey > 0 ? sy : sy+ey )-b.offsetTop)*a), width:Math.floor(Math.abs(ex)*a), height:Math.floor(Math.abs(ex)*a), desc: q('.bug-desc').value }); }); }); } function send(image,box) { let formData = new FormData(); let req = new XMLHttpRequest(); formData.append("box", JSON.stringify(box)); formData.append("screenshot", image); req.open("POST", '/upload/screenshot'); req.send(formData); } .bug-container { background: rgb(255,0,0,0.1); margin-top:20px; text-align: center; } .send { border-radius:5px; padding:10px; background: green; cursor: pointer; } .region { position: absolute; background: rgba(255,0,0,0.4); } .example { height: 100px; background: yellow; } .bug { margin-top: 10px; cursor: crosshair; } .hide { display: none; } .screen { pointer-events: none } <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script> <body> <div>Screenshot tester</div> <button onclick="report()">Report bug</button> <div class="example">Lorem ipsum</div> <div class="bug-container hide"> <div>Select bug region: click once - move mouse - click again</div> <div class="bug"> <img width="75%" class="screen" > <div class="region"></div> </div> <div> <textarea class="bug-desc">Describe bug here...</textarea> </div> <div class="send">SEND BUG</div> </div> </body>
其他回答
PoC
正如Niklas所提到的,你可以使用html2canvas库在浏览器中使用JS进行截图。我将通过提供一个使用这个库截屏的例子(“概念证明”)来扩展他的回答:
function report() { let region = document.querySelector("body"); // whole screen html2canvas(region, { onrendered: function(canvas) { let pngUrl = canvas.toDataURL(); // png in dataURL format let img = document.querySelector(".screen"); img.src = pngUrl; // here you can allow user to set bug-region // and send it with 'pngUrl' to server }, }); } .container { margin-top: 10px; border: solid 1px black; } <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script> <div>Screenshot tester</div> <button onclick="report()">Take screenshot</button> <div class="container"> <img width="75%" class="screen"> </div>
在onrender中的report()函数中,在获得图像作为数据URI后,您可以将其显示给用户,并允许他通过鼠标绘制“bug区域”,然后将截图和区域坐标发送到服务器。
在这个例子中,async/await版本使用了很好的makeScreenshot()函数。
更新
简单的例子,它允许你截屏,选择区域,描述bug和发送POST请求(这里是jsfiddle)(主要函数是report())。
async function report() { let screenshot = await makeScreenshot(); // png dataUrl let img = q(".screen"); img.src = screenshot; let c = q(".bug-container"); c.classList.remove('hide') let box = await getBox(); c.classList.add('hide'); send(screenshot,box); // sed post request with bug image, region and description alert('To see POST requset with image go to: chrome console > network tab'); } // ----- Helper functions let q = s => document.querySelector(s); // query selector helper window.report = report; // bind report be visible in fiddle html async function makeScreenshot(selector="body") { return new Promise((resolve, reject) => { let node = document.querySelector(selector); html2canvas(node, { onrendered: (canvas) => { let pngUrl = canvas.toDataURL(); resolve(pngUrl); }}); }); } async function getBox(box) { return new Promise((resolve, reject) => { let b = q(".bug"); let r = q(".region"); let scr = q(".screen"); let send = q(".send"); let start=0; let sx,sy,ex,ey=-1; r.style.width=0; r.style.height=0; let drawBox= () => { r.style.left = (ex > 0 ? sx : sx+ex ) +'px'; r.style.top = (ey > 0 ? sy : sy+ey) +'px'; r.style.width = Math.abs(ex) +'px'; r.style.height = Math.abs(ey) +'px'; } //console.log({b,r, scr}); b.addEventListener("click", e=>{ if(start==0) { sx=e.pageX; sy=e.pageY; ex=0; ey=0; drawBox(); } start=(start+1)%3; }); b.addEventListener("mousemove", e=>{ //console.log(e) if(start==1) { ex=e.pageX-sx; ey=e.pageY-sy drawBox(); } }); send.addEventListener("click", e=>{ start=0; let a=100/75 //zoom out img 75% resolve({ x:Math.floor(((ex > 0 ? sx : sx+ex )-scr.offsetLeft)*a), y:Math.floor(((ey > 0 ? sy : sy+ey )-b.offsetTop)*a), width:Math.floor(Math.abs(ex)*a), height:Math.floor(Math.abs(ex)*a), desc: q('.bug-desc').value }); }); }); } function send(image,box) { let formData = new FormData(); let req = new XMLHttpRequest(); formData.append("box", JSON.stringify(box)); formData.append("screenshot", image); req.open("POST", '/upload/screenshot'); req.send(formData); } .bug-container { background: rgb(255,0,0,0.1); margin-top:20px; text-align: center; } .send { border-radius:5px; padding:10px; background: green; cursor: pointer; } .region { position: absolute; background: rgba(255,0,0,0.4); } .example { height: 100px; background: yellow; } .bug { margin-top: 10px; cursor: crosshair; } .hide { display: none; } .screen { pointer-events: none } <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script> <body> <div>Screenshot tester</div> <button onclick="report()">Report bug</button> <div class="example">Lorem ipsum</div> <div class="bug-container hide"> <div>Select bug region: click once - move mouse - click again</div> <div class="bug"> <img width="75%" class="screen" > <div class="region"></div> </div> <div> <textarea class="bug-desc">Describe bug here...</textarea> </div> <div class="send">SEND BUG</div> </div> </body>
这里有一个使用getDisplayMedia的例子
document.body.innerHTML = '<video style="width: 100%; height: 100%; border: 1px black solid;"/>';
navigator.mediaDevices.getDisplayMedia()
.then( mediaStream => {
const video = document.querySelector('video');
video.srcObject = mediaStream;
video.onloadedmetadata = e => {
video.play();
video.pause();
};
})
.catch( err => console.log(`${err.name}: ${err.message}`));
同样值得一看的还有Screen Capture API文档。
这是一个完整的截图示例,可以在2021年使用chrome浏览器。最终结果是一个准备传输的blob。流程是:请求媒体>抓取帧>绘制到画布>传输到blob。如果你想更有效地使用内存探索OffscreenCanvas或者ImageBitmapRenderingContext
https://jsfiddle.net/v24hyd3q/1/
// Request media
navigator.mediaDevices.getDisplayMedia().then(stream =>
{
// Grab frame from stream
let track = stream.getVideoTracks()[0];
let capture = new ImageCapture(track);
capture.grabFrame().then(bitmap =>
{
// Stop sharing
track.stop();
// Draw the bitmap to canvas
canvas.width = bitmap.width;
canvas.height = bitmap.height;
canvas.getContext('2d').drawImage(bitmap, 0, 0);
// Grab blob from canvas
canvas.toBlob(blob => {
// Do things with blob here
console.log('output blob:', blob);
});
});
})
.catch(e => console.log(e));
你可以试试我的新JS库:截图. JS。
它可以拍摄真实的屏幕截图。
你加载脚本:
<script src="https://raw.githubusercontent.com/amiad/screenshot.js/master/screenshot.js"></script>
并截屏:
new Screenshot({success: img => {
// callback function
myimage = img;
}});
您可以在项目页面中阅读更多选项。
你的web应用现在可以使用getUserMedia()截取客户端整个桌面的“原生”截图:
来看看这个例子:
https://www.webrtc-experiment.com/Pluginfree-Screen-Sharing/
客户端将不得不使用chrome(目前),将需要启用chrome://标志下的屏幕捕捉支持。