-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
177 lines (141 loc) · 6.1 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
165
166
167
168
169
170
171
172
173
174
175
176
177
function sendToTab(action, value) {
try{
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: action, value: value });
});
}
catch{
console.log("Not connected to tab. (is this apart of https://*.cloud.panopto.eu/* ?)")
}
}
document.addEventListener("DOMContentLoaded", function() {
checkIfFirstTimeUse = localStorage.getItem('panoptoExtSpeed');
if(!checkIfFirstTimeUse) {
localStorage.setItem('panoptoExtSpeed','1');
}
checkIfFirstTimeUse = localStorage.getItem('panoptoExtToggle');
if(!checkIfFirstTimeUse) {
localStorage.setItem('panoptoExtToggle','false')
}
delete(checkIfFirstTimeUse);
const slider = document.getElementById('dynamicSlider');
const editableSpan = document.getElementById('sliderValue');
const checkBox = document.getElementById('startupSpeed');
let z = localStorage.getItem('panoptoExtToggle');
if(z){
if(z == "true"){
checkBox.checked = true;
}
}
checkBox.addEventListener('click', () => {
if(checkBox.checked){
localStorage.setItem('panoptoExtToggle', 'true');
}
else{
localStorage.setItem('panoptoExtToggle', 'false');
}
console.log(localStorage.getItem('panoptoExtToggle'));
sendToTab("speedOnStartup", localStorage.getItem('panoptoExtToggle'));
});
editableSpan.textContent = localStorage.getItem('panoptoExtSpeed') + "x";
console.log(editableSpan.textContent)
editableSpan.addEventListener('click', () => {
editableSpan.setAttribute('contenteditable', 'true');
editableSpan.focus();
})
editableSpan.addEventListener('blur', () => {
editableSpan.removeAttribute('contenteditable');
editableSpan.textContent = removeAllNonNums(editableSpan.textContent) + "x"
// SEND THE THING
sendToTab("updateSpeed", localStorage.getItem('panoptoExtSpeed'));
});
editableSpan.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault(); // Prevent new line
editableSpan.blur(); // Trigger blur event to accept the value
} else if (!isNumericKey(event)) {
event.preventDefault(); // Prevent non-numeric characters
}
});
function removeAllNonNums(textStr)
{
let output = textStr.replace(/[^\d.]/g, '')
const floatValue = parseFloat(output);
if (!isNaN(floatValue)) {
localStorage.setItem('panoptoExtSpeed', floatValue.toString())
slider.value = floatValue.toString()
return floatValue.toString(); // Return as string to preserve decimal places
}
// Attempt to parse as integer
const intValue = parseInt(output);
if (!isNaN(intValue)) {
localStorage.setItem('panoptoExtSpeed', intValue.toString())
slider.value = intValue.toString()
return intValue.toString(); // Return as string to remove any leading zeros
}
return '1.00'
}
function isNumericKey(event) {
// Allow backspace, delete, arrow keys, and numeric input
const key = event.key;
return (key >= '0' && key <= '9') || ['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'x', 'X', '.', 'Ctrl', 'a'].includes(key);
}
// Set default values if they don't exist
let storedSpeed = parseFloat(localStorage.getItem('panoptoExtSpeed'));
if (isNaN(storedSpeed)) {
storedSpeed = 1;
localStorage.setItem('panoptoExtSpeed', '1');
}
const sliderValue = document.getElementById('sliderValue');
// Check if the stored speed is above the slider's default max (3)
let currentMax = parseFloat(slider.max);
if (storedSpeed > currentMax) {
slider.max = storedSpeed; // Update slider max to reflect the actual speed
}
// Set the current slider value to the stored speed
slider.value = storedSpeed;
sliderValue.innerText = `${storedSpeed.toFixed(2)}x`;
slider.addEventListener('input', function() {
updateSlider(this);
});
slider.addEventListener('mouseup', function() {
sendSpeed();
})
function updateSlider(sliderElem) {
let value = parseFloat(sliderElem.value);
localStorage.setItem('panoptoExtSpeed', value);
sliderValue.innerText = `${value.toFixed(2)}x`;
}
function sendSpeed() {
let value = parseFloat(slider.value);
sendToTab("updateSpeed", value);
}
function formatTime(seconds) {
const days = Math.floor(seconds / 86400); // 86400 seconds in a day
seconds %= 86400;
const hours = Math.floor(seconds / 3600); // 3600 seconds in an hour
seconds %= 3600;
const minutes = Math.floor(seconds / 60); // 60 seconds in a minute
seconds = Math.floor(seconds % 60); // Remaining seconds
let formattedTime = "";
if (days > 0) formattedTime += `${days} day${days !== 1 ? "s" : ""}, `;
if (hours > 0) formattedTime += `${hours} hour${hours !== 1 ? "s" : ""}, `;
if (minutes > 0) formattedTime += `${minutes} minute${minutes !== 1 ? "s" : ""}, `;
if (seconds > 0 || formattedTime === "") formattedTime += `${seconds} second${seconds !== 1 ? "s" : ""}`;
// Remove trailing comma and space if any
return formattedTime.replace(/, $/, "");
}
const timeSavedEl = document.getElementById("timeSaved");
// Function to get the current saved time from the background script
const updateSavedTime = () => {
chrome.runtime.sendMessage({ type: "getSavedTime" }, (response) => {
if (response?.savedTime != null) {
timeSavedEl.textContent = `${formatTime(response.savedTime)}`;
}
});
};
// Update the saved time every second
setInterval(updateSavedTime, 1000);
// Initial update
updateSavedTime();
});