background.js
2.56 KB
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
// background 虽然天然可以跨域,然而既然是后台就没有 cookie 的概念,即使 fetch 设置了 credentials, cookie 依然不会被保存
// http://stackoverflow.com/questions/29251842/why-new-js-fetch-standard-forbid-response-header-name-as-set-cookie-1-2
// const { createFFmpeg, fetchFile } = FFmpeg;
// async function test(){
// const ffmpeg = createFFmpeg({log: true});
// await ffmpeg.load();
// console.log(ffmpeg);
// };
// test();
const videos = {};
/**
* data: fr.result,
size: blobData.size,
mimeType: blobData.type,
name: document.title,
action: 'data'
*/
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
// console.log("Received %o from %o, frame", msg, sender.tab, sender.frameId);
sendResponse("Gotcha!");
if(msg.action === 'end'){
return download(sender.documentId);
}
if(!msg.data || msg.size !== msg.data.length){
return console.warn('data received error: ', msg.name);
}
const blobAsBinString = msg.data;
const bytes = new Uint8Array(blobAsBinString.length);
for(let i=0,len=blobAsBinString.length; i<len; i++){
bytes[i] = blobAsBinString.charCodeAt(i);
}
videos[sender.documentId] = videos[sender.documentId] || {};
videos[sender.documentId].blob = videos[sender.documentId].blob || [];
videos[sender.documentId].mimeType = msg.mimeType;
videos[sender.documentId].name = msg.name;
videos[sender.documentId].blob.push(bytes);
});
function download(id){
const tar = videos[id];
if(!tar){
return ;
}
const blob = new Blob(tar.blob, {type: tar.mimeType});
chrome.downloads.download({
url: URL.createObjectURL(blob),
filename: `${tar.name}.mkv`
}, resp => {
delete videos[id];
});
};
// chrome.webNavigation.onCompleted
chrome.webNavigation.onBeforeNavigate.addListener(frame => {
// chrome.webNavigation.getFrame({
// frameId: frame.frameId,
// tabId: frame.tabId
// }, details => {
// alert(JSON.stringify(details));
// });
});
// chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
// chrome.webNavigation.getAllFrames({tabId: tabId}, function(details) {
// details.forEach(function(frame) {
// console.log(tabId);
// // chrome.tabs.sendMessage(
// // tabId,
// // {text: 'hey_cs'},
// // {frameId: frame.frameId},
// // function(response) { console.log(response) }
// // );
// });
// });
// });