Skip to content

Commit

Permalink
Add time limit functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamman committed Mar 1, 2024
1 parent 2a9f09c commit d97719b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,33 @@ const redirectUrl = chrome.extension.getURL("redirect/index.html");

let lastActive = Date.now();

async function checkTimeLimit(s) {
let storage = s || await browser.storage.local.get();
// If the time limit is not enabled there's no sense checking if it's passed
if (!storage.preferences.general.timeLimit.enabled) return false;
// If time limit is passed return true
if (storage.stats.timeDistracted > (storage.preferences.general.timeLimit.time / 1000)) {
console.log("Turning blocking back on");
browser.storage.local.set({ blocking: { enabled: true } });
return true;
}
return false; // Default to false
}

function isBlocked(pageUrl) { return blockedOrigins.map(url => url == (new URL(pageUrl)).origin).sort().reverse()[0]; }

async function checkBlocking() {
let blocking = (await browser.storage.local.get()).blocking;
if (blocking.reEnable > 0 && !blocking.enabled && Date.now() > blocking.reEnable) {
let storage = await browser.storage.local.get(),
blocking = storage.blocking;
// If blocking is already enabled or allowed distracted time is up
if (blocking.enabled || await checkTimeLimit(storage)) return true;
// Reinable blocking after a break
if (blocking.reEnable > 0 && Date.now() > blocking.reEnable) {
console.log("Turning blocking back on");
browser.storage.local.set({
blocking: {
enabled: true
}
});
browser.storage.local.set({ blocking: { enabled: true } });
return true;
}
// And otherwise just send back it's current value
return blocking.enabled;
}

Expand Down

1 comment on commit d97719b

@benjamman
Copy link
Owner Author

Choose a reason for hiding this comment

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

This should close #1, it is a 100% rigid time limit though. It should be able to be overridden, which shouldn't take much effort to code in.

Please sign in to comment.