-
Notifications
You must be signed in to change notification settings - Fork 0
/
sd-background.js
executable file
·94 lines (87 loc) · 2.5 KB
/
sd-background.js
1
2
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
chrome.runtime.onInstalled.addListener((details) => {
//console.log(details);
if (details.reason.search(/install/g) === -1) {
return
}
chrome.storage.local.set({keepLastOnly:true});
chrome.tabs.create({
url: chrome.extension.getURL("welcome.html"),
active: true
})
})
chrome.browserAction.onClicked.addListener(function(tab) {
startScreenDrop(null);
});
let stream = null;
let mediaRecorder = null;
//let audioStream = null;
let audioTrack = null;
function downloadScreenDrop(url) {
chrome.storage.local.get('keepLastOnly',(res) => {
let filename = '';
if (res.keepLastOnly)
filename = 'screendrop.webm';
else
filename = 'screendrop-' + Date.now() + '.webm';
chrome.downloads.download({
url: url,
filename: filename,
conflictAction: res.keepLastOnly?'overwrite':'uniquify',
}, (downloadId) => {
//chrome.storage.local.set({downloadId});
});
});
}
async function startScreenDrop() {
if (mediaRecorder) {
stream.getTracks().forEach((t) => t.stop());
mediaRecorder.stop();
return;
}
try {
let audioStream = await navigator.mediaDevices.getUserMedia({audio:true});
audioTrack = audioStream.getAudioTracks()[0];
} catch (eaudiopermission) {
console.log('no audio',eaudiopermission);
}
try {
stream = await navigator.mediaDevices.getDisplayMedia({/*video: true*//*, audio: true*/});
} catch (epermission) {
console.log('cancelled')
return;
}
if (audioTrack)
stream.addTrack(audioTrack);
stream.getVideoTracks()[0].onended = function () {
startScreenDrop();
};
let chunks = [];
mediaRecorder = new MediaRecorder(stream,{mimeType:'video/webm;codecs=vp8,opus'});
mediaRecorder.ondataavailable = (e) => {
chunks.push(e.data);
}
mediaRecorder.onstop = function(e) {
chrome.browserAction.setIcon({path: 'icons/off.png'});
mediaRecorder = null;
let blob = new Blob(chunks, { 'type' : 'video/webm' });
chunks = [];
let fr = new FileReader();
fr.onload = (e) => {
let url = fr.result;
downloadScreenDrop(url);
}
fr.readAsDataURL(blob)
};
mediaRecorder.start(1000);
/*chrome.storage.local.get(['downloadId','keep'],(res) => {
if (res.downloadId) {
if (!res.keep) {
chrome.downloads.erase({id: res.downloadId});
//chrome.downloads.removeFile(res.downloadId, () => {
// chrome.downloads.erase({id: res.downloadId});
//});
}
}
});*/
chrome.browserAction.setIcon({path: 'icons/on.png'});
}