From b19389776d3887031d62845aaf1e1844c60f8136 Mon Sep 17 00:00:00 2001 From: Joe Jarvis Date: Sat, 8 Feb 2020 11:22:54 -0500 Subject: [PATCH] Fix filter by visits --- extension/background.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) mode change 100644 => 100755 extension/background.js diff --git a/extension/background.js b/extension/background.js old mode 100644 new mode 100755 index 5a3b18c..43979d5 --- a/extension/background.js +++ b/extension/background.js @@ -1,20 +1,29 @@ /* global defaultValues */ +// brute force history items with visitCount or less visits async function filterByVisits(visitCount, end) { - // brute force history items with visitCount or less visits - let 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}); + let start = 0; + + let totalHistory = []; + + let history = await browser.history.search({ + text: "", + startTime: start, + endTime: end, + maxResults: 1 + }); + while (history.length > 0) { + totalHistory = totalHistory.concat(history); + start = history[history.length - 1].lastVisitTime + 1; + history = await browser.history.search({text: "", startTime: start, endTime: end, maxResults: 1}); } - return history.filter(item => item.visitCount <= visitCount); + return totalHistory.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 }); } } @@ -34,13 +43,13 @@ async function deleteOlderThan(state) { if (res.deleteMode === "days") { // delete by range OR visit count and range if (res.visitCount === 0) { - browser.history.deleteRange({startTime: 0, endTime: endDate}); + browser.history.deleteRange({ startTime: 0, endTime: endDate }); } else { deleteByVisits(res.visitCount, endDate); } } } else if (res.deleteMode === "visits") { - deleteByVisits(res.visitCount, new Date().getTime()); + deleteByVisits(res.visitCount, Date.now()); } } }