Improved deleting based on number of visits.

This commit is contained in:
Joe Jarvis 2019-06-25 11:08:59 -04:00
parent 2bc552565b
commit a61ce38780
5 changed files with 62 additions and 41 deletions

View file

@ -1,20 +1,20 @@
/* global defaultValues */
async function filterByVisits(visitCount, end) { async function filterByVisits(visitCount, end) {
// brute force history items with visitCount or less visits // brute force history items with visitCount or less visits
let search = 1000; let search = 1000;
while (true) { let history = await browser.history.search({text: "", maxResults: search, startTime: 0, endTime: end});
const history = await browser.history.search({text: "", maxResults: search, startTime: 0, endTime: end}); while (search < history.length) {
if (search > history.length) { search += 1000;
return history.filter(item => item.visitCount <= visitCount); history = await browser.history.search({text: "", maxResults: search, startTime: 0, endTime: end});
} else {
search += 1000;
}
} }
return history.filter(item => item.visitCount <= visitCount);
} }
async function deleteByVisits(visitCount, end) { async function deleteByVisits(visitCount, end) {
let history = await filterByVisits(visitCount, end); let history = await filterByVisits(visitCount, end);
for (let item of history) { 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 res = await browser.storage.local.get();
const days = parseInt(res.days) || 0; const days = parseInt(res.days) || 0;
if (days !== 0) { if (days !== 0) {
// get date x days ago
let end = new Date(); let end = new Date();
end.setHours(0); end.setHours(0);
end.setMinutes(0); end.setMinutes(0);
@ -30,21 +31,20 @@ async function deleteOlderThan(state) {
end.setMilliseconds(0); end.setMilliseconds(0);
end.setDate(end.getDate() - days); end.setDate(end.getDate() - days);
let endDate = end.getTime(); let endDate = end.getTime();
if (mode === "days") { if (res.deleteMode === "days") {
// delete by range OR visit count and range
if (res.visitCount === 0) { if (res.visitCount === 0) {
browser.history.deleteRange({startTime: 0, endTime: endDate}); browser.history.deleteRange({startTime: 0, endTime: endDate});
} else { } else {
deleteByVisits(res.visitCount, endDate); deleteByVisits(res.visitCount, endDate);
} }
} }
} } else if (res.deleteMode === "visits") {
if (mode === "visits") {
deleteByVisits(res.visitCount, new Date().getTime()); deleteByVisits(res.visitCount, new Date().getTime());
} }
} }
} }
async function setup() { async function setup() {
let res = await browser.storage.local.get(); let res = await browser.storage.local.get();
res = defaultValues(res, { res = defaultValues(res, {

View file

@ -13,7 +13,7 @@
"96": "icons/icon-96.png" "96": "icons/icon-96.png"
}, },
"background": { "background": {
"scripts": ["background.js"] "scripts": ["webext_utilities.js", "background.js"]
}, },
"permissions": [ "permissions": [
"history", "history",

View file

@ -1,22 +1,20 @@
body, input { body, input {
font-size: 1.25rem; font-size: 1.25rem;
} }
#box {
align-items: center;
display: flex;
float: left;
}
#labels {
float: left;
width: 50%;
}
#settings { #settings {
float: right; width: 100%;
width: 50%;
} }
span {
line-height: 30px; tr {
border-bottom: 1px solid black;
} }
table > tr:last-child {
border-bottom: none;
}
input { input {
height: 30px; height: 30px;
text-align: right; text-align: right;

View file

@ -6,19 +6,28 @@
</head> </head>
<body> <body>
<div id="box"> <div id="box">
<div id="labels"> <table id="settings">
<span>Number of days to keep history (set to 0 to disable)</span><br> <tr>
<span>Maximum no of visits</span><br> <td>Number of days to keep history (set to 0 to disable)</td>
<span>Delete Mode</span> <td><input id="days" type="number" min="0"></td>
</div> </tr>
<div id="settings"> <tr>
<input id="days" type="number" min="0"> <td>Maximum number of visits</td>
<input id="visitCount" type="number" min="0"> <td><input id="visitCount" type="number" min="0"></td>
<select id="mode"> <tr>
<option value="days">Delete older than X days</option> <td>Delete Mode</td>
<option value="visits">Delete sites with &lt; X visits</option> <td>
</select> <select id="mode">
</div> <option value="days">Delete older than X days</option>
<option value="visits">Delete URLs with &lt; X visits</option>
</select>
</td>
</tr>
</table>
<p>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)</p>
<p>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.)</p>
<p>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.)</p>
</div> </div>
<script src="webext_utilities.js"></script> <script src="webext_utilities.js"></script>
<script src="options.js"></script> <script src="options.js"></script>

View file

@ -1,3 +1,5 @@
/* global generateElementsVariable */
const DOM = generateElementsVariable([ const DOM = generateElementsVariable([
"days", "days",
"visitCount", "visitCount",
@ -5,11 +7,21 @@ const DOM = generateElementsVariable([
"settings" "settings"
]); ]);
function disable(mode) {
if (mode === "days") {
DOM.visitCount.disabled = true;
} else {
DOM.visitCount.disabled = false;
}
}
function updateDays(e) { function updateDays(e) {
disable(DOM.mode.value);
browser.storage.local.set({ browser.storage.local.set({
days: parseInt(DOM.days.value), days: parseInt(DOM.days.value),
visitCount: parseInt(DOM.visitCount.value), visitCount: parseInt(DOM.visitCount.value),
mode: DOM.mode.value deleteMode: DOM.mode.value
}); });
e.preventDefault(); e.preventDefault();
} }
@ -18,7 +30,9 @@ async function restoreOptions() {
const res = await browser.storage.local.get(); const res = await browser.storage.local.get();
DOM.days.value = res.days || 0; DOM.days.value = res.days || 0;
DOM.visitCount.value = res.visitCount || 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); DOM.settings.addEventListener("input", updateDays);