-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
181 lines (156 loc) · 4.93 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Chrome's downloads will be cleared out every 15 seconds.
var interval;
var settings;
var reloadInterval;
var reInit = (1.5 * 60 * 60 * 1000);
// The initialization routine is called only once.
onload = setTimeout(loadEverything, 1000);
chrome.storage.onChanged.addListener(function (changes, namespace) {
console.log("storage changed");
loadEverything();
});
function loadEverything() {
console.log("load everything");
chrome.storage.sync.get(['enabled', 'how_often', 'disable_shelf'], function (loadedSettings) {
console.log(JSON.stringify(loadedSettings));
if (loadedSettings) {
settings = loadedSettings;
var firstLoad = false;
if (typeof settings.disable_shelf === "undefined") {
settings.disable_shelf = false;
firstLoad = true;
}
if (typeof settings.enabled === "undefined") {
settings.enabled = false;
firstLoad = true;
}
if (typeof settings.how_often === "undefined") {
settings.how_often = 30;
firstLoad = true;
}
if (!!firstLoad) {
saveSettings();
}
} else {
settings = {
enabled: false,
how_often: 30,
disable_shelf: false
};
}
chrome.downloads.setShelfEnabled(!settings.disable_shelf);
init();
});
setupContextMenus();
}
function setupContextMenus() {
chrome.contextMenus.removeAll();
chrome.contextMenus.create({
title: "Change Options",
"contexts": ["browser_action"],
onclick: function (info, tab) {
chrome.tabs.create({ url: 'options.html' });
}
});
chrome.contextMenus.create({
title: "Donate to keep Extensions Alive",
"contexts": ["browser_action"],
onclick: function (info, tab) {
chrome.tabs.create({ url: 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=67TZLSEGYQFFW' });
}
});
chrome.contextMenus.create({
title: "Leave a review",
"contexts": ["browser_action"],
onclick: function (info, tab) {
chrome.tabs.create({ url: 'https://chrome.google.com/webstore/detail/always-clear-downloads-in/efoelbbfbknfhpmgclpcdbkoieedkkai' });
}
});
chrome.contextMenus.create({
title: "Report an issue",
"contexts": ["browser_action"],
onclick: function (info, tab) {
chrome.tabs.create({ url: 'https://github.com/stefanXO/Always-Clear-Downloads/issues' });
}
});
chrome.contextMenus.create({
title: "Send a suggestion",
"contexts": ["browser_action"],
onclick: function (info, tab) {
chrome.tabs.create({ url: 'https://github.com/stefanXO/Always-Clear-Downloads/issues' });
chrome.tabs.create({ url: 'mailto:[email protected]' });
}
});
}
function saveSettings() {
chrome.storage.sync.set(settings, function (s) {
console.log("saved");
});
}
reloadInterval = setInterval(init, reInit);
function init() {
// Set the interval at which the downloads will be erased.
clearTimeout(interval);
interval = setTimeout(clearDownloads, settings["how_often"] * 1000);
if(settings["how_often"] * 1000 >= reInit) {
clearInterval(reloadInterval);
var newReInit = (settings["how_often"] * 1000 * 3) + 1;
reloadInterval = setInterval(init, newReInit);
}else {
clearInterval(reloadInterval);
reloadInterval = setInterval(init, reInit);
}
};
function clearDownloads() {
clearTimeout(interval);
interval = setTimeout(clearDownloads, settings["how_often"] * 1000);
chrome.downloads.setShelfEnabled(!settings.disable_shelf);
if (!settings.enabled) return;
// Debug message only. Remove for actual runtime.
// alert ("About to remove Downloads");
// Perform the clearing of the browsing data. (0 means everything.)
chrome.browsingData.removeDownloads({ "since": 0 });
if (!settings.disable_shelf) {
chrome.downloads.search({ state: "in_progress", limit: 1 }, function (items) {
if (items.length > 0) {
// we have running downloads
// don't clear anything
} else {
chrome.downloads.setShelfEnabled(false);
chrome.downloads.setShelfEnabled(true);
}
});
}
chrome.downloads.erase({ state: "complete" });
// Debug message only. Remove for actual runtime.
// alert ("Downloads cleared!");
};
function openOrFocusOptionsPage() {
var optionsUrl = chrome.extension.getURL('options.html');
chrome.tabs.query({}, function (extensionTabs) {
var found = false;
for (var i = 0; i < extensionTabs.length; i++) {
if (optionsUrl == extensionTabs[i].url) {
found = true;
console.log("tab id: " + extensionTabs[i].id);
chrome.tabs.update(extensionTabs[i].id, { "selected": true });
}
}
if (found == false) {
chrome.tabs.create({ url: "options.html" });
}
});
}
chrome.extension.onConnect.addListener(function (port) {
var tab = port.sender.tab;
// This will get called by the content script we execute in
// the tab as a result of the user pressing the browser action.
port.onMessage.addListener(function (info) {
var max_length = 1024;
if (info.selection.length > max_length) info.selection = info.selection.substring(0, max_length);
openOrFocusOptionsPage();
});
});
chrome.browserAction.onClicked.addListener(function (tab) {
openOrFocusOptionsPage();
});