-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
125 lines (114 loc) · 4.8 KB
/
popup.js
File metadata and controls
125 lines (114 loc) · 4.8 KB
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
// Function to update login status
function updateLoginStatus() {
chrome.storage.local.get("token", (result) => {
console.log("Checking token status:", result);
const signInButton = document.getElementById("googleSignIn");
const logoutButton = document.getElementById("logoutButton");
const tabProductivity = document.getElementById("tab-productivity");
const historyButton = document.getElementById("historyButton");
if (result.token) {
console.log("Token found:", result.token);
signInButton.style.display = "none";
tabProductivity.style.display = "flex";
logoutButton.style.display = "flex";
historyButton.style.display = "flex";
} else {
console.log("No token found");
signInButton.style.display = "flex";
tabProductivity.style.display = "none";
logoutButton.style.display = "none";
historyButton.style.display = "none";
}
});
}
// Function to handle logout
function handleLogout() {
chrome.storage.local.remove("token", () => {
console.log("Token removed successfully");
updateLoginStatus();
});
}
// Handle the token from the URL after OAuth redirects
const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get('token');
console.log("URL token check:", token);
if (token) {
console.log("Storing new token:", token);
chrome.storage.local.set({ token }, () => {
if (chrome.runtime.lastError) {
console.error("Error storing token:", chrome.runtime.lastError);
} else {
console.log("Token stored successfully:", token);
// Update status immediately after storing token
updateLoginStatus();
// Redirect back to the popup window after successful login
window.location.href = `chrome-extension://${chrome.runtime.id}/popup.html`;
}
});
}
// Event Listener for Google Sign-In
document.getElementById("googleSignIn").addEventListener("click", () => {
console.log("Sign-in button clicked");
chrome.tabs.create({ url: "http://localhost:3000/auth/google" }, (tab) => {
console.log("OAuth tab opened successfully:", tab);
});
});
// Check login status and set up event listeners when popup opens
document.addEventListener('DOMContentLoaded', () => {
updateLoginStatus();
const logoutButton = document.getElementById("logoutButton");
if (logoutButton) {
logoutButton.addEventListener("click", handleLogout);
}
const historyButton = document.getElementById("historyButton");
if (historyButton) {
historyButton.addEventListener("click", () => {
chrome.tabs.create({ url: chrome.runtime.getURL("history.html") });
});
}
});
// Listen for token updates from background script
chrome.runtime.onMessage.addListener((message) => {
if (message.type === 'TOKEN_UPDATED') {
console.log("Received token update notification");
updateLoginStatus();
}
});
// Listen for messages from the background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === "TAB_ANALYZED") {
const { url, title, classification } = message.data;
// Normalize the classification value
const normalizedClassification = classification.trim().toLowerCase();
// Log the normalized classification for debugging
console.log("Normalized classification:", normalizedClassification);
// Update the popup UI with formatted content
const tabProductivityElement = document.getElementById("tab-productivity");
tabProductivityElement.innerHTML = `
<div style="padding: 20px; border: 2px solid ${
normalizedClassification === "productive"
? "green"
: normalizedClassification === "unproductive"
? "red"
: "orange"
}; border-radius: 10px; background-color: ${
normalizedClassification === "productive"
? "#e6ffe6"
: normalizedClassification === "unproductive"
? "#ffe6e6"
: "#fff5e6"
}; text-align: center;">
<h2 style="margin: 0; font-size: 24px; font-weight: bold;">
${
normalizedClassification === "productive"
? "Productive"
: normalizedClassification === "unproductive"
? "Unproductive"
: "Neutral"
}
</h2>
<p style="margin: 10px 0; font-size: 18px;"><strong>Title:</strong> ${title}</p>
</div>
`;
}
});