-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathutils.js
More file actions
298 lines (245 loc) · 10 KB
/
utils.js
File metadata and controls
298 lines (245 loc) · 10 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { Utils } from './jsplayready/utils.js';
import { RemoteCdm } from './jsplayready/remote_cdm.js';
export class AsyncLocalStorage {
static async setStorage(items) {
return new Promise((resolve, reject) => {
chrome.storage.local.set(items, () => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError));
} else {
resolve();
}
});
});
}
static async getStorage(keys) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(keys, (result) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError));
} else {
resolve(result);
}
});
});
}
static async removeStorage(keys) {
return new Promise((resolve, reject) => {
chrome.storage.local.remove(keys, (result) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError));
} else {
resolve(result);
}
});
});
}
}
export class SettingsManager {
static async setEnabled(enabled) {
await AsyncLocalStorage.setStorage({ enabled: enabled });
}
static async getEnabled() {
const result = await AsyncLocalStorage.getStorage(["enabled"]);
return result["enabled"] === undefined ? false : result["enabled"];
}
static downloadFile(content, filename) {
const blob = new Blob([content], { type: 'application/octet-stream' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
static async importDevice(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = async function (loaded) {
const result = loaded.target.result;
const b64_device = Utils.bytesToBase64(new Uint8Array(result));
const device_name = file.name.slice(0, -4);
if (!await DeviceManager.loadPlayreadyDevice(device_name)) {
await DeviceManager.savePlayreadyDevice(device_name, b64_device);
}
await DeviceManager.saveSelectedPlayreadyDevice(device_name);
resolve();
};
reader.readAsArrayBuffer(file);
});
}
static async saveDarkMode(dark_mode) {
await AsyncLocalStorage.setStorage({ dark_mode: dark_mode });
}
static async getDarkMode() {
const result = await AsyncLocalStorage.getStorage(["dark_mode"]);
return result["dark_mode"] || false;
}
static setDarkMode(dark_mode) {
const textImage = document.getElementById("textImage");
const toggle = document.getElementById('darkModeToggle');
toggle.checked = dark_mode;
document.body.classList.toggle('dark-mode', dark_mode);
textImage.src = dark_mode ? "../images/proxy_text_dark.png" : "../images/proxy_text.png";
}
static async loadRemoteCDM(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = async function (loaded) {
const result = loaded.target.result;
let json_file = null;
try {
json_file = JSON.parse(result);
} catch {
resolve();
return;
}
const device_name = json_file.device_name ?? json_file.name;
console.log("NAME:", device_name);
if (await RemoteCDMManager.loadRemoteCDM(device_name) === "{}") {
await RemoteCDMManager.saveRemoteCDM(device_name, json_file);
}
await RemoteCDMManager.saveSelectedRemoteCDM(device_name);
resolve();
};
reader.readAsText(file);
});
}
static async saveSelectedDeviceType(selected_type) {
await AsyncLocalStorage.setStorage({ device_type: selected_type });
}
static async getSelectedDeviceType() {
const result = await AsyncLocalStorage.getStorage(["device_type"]);
return result["device_type"] || "PRD";
}
static setSelectedDeviceType(device_type) {
switch (device_type) {
case "PRD":
/*const prd_select = document.getElementById('prd_select');
prd_select.checked = true;*/
break;
case "REMOTE":
/*const remote_select = document.getElementById('remote_select');
remote_select.checked = true;*/
break;
}
}
static async saveUseShakaPackager(use_shaka) {
await AsyncLocalStorage.setStorage({ use_shaka: use_shaka });
}
static async getUseShakaPackager() {
const result = await AsyncLocalStorage.getStorage(["use_shaka"]);
return result["use_shaka"] ?? true;
}
static async saveExecutableName(exe_name) {
await AsyncLocalStorage.setStorage({ exe_name: exe_name });
}
static async getExecutableName() {
const result = await AsyncLocalStorage.getStorage(["exe_name"]);
return result["exe_name"] ?? "N_m3u8DL-RE";
}
}
export class RemoteCDMManager {
static async saveRemoteCDM(name, obj) {
const result = await AsyncLocalStorage.getStorage(['remote_cdms']);
const array = result.remote_cdms === undefined ? [] : result.remote_cdms;
array.push(name);
await AsyncLocalStorage.setStorage({ remote_cdms: array });
await AsyncLocalStorage.setStorage({ [name]: obj });
}
static async loadRemoteCDM(name) {
const result = await AsyncLocalStorage.getStorage([name]);
return JSON.stringify(result[name] || {});
}
static setRemoteCDM(name, value){
const remote_combobox = document.getElementById('remote-combobox');
const remote_element = document.createElement('option');
remote_element.text = name;
remote_element.value = value;
remote_combobox.appendChild(remote_element);
}
static async loadSetAllRemoteCDMs() {
const result = await AsyncLocalStorage.getStorage(['remote_cdms']);
const array = result.remote_cdms || [];
for (const item of array) {
this.setRemoteCDM(item, await this.loadRemoteCDM(item));
}
}
static async saveSelectedRemoteCDM(name) {
await AsyncLocalStorage.setStorage({ selected_remote_cdm: name });
}
static async getSelectedRemoteCDM() {
const result = await AsyncLocalStorage.getStorage(["selected_remote_cdm"]);
return result["selected_remote_cdm"] || "";
}
static async selectRemoteCDM(name) {
document.getElementById('remote-combobox').value = await this.loadRemoteCDM(name);
}
static async removeSelectedRemoteCDM() {
const selected_remote_cdm_name = await RemoteCDMManager.getSelectedRemoteCDM();
const result = await AsyncLocalStorage.getStorage(['remote_cdms']);
const array = result.remote_cdms === undefined ? [] : result.remote_cdms;
const index = array.indexOf(selected_remote_cdm_name);
if (index > -1) {
array.splice(index, 1);
}
await AsyncLocalStorage.setStorage({ remote_cdms: array });
await AsyncLocalStorage.removeStorage([selected_remote_cdm_name]);
}
static async removeSelectedRemoteCDMKey() {
await AsyncLocalStorage.removeStorage(["selected_remote_cdm"]);
}
}
export class DeviceManager {
static async savePlayreadyDevice(name, value) {
const result = await AsyncLocalStorage.getStorage(['devices']);
const array = result.devices === undefined ? [] : result.devices;
array.push(name);
await AsyncLocalStorage.setStorage({ devices: array });
await AsyncLocalStorage.setStorage({ [name]: value });
}
static async loadPlayreadyDevice(name) {
const result = await AsyncLocalStorage.getStorage([name]);
return result[name] || "";
}
static setPlayreadyDevice(name, value){
const prd_combobox = document.getElementById('prd-combobox');
const prd_element = document.createElement('option');
prd_element.text = name;
prd_element.value = value;
prd_combobox.appendChild(prd_element);
}
static async loadSetAllPlayreadyDevices() {
const result = await AsyncLocalStorage.getStorage(['devices']);
const array = result.devices || [];
for (const item of array) {
this.setPlayreadyDevice(item, await this.loadPlayreadyDevice(item));
}
}
static async saveSelectedPlayreadyDevice(name) {
await AsyncLocalStorage.setStorage({ selected: name });
}
static async getSelectedPlayreadyDevice() {
const result = await AsyncLocalStorage.getStorage(["selected"]);
return result["selected"] || "";
}
static async selectPlayreadyDevice(name) {
document.getElementById('prd-combobox').value = await this.loadPlayreadyDevice(name);
}
static async removeSelectedPlayreadyDevice() {
const selected_device_name = await DeviceManager.getSelectedPlayreadyDevice();
const result = await AsyncLocalStorage.getStorage(['devices']);
const array = result.devices === undefined ? [] : result.devices;
const index = array.indexOf(selected_device_name);
if (index > -1) {
array.splice(index, 1);
}
await AsyncLocalStorage.setStorage({ devices: array });
await AsyncLocalStorage.removeStorage([selected_device_name]);
}
static async removeSelectedPlayreadyDeviceKey() {
await AsyncLocalStorage.removeStorage(["selected"]);
}
}