-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
49 lines (42 loc) · 1.65 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
var extensionActive = false;
var className;
var masterIntervals = {};
// Supposed to Called when the user clicks on the browser action icon.
chrome.browserAction.onClicked.addListener(function() {
extensionActive = true; // quick bug fix to two-click problem
if (extensionActive) {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
masterIntervals["url"] = tabs[0].url;
chrome.tabs.sendMessage(tabs[0].id, { action: "start" }, function(response) {
});
});
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "stop" }, function(response) {
});
});
}
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.action == "setClass") {
console.log(request.class);
if (className)
masterIntervals[className] = request.prevIntervals;
className = request.class;
sendResponse({
intervals: (masterIntervals[className] ? masterIntervals[className] : [])
});
} else if (request.action == "download") {
if (className)
masterIntervals[className] = request.prevIntervals;
chrome.downloads.download({
url: "data:," + encodeURIComponent(JSON.stringify(pruneIntervals(masterIntervals))),
filename: "intervals" + ".json" // maybe make the filename the vid id
});
}
}
);
function pruneIntervals() { // TODO implement this
return masterIntervals;
}