From d7a4aa15b7ed9c16fc16f4778af9fd64ce446c04 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sun, 6 Sep 2020 16:07:07 +0200 Subject: [PATCH] Turn into unconditional cleaner --- LICENSE | 2 +- README.md | 8 +--- extension/background.js | 73 ++--------------------------------- extension/manifest.json | 21 +++------- extension/options.css | 22 ----------- extension/options.html | 35 ----------------- extension/options.js | 39 ------------------- extension/webext_utilities.js | 25 ------------ package.json | 10 ++--- 9 files changed, 18 insertions(+), 217 deletions(-) delete mode 100644 extension/options.css delete mode 100644 extension/options.html delete mode 100644 extension/options.js delete mode 100644 extension/webext_utilities.js diff --git a/LICENSE b/LICENSE index 35ad3fb..7c0bc25 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md index 4da100f..2f86f63 100644 --- a/README.md +++ b/README.md @@ -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/) \ No newline at end of file diff --git a/extension/background.js b/extension/background.js index 43979d5..b4aaea1 100755 --- a/extension/background.js +++ b/extension/background.js @@ -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); diff --git a/extension/manifest.json b/extension/manifest.json index f63f3c6..5ed190e 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -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" + ] } diff --git a/extension/options.css b/extension/options.css deleted file mode 100644 index 7789e18..0000000 --- a/extension/options.css +++ /dev/null @@ -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%; -} diff --git a/extension/options.html b/extension/options.html deleted file mode 100644 index 7d30192..0000000 --- a/extension/options.html +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - -
- - - - - - - - - - - - -
Number of days to keep history (set to 0 to disable)
Maximum number of visits
Delete Mode - -
- -

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)

-

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.)

-

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.)

-
- - - - diff --git a/extension/options.js b/extension/options.js deleted file mode 100644 index 2850c4a..0000000 --- a/extension/options.js +++ /dev/null @@ -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); diff --git a/extension/webext_utilities.js b/extension/webext_utilities.js deleted file mode 100644 index 70e0f7c..0000000 --- a/extension/webext_utilities.js +++ /dev/null @@ -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; -} diff --git a/package.json b/package.json index dd9a46c..a26d35c 100755 --- a/package.json +++ b/package.json @@ -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",