diff --git a/extension/background.js b/extension/background.js index 142d383..5a3b18c 100644 --- a/extension/background.js +++ b/extension/background.js @@ -1,20 +1,20 @@ +/* global defaultValues */ + async function filterByVisits(visitCount, end) { // brute force history items with visitCount or less visits let search = 1000; - while (true) { - const history = await browser.history.search({text: "", maxResults: search, startTime: 0, endTime: end}); - if (search > history.length) { - return history.filter(item => item.visitCount <= visitCount); - } else { - search += 1000; - } + let history = await browser.history.search({text: "", maxResults: search, startTime: 0, endTime: end}); + while (search < history.length) { + search += 1000; + history = await browser.history.search({text: "", maxResults: search, startTime: 0, endTime: end}); } + return history.filter(item => item.visitCount <= visitCount); } async function deleteByVisits(visitCount, end) { let history = await filterByVisits(visitCount, end); for (let item of history) { - browser.history.deleteURL({url: item.url}); + browser.history.deleteUrl({url: item.url}); } } @@ -23,6 +23,7 @@ async function deleteOlderThan(state) { const res = await browser.storage.local.get(); const days = parseInt(res.days) || 0; if (days !== 0) { + // get date x days ago let end = new Date(); end.setHours(0); end.setMinutes(0); @@ -30,21 +31,20 @@ async function deleteOlderThan(state) { end.setMilliseconds(0); end.setDate(end.getDate() - days); let endDate = end.getTime(); - if (mode === "days") { + if (res.deleteMode === "days") { + // delete by range OR visit count and range if (res.visitCount === 0) { browser.history.deleteRange({startTime: 0, endTime: endDate}); } else { deleteByVisits(res.visitCount, endDate); } } - } - if (mode === "visits") { + } else if (res.deleteMode === "visits") { deleteByVisits(res.visitCount, new Date().getTime()); } } } - async function setup() { let res = await browser.storage.local.get(); res = defaultValues(res, { diff --git a/extension/manifest.json b/extension/manifest.json index b85f486..f63f3c6 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -13,7 +13,7 @@ "96": "icons/icon-96.png" }, "background": { - "scripts": ["background.js"] + "scripts": ["webext_utilities.js", "background.js"] }, "permissions": [ "history", diff --git a/extension/options.css b/extension/options.css index fd834c7..7789e18 100644 --- a/extension/options.css +++ b/extension/options.css @@ -1,22 +1,20 @@ body, input { font-size: 1.25rem; } -#box { - align-items: center; - display: flex; - float: left; -} -#labels { - float: left; - width: 50%; -} + + #settings { - float: right; - width: 50%; + width: 100%; } -span { - line-height: 30px; + +tr { + border-bottom: 1px solid black; } + +table > tr:last-child { + border-bottom: none; +} + input { height: 30px; text-align: right; diff --git a/extension/options.html b/extension/options.html index e201746..7d30192 100644 --- a/extension/options.html +++ b/extension/options.html @@ -6,19 +6,28 @@
-
- Number of days to keep history (set to 0 to disable)
- Maximum no of visits
- Delete Mode -
-
- - - -
+ + + + + + + + + + + + +
Number of days to keep history (set to 0 to disable)
Maximum number of visits
Delete Mode + +
+ +

Setting the number of days will delete anything older than that amount of days. (Setting it to 7 will delete any history older than a week)

+

Setting a number of visits will cause it to delete sites only with that amount of visits or less. (Setting it to 5 will delete any URLs from your history that you only visited 5 or less times.)

+

If you have both a number of days and a number of visits set, it will delete history older than the number of days AND less than the number of visits. (Setting them to 7 and 5 will cause it to delete any is both older than a week AND has been visited 5 or less times.)

diff --git a/extension/options.js b/extension/options.js index d22c1de..2850c4a 100644 --- a/extension/options.js +++ b/extension/options.js @@ -1,3 +1,5 @@ +/* global generateElementsVariable */ + const DOM = generateElementsVariable([ "days", "visitCount", @@ -5,11 +7,21 @@ const DOM = generateElementsVariable([ "settings" ]); +function disable(mode) { + if (mode === "days") { + DOM.visitCount.disabled = true; + } else { + DOM.visitCount.disabled = false; + } +} + function updateDays(e) { + disable(DOM.mode.value); + browser.storage.local.set({ days: parseInt(DOM.days.value), visitCount: parseInt(DOM.visitCount.value), - mode: DOM.mode.value + deleteMode: DOM.mode.value }); e.preventDefault(); } @@ -18,7 +30,9 @@ async function restoreOptions() { const res = await browser.storage.local.get(); DOM.days.value = res.days || 0; DOM.visitCount.value = res.visitCount || 0; - DOM.mode.value = res.mode || "days"; + DOM.mode.value = res.deleteMode || "days"; + + disable(res.deleteMode); } DOM.settings.addEventListener("input", updateDays);