HistoryCleaner/extension/background.js

63 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-09-09 23:02:04 +00:00
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") {
2018-09-09 23:02:04 +00:00
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);
}
2017-02-07 23:31:13 +00:00
}
2018-09-09 23:02:04 +00:00
}
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);
2017-02-07 23:31:13 +00:00
}
2018-09-09 23:02:04 +00:00
browser.storage.local.set(res);
}
browser.idle.onStateChanged.addListener(deleteOlderThan);
browser.runtime.onInstalled.addListener(setup);