-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
42 lines (29 loc) · 1.27 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
Limit the lifetime of history entries in Chrome.
WARNING: This isn't thoroughly tested, and could cause unexpected history changes / performance issues.
Author: Nick Semenkovich <[email protected]>
License: MIT
*/
"use strict";
function scanHistory() {
// Grab the history preference, or default to 4 days.
var maxHistoryLifeInDays = JSON.parse(localStorage.historyLimit || 4);
var maxHistoryLifeInMilliseconds = maxHistoryLifeInDays * 24 * 60 * 60 * 1000;
var maxTime = Date.now() - maxHistoryLifeInMilliseconds;
// Wow, this is an easy API.
chrome.history.deleteRange({startTime: 0, endTime: maxTime},
function() { if (chrome.runtime.lastError) { console.warn(chrome.runtime.lastError); } }
);
}
// Called on first run/update
function setup() {
// Run every two hours
chrome.alarms.create("scanHistory", { periodInMinutes: 120 });
// And let's run once right now!
scanHistory();
}
// Set some onInstalled listeners to fire, since we're not a persistent background page.
chrome.runtime.onInstalled.addListener(setup);
chrome.alarms.onAlarm.addListener(function(alarm) { scanHistory(); });
// Let's run once at startup, too, since alarm can be finicky.
chrome.runtime.onStartup.addListener(function() { scanHistory(); });