-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.js
153 lines (142 loc) · 7.17 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
function isValidHex(str) {
return str.match(/^#[a-f0-9]{6}$/i) !== null;
}
function hexToRGB(str){
var ans = [0, 0, 0];
var splt = [str[0] + str[1], str[2] + str[3], str[4] + str[5]];
for(var i = 0; i < 3; i++){
ans[i] = parseInt(splt[i], 16);
}
return ans;
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return componentToHex(r) + componentToHex(g) + componentToHex(b);
}
document.addEventListener('DOMContentLoaded', function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if(!(/^https:\/\/www\.youtube\.com\/watch/.test(tabs[0].url))){
document.getElementById("settings").style.display = "none";
document.getElementById("loadingComments").style.display = "none";
document.getElementById("otherURL").style.display = "block";
document.getElementById("noComments").style.display = "none";
document.body.style.height = "55px";
} else {
chrome.tabs.sendMessage(tabs[0].id, {retrieveStatus: true}, function(response) {
if(response.loadingComments){
document.getElementById("settings").style.display = "none";
document.getElementById("loadingComments").style.display = "block";
document.getElementById("otherURL").style.display = "none";
document.getElementById("noComments").style.display = "none";
document.body.style.height = "40px";
} else {
if(response.nonzero){
document.getElementById("settings").style.display = "block";
document.getElementById("loadingComments").style.display = "none";
document.getElementById("otherURL").style.display = "none";
document.getElementById("noComments").style.display = "none";
document.body.style.height = "350px";
} else {
document.getElementById("settings").style.display = "none";
document.getElementById("loadingComments").style.display = "none";
document.getElementById("otherURL").style.display = "none";
document.getElementById("noComments").style.display = "block";
document.body.style.height = "55px";
}
}
});
}
});
var toggleHeatmap = document.getElementById('toggleHeatmap');
toggleHeatmap.addEventListener('click', function(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {toggleHeatmap: true, value: tabs[0].id}, function(response) {});
});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {toggleMarkers: true}, function(response) {});
});
});
var saveButton = document.getElementById("saveButton");
saveButton.addEventListener('click', function(){
const selectedHex = document.getElementById("hexInput").value;
if(!isValidHex("#"+selectedHex)){
document.getElementById("hexInput").value = "";
} else {
document.getElementById("colorPreview").style.backgroundColor = "#"+selectedHex;
document.getElementById("hexInput").placeholder = selectedHex
const rgbVal = hexToRGB(selectedHex);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {primaryColorChange: true, colorValue: rgbVal}, function(response) {});
});
}
})
var toggleComments = document.getElementById("toggleComments");
toggleComments.addEventListener('click', function(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {toggleComments: true}, function(response) {});
});
})
var toggleLiveComments = document.getElementById("toggleLiveComments");
toggleLiveComments.addEventListener('click', function(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {toggleLiveComments: true}, function(response) {});
});
})
var slider = document.getElementById("densitySlider");
var output = document.getElementById("valueDisplay");
slider.oninput = function() {
output.innerHTML = this.value.toString() + ((this.value == 1) ? " second" : " seconds");
const val = this.value.toString()
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {densityChange: true, densityValue: val}, function(response) {});
});
}
// Load and set API key toggle state
var apiKeyInputs = document.getElementsByClassName("apiKeyInput");
var apiKeyToggles = document.getElementsByClassName("toggleApiKey");
Array.from(apiKeyToggles).forEach(function(apiKeyToggle, index) {
var apiKeyInput = apiKeyInputs[index];
chrome.storage.local.get(['apiKey'], function(result) {
if (result.apiKey) {
apiKeyToggle.checked = true;
apiKeyInput.value = result.apiKey;
} else {
apiKeyToggle.checked = false;
apiKeyInput.disabled = true;
apiKeyInput.value = "";
}
});
// Toggle API key usage
apiKeyToggle.addEventListener("change", function() {
if (apiKeyToggle.checked) {
chrome.storage.local.set({ apiKey: apiKeyInput.value });
apiKeyInput.disabled = false;
} else {
chrome.storage.local.remove("apiKey");
apiKeyInput.disabled = true;
apiKeyInput.value = ""; // Revert to default display
}
});
// Save API key on input change if toggle is active
apiKeyInput.addEventListener("input", function() {
if (apiKeyToggle.checked) {
chrome.storage.local.set({ apiKey: apiKeyInput.value });
}
});
});
});
window.onload = function() {
chrome.storage.local.get(['heatmap', 'normalMarker', 'density', 'commentView', 'primaryColor', 'liveCommentView'], function(result) {
document.getElementById('toggleHeatmap').checked = result.heatmap;
document.getElementById("densitySlider").value = result.density;
document.getElementById("valueDisplay").innerHTML = result.density.toString() + ((result.density == 1) ? " second" : " seconds");
document.getElementById("toggleComments").checked = result.commentView;
var changedHex = rgbToHex(result.primaryColor[0],result.primaryColor[1],result.primaryColor[2]);
document.getElementById("colorPreview").style.backgroundColor = "#" + changedHex;
document.getElementById("hexInput").placeholder = changedHex;
document.getElementById("toggleLiveComments").checked = result.liveCommentView
});
}