-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
64 lines (57 loc) · 2.49 KB
/
script.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
//https://stackoverflow.com/questions/23150333/html5-javascript-dataurl-to-blob-blob-to-dataurl
//**blob to dataURL**
function blobToDataURL(blob, callback) {
var a = new FileReader();
a.onload = function(e) {callback(e.target.result);}
a.readAsDataURL(blob);
}
//From https://github.com/vsDizzy/SaveAsMHT/blob/master/extension/background.js
//https://stackoverflow.com/questions/6497548/chrome-extension-make-it-run-every-page-load
//https://github.com/parsegarden/Save-As-MHTML/blob/master/js/background.js
function saveCurrentTab (tabId, sendResponse) { //, changeInfo, tab) {
// if (changeInfo.status == 'complete') {
console.log(tabId)
chrome.pageCapture.saveAsMHTML({tabId:tabId},
function(blob) {
blobToDataURL(blob,
function(dataurl) {
chrome.downloads.download({saveAs:false,url:dataurl,filename:"saved.mhtml"});
})
sendResponse({farewell:FileReader().readAsText(blob)})
})
// }
}
//https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
//chrome.tabs.onUpdated.addListener()
chrome.commands.onCommand.addListener(function (command) {
console.log("key");
if (command == "save_page_as_mhtml") {
tabId = chrome.tabs.getSelected(null, function(tab) {
console.log(JSON.stringify(tab,null, 2))
saveCurrentTab(tab.id);
})
//await sleep(30 * 1000)
}
})
//To send a message from the browser to the extension, we need to
//pass a message to a content script in the middle.
//https://stackoverflow.com/questions/26156978/sending-message-from-content-script-to-background-script-breaks-chrome-extension
//https://stackoverflow.com/questions/11431337/sending-message-to-chrome-extension-from-a-web-page
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello") {
tabId = chrome.tabs.getSelected(null, function(tab) {
console.log(JSON.stringify(tab,null, 2))
saveCurrentTab(tab.id, sendResponse);
})
//await sleep(30 * 1000)
}
return true; //https://stackoverflow.com/questions/54126343/how-to-fix-unchecked-runtime-lasterror-the-message-port-closed-before-a-respon
//it would be better to send a message back
});