Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parental control sessionTimeout fixes #85

Merged
merged 4 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/parental_control/extension/content/ui/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,14 @@ const updateTimeLeftUI = async () => {
if (timeLeft <= 0) {
clearInterval(intervalId);
timeText.textContent = "Time's up";
timeHours.textContent = "00";
timeMinutes.textContent = "00";
}
} else {
clearInterval(intervalId);
timeText.textContent = "Time's up";
timeHours.textContent = "00";
timeMinutes.textContent = "00";
}
};
await updateTimer();
Expand Down
58 changes: 43 additions & 15 deletions components/parental_control/extension/service_worker/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@ const LOCAL_STORAGE_UPDATE_INTERVAL = 2e4;

let timerId;
// Function to restart the timer with the remaining time when the first window is opened again
const clearAllTimers = () => {
if (timerId) {
clearTimeout(timerId);
timerId = null;
}
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
}
}

const startTimer = async () => {
try {
const data = await chrome.storage.local.get(['timeLeft', 'loggedIn', 'sessionTimeout'])
if (!data.loggedIn || data.sessionTimeout) return;

clearAllTimers();

if (data.timeLeft) {
timerId = setTimeout(sessionTimeout, data.timeLeft);
updateTimeInLocalStorage(data.timeLeft);
Expand All @@ -25,7 +39,7 @@ chrome.tabs.onCreated.addListener(async (tab) => {
chrome.action.setIcon({ path: "../assets/Logo_active.png" });
}
if (data.loggedIn && data.sessionTimeout) {
chrome.tabs.update(tab.id, { url: '../content/ui/sessionTimeout.html' });
chrome.tabs.update(tab.id, { url: 'extension/content/ui/sessionTimeout.html' });
}
else if (data.loggedIn && !data.sessionTimeout) {
startTimer();
Expand All @@ -35,19 +49,24 @@ chrome.tabs.onCreated.addListener(async (tab) => {
// Function to update time in local storage every minute
let intervalId;
const updateTimeInLocalStorage = async (timeLeft) => {
if (intervalId) {
clearInterval(intervalId);
}

intervalId = setInterval(async () => {
timeLeft -= LOCAL_STORAGE_UPDATE_INTERVAL;
await chrome.storage.local.set({ timeLeft: timeLeft })
if (timeLeft < 0) {
clearInterval(intervalId)
clearInterval(intervalId);
intervalId = null;
}
}, LOCAL_STORAGE_UPDATE_INTERVAL); // 20000 milliseconds = 20 sec
}, LOCAL_STORAGE_UPDATE_INTERVAL);
}

const sessionTimeout = async () => {
await blockHttpsSearch();
await chrome.storage.local.set({ sessionTimeout: true });
const url = '../content/ui/sessionTimeout.html';
const url = 'extension/content/ui/sessionTimeout.html';
await handleBrowserWindows(url);
}

Expand Down Expand Up @@ -146,9 +165,12 @@ const logoutUser = async (password, sendResponse) => {
try {
chrome.action.setIcon({ path: "../assets/Logo_inactive.png" });
removeServiceWorker();
clearTimeout(timerId);
clearInterval(intervalId)
await chrome.storage.local.set({ loggedIn: false, sessionTimeout: false })
clearAllTimers();
await chrome.storage.local.set({
loggedIn: false,
sessionTimeout: false,
timeLeft: 0 // Reset timeLeft to prevent any lingering timer state
})
sendResponse({ status: true });
await allowHttpsSearchAsync();
await handleBrowserWindows();
Expand All @@ -160,14 +182,20 @@ const logoutUser = async (password, sendResponse) => {
}
}

const handleBrowserWindows = async (url) => {
const windows = await chrome.windows.getAll({ populate: true })
windows.forEach((window) => {
chrome.windows.remove(window.id);
});
if (!url) await chrome.windows.create({ type: 'normal' });
else chrome.windows.create({ url: url, type: 'normal' });
}
const handleBrowserWindows = async (url) => {
let newWindow;
if(url === 'extension/content/ui/sessionTimeout.html')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this url ('extension/content/ui/sessionTimeout.html') a constant

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and instead of passing a url you can just pass a flag for to show session timed out screen or not

newWindow = await chrome.windows.create({ url: url, type: 'normal' });
else
newWindow = await chrome.windows.create({ type: 'normal' });

const windows = await chrome.windows.getAll({ populate: true });
for (const window of windows) {
if (window.id !== newWindow.id) {
chrome.windows.remove(window.id);
}
}
};

const updateBlockingRules = async (rulesToInject) => {
const oldRules = await chrome.declarativeNetRequest.getDynamicRules();
Expand Down
6 changes: 3 additions & 3 deletions components/ping_ai_copilot/extension/content/ui/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@
}

#iconImage {
width: 40px;
height: 40px;
width: 27px;
height: 27px;
border-radius: 50%;
cursor: pointer;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.26);
transition: box-shadow 0.3s ease;
}

#iconImage:hover {
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.32);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.32);
}

#summary-box {
Expand Down
Loading