Started setup for delete by visitCount

This commit is contained in:
Joe Jarvis 2018-09-09 19:02:04 -04:00
parent 582936eab4
commit 2bc552565b
4 changed files with 115 additions and 22 deletions

View file

@ -1,16 +1,62 @@
browser.idle.onStateChanged.addListener((state) => {
if (state === "idle") {
browser.storage.local.get("days").then((res) => {
var days = parseInt(res.days) || 0;
if (days !== 0) {
var end = new Date();
end.setHours(0);
end.setMinutes(0);
end.setSeconds(0);
end.setMilliseconds(0);
end.setDate(end.getDate() - days);
browser.history.deleteRange({startTime: 0, endTime: end.getTime()});
}
});
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;
}
}
});
}
async function deleteByVisits(visitCount, end) {
let history = await filterByVisits(visitCount, end);
for (let item of history) {
browser.history.deleteURL({url: item.url});
}
}
async function deleteOlderThan(state) {
if (state === "idle") {
const res = await browser.storage.local.get();
const days = parseInt(res.days) || 0;
if (days !== 0) {
let end = new Date();
end.setHours(0);
end.setMinutes(0);
end.setSeconds(0);
end.setMilliseconds(0);
end.setDate(end.getDate() - days);
let endDate = end.getTime();
if (mode === "days") {
if (res.visitCount === 0) {
browser.history.deleteRange({startTime: 0, endTime: endDate});
} else {
deleteByVisits(res.visitCount, endDate);
}
}
}
if (mode === "visits") {
deleteByVisits(res.visitCount, new Date().getTime());
}
}
}
async function setup() {
let res = await browser.storage.local.get();
res = defaultValues(res, {
days: 0,
visitCount: 0,
deleteMode: "days"
});
if (typeof res.days === "string") {
res.days = parseInt(res.days);
}
browser.storage.local.set(res);
}
browser.idle.onStateChanged.addListener(deleteOlderThan);
browser.runtime.onInstalled.addListener(setup);

View file

@ -7,12 +7,20 @@
<body>
<div id="box">
<div id="labels">
<span>Number of days to keep history (set to 0 to disable)</span>
<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">
<select id="mode">
<option value="days">Delete older than X days</option>
<option value="visits">Delete sites with &lt; X visits</option>
</select>
</div>
</div>
<script src="webext_utilities.js"></script>
<script src="options.js"></script>
</body>
</html>

View file

@ -1,11 +1,25 @@
const DOM = generateElementsVariable([
"days",
"visitCount",
"mode",
"settings"
]);
function updateDays(e) {
browser.storage.local.set({days: document.querySelector("#days").value});
browser.storage.local.set({
days: parseInt(DOM.days.value),
visitCount: parseInt(DOM.visitCount.value),
mode: DOM.mode.value
});
e.preventDefault();
}
function restoreOptions() {
browser.storage.local.get().then((res) => {
document.querySelector("#days").value = res.days || 0;
});
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.settings.addEventListener("input", updateDays);
document.addEventListener("DOMContentLoaded", restoreOptions);
document.getElementById("days").addEventListener("input", updateDays);

View file

@ -0,0 +1,25 @@
function generateElementsVariable(list) {
// generate an object with elements based on a list of ids
let dom = {};
for (let item of list) {
dom[item] = document.getElementById(item);
}
return dom;
}
function defaultValues(object, settings) {
// initialize object with values.
for (let key in settings) {
if (!object.hasOwnProperty(key)) {
object[key] = settings[key];
}
}
return object;
}
function getContext() {
// return the context of the current view
return browser.extension.getViews({type: "popup"}).indexOf(window) > -1 ? "popup" :
browser.extension.getViews({type: "sidebar"}).indexOf(window) > -1 ? "sidebar" :
browser.extension.getViews({type: "tab"}).indexOf(window) > -1 ? "tab" : undefined;
}