diff --git a/background.js b/background.js
index 20753eb..a80fb60 100644
--- a/background.js
+++ b/background.js
@@ -61,6 +61,17 @@ async function handleTab(tabId, url) {
chrome.storage.sync.set({'lastunlock': 0});
unlockTime = 0;
}
+
+ let midnight = new Date(options.lastUnlock * 1000);
+ midnight.setDate(midnight.getDate() + 1);
+ midnight.setHours(0);
+ midnight.setMinutes(0);
+ midnight.setSeconds(0);
+ midnight.setMilliseconds(0);
+ if (new Date(now * 1000) >= midnight) {
+ chrome.storage.sync.set({'remainingopens': Object.fromEntries(options.blacklist.map((key, _index) => [key, options.allowedOpens]))});
+ }
+
let unlocked = now - unlockTime <= options.siteTime;
if (unlocked) {
if (timeoutId !== null) {
@@ -74,12 +85,10 @@ async function handleTab(tabId, url) {
}
function lockTab(tabId, blacklist) {
- chrome.storage.sync.set({'lastunlock': 0}, () => {
- chrome.tabs.get(tabId, (tab) => {
- if (tab.active && tab.url && isDiscouraged(tab.url, blacklist)) {
- chrome.tabs.update(tabId, {url: chrome.runtime.getURL('unlock.html') + '?url=' + encodeURIComponent(tab.url)});
- }
- });
+ chrome.tabs.get(tabId, (tab) => {
+ if (tab.active && tab.url && isDiscouraged(tab.url, blacklist)) {
+ chrome.tabs.update(tabId, {url: chrome.runtime.getURL('unlock.html') + '?url=' + encodeURIComponent(tab.url)});
+ }
});
}
diff --git a/options.html b/options.html
index fa0fd58..887df9a 100644
--- a/options.html
+++ b/options.html
@@ -16,6 +16,10 @@
+ Number of allowed opens per day: (-1 to disable)
+
+
+
Schedule by time of day:
diff --git a/options.js b/options.js
index c3c47f8..19ef54c 100644
--- a/options.js
+++ b/options.js
@@ -1,11 +1,14 @@
async function getOptions() {
return new Promise((resolve) => {
- chrome.storage.sync.get(['blacklist', 'sitetime', 'frictiontime', 'lastunlock', 'schedule'], (items) => {
+ chrome.storage.sync.get(['blacklist', 'sitetime', 'frictiontime', 'allowedopens', 'remainingopens', 'lastunlock', 'schedule'], (items) => {
if (!items) {
+ let defaults = ['reddit.com', 'facebook.com', 'twitter.com'];
resolve({
- blacklist: ['reddit.com', 'facebook.com', 'twitter.com'],
- siteTime: 120,
- frictionTime: 6
+ blacklist: defaults,
+ siteTime: 1,
+ frictionTime: 6,
+ allowedOpens: -1,
+ remainingOpens: Object.fromEntries(defaults.map((key, _index) => [key, 0]))
});
return;
}
@@ -15,6 +18,8 @@ async function getOptions() {
}
let siteTime = items.sitetime || 120;
let frictionTime = items.frictiontime || 6;
+ let allowedOpens = items.allowedopens || -1;
+ let remainingOpens = items.remainingopens || Object.fromEntries(blacklist.map((key, _index) => [key, 0]));
let lastUnlock = items.lastunlock || 0;
let schedule = items.schedule || null;
@@ -22,6 +27,8 @@ async function getOptions() {
blacklist: blacklist,
siteTime: siteTime,
frictionTime: frictionTime,
+ allowedOpens: allowedOpens,
+ remainingOpens: remainingOpens,
lastUnlock: lastUnlock,
schedule: schedule,
});
diff --git a/optionspage.js b/optionspage.js
index 5eba41b..51bd02a 100644
--- a/optionspage.js
+++ b/optionspage.js
@@ -1,4 +1,4 @@
-let blacklistInput, siteTimeInput, frictionTimeInput;
+let blacklistInput, siteTimeInput, frictionTimeInput, allowedOpensInput;
let scheduleEnabledInput, scheduleFromInput, scheduleToInput;
let savedTimeout;
@@ -16,6 +16,10 @@ async function init() {
frictionTimeInput.value = options.frictionTime;
frictionTimeInput.addEventListener('change', () => save());
frictionTimeInput.addEventListener('blur', () => save());
+ allowedOpensInput = document.getElementById('allowedopens');
+ allowedOpensInput.value = options.allowedOpens;
+ allowedOpensInput.addEventListener('change', () => save());
+ allowedOpensInput.addEventListener('blur', () => save());
scheduleEnabledInput = document.getElementById('scheduleenabled');
scheduleEnabledInput.checked = options.schedule != null;
scheduleEnabledInput.addEventListener('change', () => save());
@@ -32,7 +36,7 @@ async function init() {
}
}
-function save() {
+async function save() {
let schedule = null;
if (scheduleEnabledInput.checked) {
let from = scheduleFromInput.valueAsNumber;
@@ -47,10 +51,20 @@ function save() {
{from: from, to: to},
];
}
+ let options = await getOptions();
+ let remainingOpens;
+ if (allowedOpensInput.value !== options.allowedOpens) {
+ remainingOpens = Object.fromEntries(Object.entries(options.remainingOpens).map((item, index) => [item[0], allowedOpensInput.value]));
+ } else {
+ remainingOpens = options.remainingOpens;
+ }
+
chrome.storage.sync.set({
'blacklist': blacklistInput.value,
'sitetime': siteTimeInput.value,
'frictiontime': frictionTimeInput.value,
+ 'allowedopens': allowedOpensInput.value,
+ 'remainingopens': remainingOpens,
'schedule': schedule
}, () => showSavedMessage());
}
diff --git a/unlock.html b/unlock.html
index 9cfa205..6dc9ad3 100644
--- a/unlock.html
+++ b/unlock.html
@@ -19,6 +19,8 @@
+
+