summaryrefslogtreecommitdiff
path: root/background.js
diff options
context:
space:
mode:
Diffstat (limited to 'background.js')
-rw-r--r--background.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/background.js b/background.js
new file mode 100644
index 0000000..bc33384
--- /dev/null
+++ b/background.js
@@ -0,0 +1,94 @@
+/* global chrome, MediaRecorder, FileReader */
+
+let recorder = null;
+let filename = null;
+chrome.runtime.onConnect.addListener(port => {
+
+ port.onMessage.addListener(msg => {
+ console.log(msg);
+ switch (msg.type) {
+ case 'SET_EXPORT_PATH':
+ filename = msg.filename
+ break
+ case 'REC_STOP':
+ recorder.stop()
+ break
+ case 'REC_CLIENT_PLAY':
+ if(recorder){
+ return
+ }
+ const tab = port.sender.tab
+ tab.url = msg.data.url
+ chrome.desktopCapture.chooseDesktopMedia(['tab', 'audio'], streamId => {
+ // Get the stream
+ navigator.webkitGetUserMedia({
+ audio: {
+ mandatory: {
+ chromeMediaSource: 'system'
+ }
+ },
+ video: {
+ mandatory: {
+ chromeMediaSource: 'desktop',
+ chromeMediaSourceId: streamId,
+ minWidth: 1280,
+ maxWidth: 1280,
+ minHeight: 720,
+ maxHeight: 720,
+ minFrameRate: 60,
+ }
+ }
+ }, stream => {
+ var chunks=[];
+ recorder = new MediaRecorder(stream, {
+ videoBitsPerSecond: 2500000,
+ ignoreMutedMedia: true,
+ mimeType: 'video/webm'
+ });
+ recorder.ondataavailable = function (event) {
+ if (event.data.size > 0) {
+ chunks.push(event.data);
+ }
+ };
+
+ recorder.onstop = function () {
+ var superBuffer = new Blob(chunks, {
+ type: 'video/webm'
+ });
+
+ var url = URL.createObjectURL(superBuffer);
+ // var a = document.createElement('a');
+ // document.body.appendChild(a);
+ // a.style = 'display: none';
+ // a.href = url;
+ // a.download = 'test.webm';
+ // a.click();
+
+ chrome.downloads.download({
+ url: url,
+ filename: filename
+ }, ()=>{
+ });
+ }
+ setTimeout(function(){
+ recorder.start();
+ }, 3000)
+ }, error => console.log('Unable to get user media', error))
+ })
+ break
+ default:
+ console.log('Unrecognized message', msg)
+ }
+ })
+
+ chrome.downloads.onChanged.addListener(function(delta) {
+ if (!delta.state ||(delta.state.current != 'complete')) {
+ return;
+ }
+ try{
+ port.postMessage({downloadComplete: true})
+ }
+ catch(e){}
+ });
+
+})