forked from OsaSoft/youtube-better-subscriptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
39 lines (29 loc) · 1.04 KB
/
util.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
const LOG_HEADER = "[YT-Better-Subs] ";
let printDate = false;
function isLogEnabled() {
return settings["settings.log.enabled"];
}
function log(content) {
if (isLogEnabled()) {
console.log(prepareMessage(content));
}
}
function logError(error) {
console.error(prepareMessage("ERROR! "), error.message);
console.error(error.stack.substring(0, 1000));
}
function prepareMessage(content = null) {
let message = LOG_HEADER;
if (printDate) message += new Date().toTimeString() + ": ";
if (content != null) message += (typeof content === 'object') ? JSON.stringify(content) : content;
return message;
}
function download(filename, content, applicationType = "text/plain") {
let element = document.createElement('a');
element.setAttribute('href', 'data:' + applicationType + ';charset=utf-8,' + encodeURIComponent(content));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}