-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
225 lines (192 loc) · 6.91 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
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
//this file has access to the popup
// Function to format a date in the format "Day, MM/DD"
function formatDate(date) {
const options = {weekday: "long", month: "2-digit", day: "2-digit"}
return date.toLocaleDateString("en-US", options)
}
// Function to get the start of the week (Monday)
function getStartOfWeek(date) {
const day = date.getDay()
const diff = date.getDate() - day + (day === 0 ? -6 : 1) // Adjust if it's Sunday (0)
const startOfWeek = new Date(date.setDate(diff))
return startOfWeek
}
// Function to get the end of the week (Sunday)
function getEndOfWeek(startOfWeek) {
const endOfWeek = new Date(startOfWeek)
endOfWeek.setDate(startOfWeek.getDate() + 6) // Sunday
return endOfWeek
}
// Get today's date and the start/end of the week
const today = new Date()
const startOfWeek = getStartOfWeek(new Date(today))
const endOfWeek = getEndOfWeek(startOfWeek)
// Format today's date and the week range
const todayFormatted = formatDate(today)
const weekRange = `${formatDate(startOfWeek)} - ${formatDate(endOfWeek)}`
// Update the DOM with today's date and the week range
document.getElementById("todayDate").textContent = `(${todayFormatted})`
document.getElementById("weekRange").textContent = `(${weekRange})`
document.getElementById("today").addEventListener("click", () => {
const leaderboardContainer = document.getElementById("leaderboard")
leaderboardContainer.innerHTML = "" // Clear any previous content
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
// Dynamically inject content script
chrome.scripting.executeScript(
{
target: {tabId: tabs[0].id},
files: ["content.js"],
},
() => {
// Now send the message after content.js is injected
chrome.tabs.sendMessage(tabs[0].id, {type: "scroll"}, (response) => {
if (chrome.runtime.lastError) {
console.error("Error sending message:", chrome.runtime.lastError)
} else {
console.log(response.status)
}
})
}
)
})
})
document.getElementById("week").addEventListener("click", () => {
const leaderboardContainer = document.getElementById("leaderboard")
leaderboardContainer.innerHTML = "" // Clear any previous content
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
// Dynamically inject content script
chrome.scripting.executeScript(
{
target: {tabId: tabs[0].id},
files: ["content.js"],
},
() => {
// Now send the message after content.js is injected
chrome.tabs.sendMessage(tabs[0].id, {type: "scrollWeek"}, (response) => {
if (chrome.runtime.lastError) {
console.error("Error sending message:", chrome.runtime.lastError)
} else {
console.log(response.status)
}
})
}
)
})
})
// Listen for the message from the background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === "SEND_JSON_TO_POPUP") {
// console.log('Received JSON in popup:', message.data);
// Update the UI (e.g., display the leaderboard)
displayLeaderboard(message.data)
}
})
function sortAndFilterLeaderboard(data) {
// Filter out items where the 'count' property is falsy
const filteredData = data.filter((item) => item.count && item.count !== "")
// Sort the filtered data by the 'sender' property
filteredData.sort((a, b) => {
const senderA = a.sender.toLowerCase() // Case insensitive sorting
const senderB = b.sender.toLowerCase()
if (senderA < senderB) {
return -1 // a comes before b
}
if (senderA > senderB) {
return 1 // b comes before a
}
return 0 // equal
})
return filteredData
}
function sumCount(count) {
let summ = 0
let error = false
try {
summ = count.split("-").reduce((accumulator, currentValue) => accumulator + parseFloat(currentValue), 0)
} catch (e) {
error = true
}
if (error) console.log(count, "AUSTIN AUSTIN AUSTIN")
return summ
}
function countDate(data) {
// Object to track the highest count per sender
const senderMaxCount = {}
// Loop through the data to find the highest count for each sender
data.forEach((entry) => {
const countValue = sumCount(entry.count)
if (!senderMaxCount[entry.sender] || countValue > sumCount(senderMaxCount[entry.sender].count)) {
senderMaxCount[entry.sender] = entry
}
})
return Object.values(senderMaxCount)
}
// Example function to display the leaderboard in the popup
function displayLeaderboard(data) {
const leaderboardContainer = document.getElementById("leaderboard")
leaderboardContainer.innerHTML = "" // Clear any previous content
// Set the container to left-align
leaderboardContainer.style.textAlign = "left"
const sortedData = sortAndFilterLeaderboard(data)
const highestCountData = countDate(sortedData)
if (highestCountData && highestCountData.length > 0) {
// Create the table and the header
const table = document.createElement("table")
table.classList.add("leaderboard-table")
// Create the table header
const headerRow = document.createElement("tr")
const senderHeader = document.createElement("th")
senderHeader.innerText = "Sender"
const countHeader = document.createElement("th")
countHeader.innerText = "Count"
const dateHeader = document.createElement("th")
dateHeader.innerText = "Date"
headerRow.appendChild(senderHeader)
headerRow.appendChild(countHeader)
headerRow.appendChild(dateHeader)
table.appendChild(headerRow)
// Create a row for each item in the highestCountData
highestCountData.forEach((item) => {
const row = document.createElement("tr")
const senderCell = document.createElement("td")
senderCell.innerText = item.sender
const countCell = document.createElement("td")
countCell.innerText = item.count
const dateCell = document.createElement("td")
dateCell.innerText = item.date
row.appendChild(senderCell)
row.appendChild(countCell)
row.appendChild(dateCell)
table.appendChild(row)
})
// Append the table to the leaderboard container
leaderboardContainer.appendChild(table)
} else {
const message = document.createElement("p")
message.innerText = "No leaderboard data available."
leaderboardContainer.appendChild(message)
}
}
// Add event listener to the "Copy" button
document.getElementById("Copy").addEventListener("click", () => {
// Ensure the leaderboard is displayed before attempting to copy
const leaderboardContainer = document.getElementById("leaderboard")
if (leaderboardContainer && leaderboardContainer.innerText.trim() !== "") {
copyLeaderboardToClipboard() // Call the copy function directly
} else {
console.log("Leaderboard content is empty or not loaded.")
}
})
// Function to copy the leaderboard content to the clipboard
function copyLeaderboardToClipboard() {
const leaderboardContainer = document.getElementById("leaderboard")
const leaderboardText = leaderboardContainer.innerText
navigator.clipboard
.writeText(leaderboardText)
.then(() => {
console.log("Leaderboard successfully copied to clipboard")
})
.catch((error) => {
console.error("Error copying text: ", error)
})
}