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 all 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
60 changes: 44 additions & 16 deletions components/parental_control/extension/service_worker/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ import { socialMediaBlockRules } from "../assets/rules/socialMediaBlockRules.js"
import { gamingSiteRules } from "../assets/rules/gamesBlockRules.js";

const LOCAL_STORAGE_UPDATE_INTERVAL = 2e4;
const sessionTimeoutUrl = 'extension/content/ui/sessionTimeout.html';

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 +40,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: sessionTimeoutUrl });
}
else if (data.loggedIn && !data.sessionTimeout) {
startTimer();
Expand All @@ -35,20 +50,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';
await handleBrowserWindows(url);
await handleBrowserWindows(true);
}

// Function to block Google search URLs
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 (isSessionTimeout) => {
let newWindow;
if(isSessionTimeout)
newWindow = await chrome.windows.create({ url: sessionTimeoutUrl, 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