-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.js
164 lines (133 loc) Β· 5.67 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
156
157
158
159
160
161
162
163
164
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
'use strict';
// Ask background.js about the state of this tab, and update popup accordingly.
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.runtime.sendMessage({ type: "getTabInfo", tab: tabs[0] }, (response) => updatePopup(response));
});
// Add a listener to receive dynamic updates from background.js
chrome.runtime.onMessage.addListener((request, s, c) => {
if (request.type == "updateFromBackground") {
updatePopup(request.data);
}
});
// Hook up buttons
document.getElementById('downloadUrl').onclick = () =>
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.runtime.sendMessage({ type: "downloadUrl", tab: tabs[0] });
});
document.getElementById('downloadLeft').onclick = () =>
chrome.tabs.query({ currentWindow: true }, function (tabs) {
chrome.runtime.sendMessage({ type: "batchDownload", tabs: getLeftSideTags(tabs) });
});
document.getElementById('downloadRight').onclick = () =>
chrome.tabs.query({ currentWindow: true }, function (tabs) {
chrome.runtime.sendMessage({ type: "batchDownload", tabs: getRightSideTags(tabs) });
});
document.getElementById('allDownloads').onclick = () => chrome.storage.sync.get(['server'], function (result) {
if (typeof result.server !== 'undefined' && result.server.trim() !== "") // check for undefined
chrome.tabs.create({
url: `${result.server}/minion/jobs`
});
});
document.getElementById('openSettings').onclick = () => chrome.runtime.openOptionsPage();
document.getElementById('recheckTab').onclick = () =>
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.runtime.sendMessage({ type: "recheckTab", tab: tabs[0] });
});
// Wow actual logic
function updatePopup(dataFromBackground) {
console.log("Received data from background.js: " + JSON.stringify(dataFromBackground));
document.getElementById('statusMsg').style = "color:black"
document.getElementById('downloadUrl').disabled = false;
try {
switch (dataFromBackground.status) {
case "downloaded":
document.getElementById('statusIcon').textContent = "β
";
document.getElementById('statusMsg').textContent = " saved to your LRR server!";
document.getElementById('statusMsg').style = "color:green"
document.getElementById('downloadUrl').disabled = true;
chrome.storage.sync.get(['server'], function (result) {
safeHtmlInject(document.getElementById('statusDetail'),
`<span>(id: <a href="${result.server}/reader?id=${dataFromBackground.arcId}" target= "_blank">
${dataFromBackground.arcId}
</a>)</span>`);
});
break;
case "downloading":
document.getElementById('statusIcon').textContent = "π";
document.getElementById('statusMsg').textContent = " being downloaded...";
document.getElementById('statusMsg').style = "color:blue"
document.getElementById('statusDetail').textContent = `(job: #${dataFromBackground.jobId})`;
break;
case "checking":
document.getElementById('statusIcon').textContent = "β";
document.getElementById('statusMsg').textContent = " being checked...";
document.getElementById('statusMsg').style = "color:orange"
document.getElementById('statusDetail').textContent = `(Please wait warmly.)`;
break;
case "other":
document.getElementById('statusIcon').textContent = "β";
document.getElementById('statusMsg').textContent = "... just a tab.";
document.getElementById('statusDetail').textContent = `(${dataFromBackground.message})`;
break;
case "error":
document.getElementById('statusIcon').textContent = "β";
document.getElementById('statusMsg').textContent = " not okay.";
document.getElementById('statusMsg').style = "color:red"
document.getElementById('statusDetail').textContent = `(Error: ${dataFromBackground.message})`;
break;
default:
document.getElementById('statusIcon').textContent = "π»";
document.getElementById('statusMsg').textContent = " a mystery.";
document.getElementById('statusDetail').textContent = `(Unknown status message ${dataFromBackground.status})`;
}
} catch (e) {
console.log(e);
document.getElementById('statusIcon').textContent = "π»";
document.getElementById('statusMsg').textContent = " a mystery.";
document.getElementById('statusDetail').textContent = `(${e})`;
}
}
function getLeftSideTags(tabs) {
var filtered_tabs = [];
var activeIndex = -1;
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].active) {
activeIndex = tabs[i].index;
break;
}
}
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].index < activeIndex) {
filtered_tabs.push(tabs[i]);
}
}
return filtered_tabs;
}
function getRightSideTags(tabs) {
var filtered_tabs = [];
var activeIndex = -1;
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].active) {
activeIndex = tabs[i].index;
break;
}
}
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].index > activeIndex) {
filtered_tabs.push(tabs[i]);
}
}
return filtered_tabs;
}
// Thanks firefox I guess https://devtidbits.com/2017/12/06/quick-fix-the-unsafe_var_assignment-warning-in-javascript
function safeHtmlInject(element, html) {
element.textContent = "";
const parser = new DOMParser()
const parsed = parser.parseFromString(html, "text/html")
const tags = parsed.getElementsByTagName("body")[0].children;
for (const tag of tags) {
element.appendChild(tag)
}
}