MediaStream Recording API新特性来实现
首先需要授权,mac的话需要在系统设置里打开浏览器屏幕共享权限
代码:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | navigator.mediaDevices.getDisplayMedia({ video: true })
 .then(function(stream) {
 
 let recorder = new MediaRecorder(stream);
 let recordedChunks = [];
 
 
 recorder.start();
 
 recorder.ondataavailable = function(event) {
 recordedChunks.push(event.data);
 }
 
 
 recorder.onstop = function() {
 
 let blob = new Blob(recordedChunks, { type: 'video/webm' });
 let url = URL.createObjectURL(blob);
 let a = document.createElement('a');
 a.href = url;
 a.download = 'recording.webm';
 document.body.appendChild(a);
 a.click();
 }
 
 })
 .catch(function(err) {
 console.error('Error: ' + err);
 });
 
 | 
不得不说js越来越强,浏览器的系统权限以及W3C规范越来越开放,怪不得谷歌可以在浏览器内运行个操作系统