-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
options.js
99 lines (90 loc) · 3.42 KB
/
options.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
import {getRegularColour, setRegularColour, getIncognitoColour, setIncognitoColour, getDebugMode, setDebugMode} from './config.js'
let collectedDebugLogs = [];
let debugMode = false;
getDebugMode().then(val => {
debugMode = val
})
function loadOptions() {
let status = document.getElementById("status")
let colour = document.getElementById("colour")
let incognitoColour = document.getElementById("incognito-colour")
getRegularColour().then(val => {
colour.value = val
}).catch(error => {
status.textContent = `Couldn't load settings: ${error}`
})
getIncognitoColour().then(val => {
incognitoColour.value = val
}).catch(error => {
status.textContent = `Couldn't load settings: ${error}`
})
// We always start with debug mode turned off.
setDebugMode(false).catch(error => {
status.textContent = `Couldn't load settings: ${error}`
})
}
function saveOptions() {
let status = document.getElementById("status")
let colour = document.getElementById("colour")
let incognitoColour = document.getElementById("incognito-colour")
let debugMode = document.getElementById("debug-mode")
setRegularColour(colour.value).then(() => {
status.textContent = ""
}).catch(error => {
status.textContent = `Couldn't save settings: ${error}`
})
setIncognitoColour(incognitoColour.value).then(() => {
status.textContent = ""
}).catch(error => {
status.textContent = `Couldn't save settings: ${error}`
})
setDebugMode(debugMode.checked).then(() => {
status.textContent = ""
}).catch(error => {
status.textContent = `Couldn't save settings: ${error}`
})
}
document.addEventListener("DOMContentLoaded", loadOptions)
for (let elem of document.getElementsByClassName("save")) {
elem.addEventListener("input", saveOptions)
}
document.getElementById("debug-mode").addEventListener("input", () => {
collectedDebugLogs = []
})
function updateDebugLogsVisibility() {
let debugLogs = document.getElementById("debug-logs")
let debugLogsUncollected = document.getElementById("debug-logs-uncollected")
let debugLogsCollected = document.getElementById("debug-logs-collected")
if (debugMode) {
debugLogs.style.display = ""
} else {
debugLogs.style.display = "none"
}
debugLogsUncollected.style.display = ""
debugLogsCollected.style.display = "none"
}
updateDebugLogsVisibility()
chrome.storage.onChanged.addListener((changes, areaName) => {
let val = changes["debug-mode"]?.newValue
if (val !== undefined) {
debugMode = val
updateDebugLogsVisibility()
}
})
document.getElementById("copy-debug-logs").addEventListener("click", () => {
let debugLogsString = collectedDebugLogs.join("\n")
navigator.clipboard.writeText(`=== Tab Muter debug logs ===\nlogs collected: ${(new Date()).toISOString()}\nentries: ${collectedDebugLogs.length}\n` + debugLogsString + "\n=== end ===\n").catch(error => {
document.getElementById("status").textContent = `Couldn't copy logs to clipboard: ${error}`
})
})
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === "log") {
if (debugMode) {
let debugLogsUncollected = document.getElementById("debug-logs-uncollected")
let debugLogsCollected = document.getElementById("debug-logs-collected")
collectedDebugLogs.push(`${(new Date()).toISOString()} ${message.level}: ${message.content}`)
debugLogsUncollected.style.display = "none"
debugLogsCollected.style.display = ""
}
}
})