Turn into unconditional cleaner

This commit is contained in:
Kumi 2020-09-06 16:07:07 +02:00
parent aa74e125f4
commit d7a4aa15b7
9 changed files with 18 additions and 217 deletions

View file

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2017 Rayquaza01
Copyright (c) 2020 Klaus-Uwe Mitterer, 2017 Rayquaza01
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -1,10 +1,6 @@
# HistoryCleaner
Firefox addon that deletes history older than a specified amount of days.
Firefox addon that deletes history whenever leaving a page.
This addon is inspired by and is a WebExtension port of [Expire History By Days](https://addons.mozilla.org/en-US/firefox/addon/expire-history-by-days/).
Set the number of days to keep history items in the options page. Setting it to 0 will disable history deletion. The deletion will occur when the browser goes idle (after about 1 minute of inactivity).
The icon is from [Material Design Icons](https://materialdesignicons.com/)
[Link to addon page](https://addons.mozilla.org/en-US/firefox/addon/history-cleaner/)
The icon is from [Material Design Icons](https://materialdesignicons.com/)

View file

@ -1,71 +1,6 @@
/* global defaultValues */
// brute force history items with visitCount or less visits
async function filterByVisits(visitCount, 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 totalHistory.filter(item => item.visitCount <= visitCount);
async function deleteHistory(state) {
let end = new Date();
browser.history.deleteRange({ startTime: 0, endTime: end });
}
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) {
// get date x days ago
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 (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);
}
}
} else if (res.deleteMode === "visits") {
deleteByVisits(res.visitCount, Date.now());
}
}
}
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);
browser.webNavigation.onCompleted.addListener(deleteHistory);

View file

@ -1,27 +1,18 @@
{
"manifest_version": 2,
"name": "History Cleaner",
"version": "1.2.2",
"description": "Deletes history older than a specified amount of days.",
"applications": {
"gecko": {
"id": "{a138007c-5ff6-4d10-83d9-0afaf0efbe5e}"
}
},
"version": "1.2.2-kumi",
"description": "Deletes history on navigation.",
"icons": {
"48": "icons/icon-48.png",
"96": "icons/icon-96.png"
},
"background": {
"scripts": ["webext_utilities.js", "background.js"]
"scripts": ["background.js"]
},
"permissions": [
"history",
"storage",
"idle"
],
"options_ui": {
"page": "options.html",
"browser_style": true
}
"webNavigation"
]
}

View file

@ -1,22 +0,0 @@
body, input {
font-size: 1.25rem;
}
#settings {
width: 100%;
}
tr {
border-bottom: 1px solid black;
}
table > tr:last-child {
border-bottom: none;
}
input {
height: 30px;
text-align: right;
width: 100%;
}

View file

@ -1,35 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="options.css">
</head>
<body>
<div id="box">
<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 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>
<script src="webext_utilities.js"></script>
<script src="options.js"></script>
</body>
</html>

View file

@ -1,39 +0,0 @@
/* global generateElementsVariable */
const DOM = generateElementsVariable([
"days",
"visitCount",
"mode",
"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),
deleteMode: DOM.mode.value
});
e.preventDefault();
}
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.deleteMode || "days";
disable(res.deleteMode);
}
DOM.settings.addEventListener("input", updateDays);
document.addEventListener("DOMContentLoaded", restoreOptions);

View file

@ -1,25 +0,0 @@
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;
}

View file

@ -1,7 +1,7 @@
{
"name": "history-cleaner",
"version": "1.0.0",
"description": "Firefox addon that deletes history older than a specified amount of days.",
"description": "Firefox addon that deletes history when leaving a page.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
@ -10,14 +10,14 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/Rayquaza01/HistoryCleaner.git"
"url": "git+https://kumig.it/kumitterer/HistoryCleaner.git"
},
"author": "Rayquaza01",
"author": "Klaus-Uwe Mitterer",
"license": "MIT",
"bugs": {
"url": "https://github.com/Rayquaza01/HistoryCleaner/issues"
"url": "https://kumig.it/kumitterer/HistoryCleaner/issues"
},
"homepage": "https://github.com/Rayquaza01/HistoryCleaner#readme",
"homepage": "https://kumig.it/kumitterer/HistoryCleaner#readme",
"dependencies": {
"eslint": "^6.5.1",
"prettier": "^1.18.2",