-
Notifications
You must be signed in to change notification settings - Fork 0
/
background-script.js
105 lines (100 loc) · 2.52 KB
/
background-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
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
95
96
97
98
99
100
101
102
103
104
105
// -*- coding: utf-8 -*-
// vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
function onCreated(){
console.log("created");
}
// dumb hard coded strings, does not make use of l10n resources
const str_downloadDisp_jp = "表示画像一括ダウンロード";
const str_downloadLink_jp = "リンク先画像一括ダウンロード";
var str_downloadDisp = str_downloadDisp_jp;
var str_downloadLink = str_downloadLink_jp;
if (navigator.language.startsWith('en')){
str_downloadDisp = "Display image batch download"; // machine translation of str_downloadDisp_jp
str_downloadLink = "Linked image batch download"; // str_downloadLink_jp
}
browser.menus.create({
id: "downloadDisp",
title: str_downloadDisp,
contexts: ["all"]
}, onCreated);
browser.menus.create({
id: "downloadLink",
title: str_downloadLink,
contexts: ["all"]
}, onCreated);
browser.menus.onClicked.addListener((info, tab) => {
switch (info.menuItemId) {
case "downloadDisp":
downloadDisp();
break;
case "downloadLink":
downloadLink();
break;
}
});
function onStartedDownload(id) {
console.log(`Started downloading: ${id}`);
}
function onFailed(error) {
console.log(`Download failed: ${error}`);
}
function onStart(){
console.log("dl start");
}
function onError(){
console.log("error");
}
function onDownloadError(){
console.log("dl error");
}
function onSendError(){
console.log("send error.");
}
function sendMessageToTabsDisp(tabs) {
for (let tab of tabs) {
console.log(tab.id);
browser.tabs.sendMessage(
tab.id,
{greeting: "disp"}
).then(response => {
downloadExec(response);
}).catch(onSendError);
}
}
function sendMessageToTabsLink(tabs) {
for (let tab of tabs) {
console.log("link");
browser.tabs.sendMessage(
tab.id,
{greeting: "link"}
).then(response => {
downloadExec(response);
}).catch(onSendError);
}
}
function downloadDisp(){
browser.tabs.query({
currentWindow: true,
active: true
}).then(sendMessageToTabsDisp).catch(onError);
}
function downloadLink(){
browser.tabs.query({
currentWindow: true,
active: true
}).then(sendMessageToTabsLink).catch(onError);
}
function downloadExec(response){
for(let url of response.list){
let filename_index = url.lastIndexOf('/')
let downloadUrl = url;
let filename = url.substring(filename_index + 1);
let downloading = browser.downloads.download({
url : downloadUrl,
//filename : filename,
//saveAs: true,
conflictAction : 'uniquify'
})
downloading.then(onStart, onDownloadError);
}
}