-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpopup.js
161 lines (141 loc) · 5.21 KB
/
popup.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
let statusDiv = document.getElementById('status');
let errorLabel = document.getElementById('error');
let successLabel = document.getElementById('success');
let pageDownloadDiv = document.getElementById('pageDownloadDiv');
let downloadButton = document.getElementById('download');
let downloadLabel = document.getElementById('downloadLabel');
let downloadDiv = document.getElementById('downloadDiv');
let optionsButton = document.getElementById('optionsButton');
let limitSpeedButton = document.getElementById('limitSpeedButton');
let externalLinkButton = document.getElementById('externalLinkButton');
let totalSpeedDiv = document.getElementById('totalSpeed');
let limitSpeedStatus = true;
function updateLimitSpeedStatus() {
getLimitSpeedStatus(function(status) {
limitSpeedStatus = status;
limitSpeedButton.style.color = limitSpeedStatus ? "black" : "#007bff";
limitSpeedButton.disabled = false;
});
}
function updateStatusDownloads(loop) {
getStatusDownloads(function (status) {
let html = '';
let totalSpeed = 0;
status.forEach(function(download) {
totalSpeed += download.speed;
html += `
<div style="margin-bottom: 12px; font-size: small">
<div class="d-flex">
<div class="ellipsis" style="padding-right: 24px">
${download.name}
</div>
<div class="ml-auto">
${download.format_eta.slice(0, 2)}h${download.format_eta.slice(3, 5)}m${download.format_eta.slice(6, 8)}
</div>
</div>
<div class="progress" style="margin: 2px 0 2px 0; height: 16px">
<div role="progressbar" class="progress-bar progress-bar-striped progress-bar-animated"
aria-valuenow="${download.percent}" aria-valuemin="0" aria-valuemax="100"
style="width: ${download.percent}%;">
${download.percent}%
</div>
</div>
</div>
`;
});
if (!html) {
html = `
<div class="text-center m-4" style="margin-bottom: 12px; color: gray">
No active downloads
</div>
`;
}
statusDiv.innerHTML = DOMPurify.sanitize(html);
if (totalSpeed > 0) {
totalSpeedDiv.innerHTML = DOMPurify.sanitize(`- ${(totalSpeed / (1000 * 1000)).toFixed(2)} MB/s`);
} else {
totalSpeedDiv.innerHTML = '';
}
if (loop) {
setTimeout(updateStatusDownloads, 3000, true);
}
});
}
function setErrorMessage(message) {
if (!message) {
errorLabel.hidden = true;
return;
}
errorLabel.innerText = message;
errorLabel.hidden = false;
}
function setSuccessMessage(message) {
if (!message) {
successLabel.hidden = true;
return;
}
successLabel.innerText = message;
successLabel.hidden = false;
}
downloadButton.onclick = function(event) {
downloadButton.disabled = true;
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
const url = tabs[0].url;
const name = tabs[0].title;
addPackage(name, url, function(success, errorMessage) {
if (!success) {
setErrorMessage(`Error downloading package: ${errorMessage}`);
return;
}
downloadDiv.hidden = true;
setSuccessMessage(`Download added`);
updateStatusDownloads(false);
});
});
};
optionsButton.onclick = function(event) {
chrome.tabs.create({'url': '/options.html'});
}
limitSpeedButton.onclick = function(event) {
limitSpeedStatus = !limitSpeedStatus;
limitSpeedButton.disabled = true;
setLimitSpeedStatus(limitSpeedStatus, function(success) {
updateLimitSpeedStatus();
});
}
pullStoredData(function() {
externalLinkButton.onclick = function(event) {
chrome.tabs.create({'url': `${origin}/home`});
}
isLoggedIn(function (loggedIn) {
if (!loggedIn) {
setErrorMessage(`You are not logged in, please go to the extension's option page`);
statusDiv.innerHTML = '';
return;
}
// Status downloads
updateStatusDownloads(true);
// Limit speed status
updateLimitSpeedStatus();
// Download current tab's page
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
const url = tabs[0].url;
const name = tabs[0].title;
downloadLabel.innerText = name;
checkURL(url, function(success) {
if (!success) {
// No plugin found for the current page
return;
}
getQueueData(function(urls) {
pageDownloadDiv.hidden = false;
if (urls.includes(url)) {
setErrorMessage(`Download already in queue`);
return;
}
downloadDiv.hidden = false;
});
});
});
});
});