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) {
// 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 {
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, {

View file

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

View file

@ -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;

View file

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

View file

@ -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);