-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
99 lines (91 loc) · 3.38 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
document.addEventListener('DOMContentLoaded', function () {
const startBtn = document.getElementById('start-btn');
const resetBtn = document.getElementById('reset-btn');
const clipboardBtn = document.getElementById('clipboard-btn');
const copiedMessage = document.getElementById('copied-message');
const totalHoursElement = document.querySelector('.bis-p');
// Retrieve and display the total hours from chrome storage when the popup is opened
chrome.storage.local.get(['totalHours'], function (result) {
const totalHours = result.totalHours || 0;
totalHoursElement.textContent = `Total Hours: ${totalHours}`;
});
console.log('DOM fully loaded');
console.log('Start button:', startBtn);
console.log('Clipboard button:', clipboardBtn);
console.log('Copied message:', copiedMessage);
console.log('Reset button:', resetBtn);
if (startBtn) {
startBtn.addEventListener('click', function () {
console.log('Start button clicked');
// Send a message to the content script to start calculation
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(
tabs[0].id,
{ action: 'start' },
function (response) {
// Update the total hours display with the response from the content script
if (response && response.totalHours !== undefined) {
totalHoursElement.textContent = `Total Hours: ${response.totalHours}`;
// Store the updated total hours in chrome storage
chrome.storage.local.set(
{ totalHours: response.totalHours },
function () {
console.log(
'Total hours updated in storage:',
response.totalHours
);
}
);
}
}
);
});
});
} else {
console.error('Start button not found');
}
if (resetBtn) {
resetBtn.addEventListener('click', function () {
console.log('Reset button clicked');
// Clear the stored total hours and reset the display
chrome.storage.local.remove('totalHours', function () {
totalHoursElement.textContent = 'Total Hours: 0';
console.log('Total hours cleared from storage');
});
});
} else {
console.error('Reset button not found');
}
if (clipboardBtn && copiedMessage) {
clipboardBtn.addEventListener('click', function () {
// Log the action
console.log('Clipboard button clicked');
// Copy the total hours to clipboard
const totalHours = totalHoursElement.textContent.replace(
'Total Hours: ',
''
);
navigator.clipboard
.writeText(totalHours)
.then(() => {
// Show the copied message
copiedMessage.style.display = 'block';
// Hide the message after 2 seconds
setTimeout(() => {
copiedMessage.style.display = 'none';
}, 2000);
})
.catch((err) => {
console.error('Failed to copy: ', err);
});
});
} else {
console.error('Clipboard button or copied message element not found');
}
// Clear the stored data when the popup is closed
window.addEventListener('unload', function () {
chrome.storage.local.remove('totalHours', function () {
console.log('Total hours cleared from storage');
});
});
});