-
Notifications
You must be signed in to change notification settings - Fork 9
/
background.js
81 lines (68 loc) · 2.2 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
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-32579564-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
var capitalise = function(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
};
// Add context menu
(function(){
var contexts = ['link', 'image', 'audio', 'video'];
var len = contexts.length;
for (var i = 0; i < len; i++) {
(function() {
var context = contexts[i];
chrome.contextMenus.create({
title: chrome.i18n.getMessage('menuItemText' + capitalise(context)),
contexts: [context],
onclick: function(info, tab) {
if (context === 'link') {
saveToDrive(info.linkUrl, context);
} else {
saveToDrive(info.srcUrl, context);
}
}
});
})();
}
})();
var saveToDrive = function(url, context) {
_gaq.push(['_trackEvent', 'Save', 'clickSave', context]);
// Indicate saving in progress
showNotif(context);
var target = 'https://docs.google.com/viewer?a=sv&url=' + encodeURIComponent(url);
$.get(target, function(data, textStatus, jqXHR) {
onSaveSuccess(context);
}, 'html').error(function(jqXHR, textStatus, errorThrown) {
onSaveError(context, target);
});
};
var onSaveSuccess = function(context) {
_gaq.push(['_trackEvent', 'Save', 'succeed', context]);
chrome.extension.getViews({type: 'notification'}).forEach(function(notifWindow) {
notifWindow.onSaveSuccess();
});
};
var onSaveError = function(context, target) {
_gaq.push(['_trackEvent', 'Save', 'fail', context]);
chrome.extension.getViews({type: 'notification'}).forEach(function(notifWindow) {
notifWindow.onSaveError(target);
});
};
var showNotif = (function() {
var notif;
return function(context) {
notif && notif.cancel();
notif = window.webkitNotifications.createHTMLNotification(
'notif.html?context=' + context
);
notif.show();
};
})();
var onVisitDrive = function(label) {
_gaq.push(['_trackEvent', 'Notification', 'visitDrive', label]);
};